From 96fad764416360f83fefb791c684a49f9765d412 Mon Sep 17 00:00:00 2001 From: "Brett V. Forsgren" Date: Fri, 21 Feb 2025 14:33:10 -0700 Subject: [PATCH] ensure proper casing for `NuGet.Config` prior to any operations being performed --- nuget/lib/dependabot/nuget/file_fetcher.rb | 1 + nuget/lib/dependabot/nuget/native_helpers.rb | 16 ++++++++++++++++ .../spec/dependabot/nuget/file_fetcher_spec.rb | 2 ++ nuget/updater/common.ps1 | 17 +++++++++++++++++ nuget/updater/main.ps1 | 1 + nuget/updater/normalize-file-names.ps1 | 14 ++++++++++++++ 6 files changed, 51 insertions(+) create mode 100755 nuget/updater/normalize-file-names.ps1 diff --git a/nuget/lib/dependabot/nuget/file_fetcher.rb b/nuget/lib/dependabot/nuget/file_fetcher.rb index 5888dc44c32..8b21ebdce54 100644 --- a/nuget/lib/dependabot/nuget/file_fetcher.rb +++ b/nuget/lib/dependabot/nuget/file_fetcher.rb @@ -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), diff --git a/nuget/lib/dependabot/nuget/native_helpers.rb b/nuget/lib/dependabot/nuget/native_helpers.rb index a59c1399b15..57615697fe8 100644 --- a/nuget/lib/dependabot/nuget/native_helpers.rb +++ b/nuget/lib/dependabot/nuget/native_helpers.rb @@ -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) diff --git a/nuget/spec/dependabot/nuget/file_fetcher_spec.rb b/nuget/spec/dependabot/nuget/file_fetcher_spec.rb index e56c04e589b..ae684eb47ee 100644 --- a/nuget/spec/dependabot/nuget/file_fetcher_spec.rb +++ b/nuget/spec/dependabot/nuget/file_fetcher_spec.rb @@ -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) diff --git a/nuget/updater/common.ps1 b/nuget/updater/common.ps1 index fadc1f0ec80..02f163167a4 100755 --- a/nuget/updater/common.ps1 +++ b/nuget/updater/common.ps1 @@ -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" +} diff --git a/nuget/updater/main.ps1 b/nuget/updater/main.ps1 index 49fd2bc94e7..4cdeeef59b1 100644 --- a/nuget/updater/main.ps1 +++ b/nuget/updater/main.ps1 @@ -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 { diff --git a/nuget/updater/normalize-file-names.ps1 b/nuget/updater/normalize-file-names.ps1 new file mode 100755 index 00000000000..1138e9a3a9f --- /dev/null +++ b/nuget/updater/normalize-file-names.ps1 @@ -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 +}