diff --git a/eng/common/SetupNugetSources.sh b/eng/common/SetupNugetSources.sh index dd2564aef01..b97cc536379 100755 --- a/eng/common/SetupNugetSources.sh +++ b/eng/common/SetupNugetSources.sh @@ -66,10 +66,8 @@ EnableInternalPackageSource() { grep -i " /dev/null if [ "$?" == "0" ]; then echo "Enabling internal source '$PackageSourceName'." - # Remove the disabled entry - local OldDisableValue="" - local NewDisableValue="" - sed -i.bak "s|$OldDisableValue|$NewDisableValue|" "$ConfigFile" + # Remove the disabled entry (including any surrounding comments or whitespace on the same line) + sed -i.bak "//d" "$ConfigFile" # Add the source name to PackageSources for credential handling PackageSources+=("$PackageSourceName") diff --git a/src/Microsoft.DotNet.SetupNugetSources.Tests/FeedEnablingTests.cs b/src/Microsoft.DotNet.SetupNugetSources.Tests/FeedEnablingTests.cs index 7af694a1db8..d38cab4887b 100644 --- a/src/Microsoft.DotNet.SetupNugetSources.Tests/FeedEnablingTests.cs +++ b/src/Microsoft.DotNet.SetupNugetSources.Tests/FeedEnablingTests.cs @@ -175,5 +175,40 @@ public async Task ConfigWithNoDisabledSources_StillAddsInternalFeeds() "https://pkgs.dev.azure.com/dnceng/internal/_packaging/dotnet6-internal-transport/nuget/v3/index.json", "should add dotnet6-internal-transport feed"); } + + [Fact] + public async Task ConfigWithCommentedOutDisabledDarcIntFeeds_RemovesEntriesAndProducesValidXml() + { + // Arrange - this test covers the issue where commented-out disabled entries would create invalid XML + var originalConfig = @" + + + + + + + + +"; + var configPath = Path.Combine(_testOutputDirectory, "nuget.config"); + await Task.Run(() => File.WriteAllText(configPath, originalConfig)); + + // Act + var result = await _scriptRunner.RunScript(configPath); + + // Assert + result.exitCode.Should().Be(0, "Script should succeed, but got error: {result.error}"); + var modifiedConfig = await Task.Run(() => File.ReadAllText(configPath)); + + // The modified config should be valid XML (this would fail if nested comments were created) + Action parseXml = () => System.Xml.Linq.XDocument.Parse(modifiedConfig); + parseXml.Should().NotThrow("modified config should be valid XML without nested comments"); + + // The darc-int feed should not be disabled + modifiedConfig.ShouldNotBeDisabled("darc-int-dotnet-roslyn-12345", "darc-int feed should be enabled"); + + // The commented-out line should be removed entirely (no comment remnants) + modifiedConfig.Should().NotContain("Reenabled for build", "should not add comments when removing disabled entries"); + } } }