Skip to content

Commit

Permalink
Merge pull request #11659 from dependabot/dev/brettfo/nuget-config-no…
Browse files Browse the repository at this point in the history
…t-honored

ensure proper casing for `NuGet.Config` prior to any operations being performed
  • Loading branch information
randhircs authored Feb 24, 2025
2 parents c17984a + 96fad76 commit 10e77e2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions nuget/lib/dependabot/nuget/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def self.required_files_message

sig { override.returns(T::Array[DependencyFile]) }
def fetch_files
NativeHelpers.normalize_file_names
NativeHelpers.install_dotnet_sdks
discovery_json_reader = DiscoveryJsonReader.run_discovery_in_directory(
repo_contents_path: T.must(repo_contents_path),
Expand Down
16 changes: 16 additions & 0 deletions nuget/lib/dependabot/nuget/native_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,22 @@ def self.run_nuget_updater_tool(job_path:, repo_root:, proj_path:, dependency:,
end
end

sig { void }
def self.normalize_file_names
# environment variables are required and the following will generate an actionable error message if they're not
_dependabot_repo_contents_path = ENV.fetch("DEPENDABOT_REPO_CONTENTS_PATH")

# this environment variable is directly used
dependabot_home = ENV.fetch("DEPENDABOT_HOME")

command = [
"pwsh",
"#{dependabot_home}/dependabot-updater/bin/normalize-file-names.ps1"
].join(" ")
output = SharedHelpers.run_shell_command(command)
puts output
end

sig { void }
def self.install_dotnet_sdks
return unless Dependabot::Experiments.enabled?(:nuget_install_dotnet_sdks)
Expand Down
2 changes: 2 additions & 0 deletions nuget/spec/dependabot/nuget/file_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def run_fetch_test(files_on_disk:, discovery_content_hash:, &_block)
discovery_json_content = discovery_content_hash.to_json
File.write(discovery_json_path, discovery_json_content)
end
allow(Dependabot::Nuget::NativeHelpers)
.to receive(:normalize_file_names)
# stub call to `fetch_file_from_host` because it expects an empty directory and other things that make the test
# more difficult than it needs to be
allow(file_fetcher_instance)
Expand Down
17 changes: 17 additions & 0 deletions nuget/updater/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,20 @@ function Install-TargetingPacks([string]$sdkInstallDir, [string[]]$targetingPack
Remove-Item -Path $archiveName
}
}

function Repair-FileCasingForName([string]$fileName) {
# Get-ChildItem is case-insensitive
$discoveredFiles = Get-ChildItem $env:DEPENDABOT_REPO_CONTENTS_PATH -r -inc $fileName
foreach ($file in $discoveredFiles) {
# `-cne` = Case-sensitive Not Equal
if ($file.Name -cne $fileName) {
$newName = "$($file.Directory)/$fileName"
Write-Host "Renaming '$file' to '$newName'"
Rename-Item -Path $file -NewName $newName
}
}
}

function Repair-FileCasing() {
Repair-FileCasingForName -fileName "NuGet.Config"
}
1 change: 1 addition & 0 deletions nuget/updater/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function Get-Files {
--api-url $env:DEPENDABOT_API_URL `
--job-id $env:DEPENDABOT_JOB_ID
$script:operationExitCode = $LASTEXITCODE
Repair-FileCasing
}

function Update-Files {
Expand Down
14 changes: 14 additions & 0 deletions nuget/updater/normalize-file-names.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Set-StrictMode -version 2.0
$ErrorActionPreference = "Stop"

. $PSScriptRoot\common.ps1

try {
Repair-FileCasing
}
catch {
Write-Host $_
Write-Host $_.Exception
Write-Host $_.ScriptStackTrace
exit 1
}

0 comments on commit 10e77e2

Please sign in to comment.