Skip to content

Commit 872d3d0

Browse files
Copilotmmitche
andauthored
Fix SetupNugetSources.sh to avoid creating invalid XML comments when re-enabling disabled feeds (#16207)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: mmitche <[email protected]>
1 parent dddb978 commit 872d3d0

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

eng/common/SetupNugetSources.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,8 @@ EnableInternalPackageSource() {
6666
grep -i "<add key=\"$PackageSourceName\" value=\"true\"" "$ConfigFile" > /dev/null
6767
if [ "$?" == "0" ]; then
6868
echo "Enabling internal source '$PackageSourceName'."
69-
# Remove the disabled entry
70-
local OldDisableValue="<add key=\"$PackageSourceName\" value=\"true\" />"
71-
local NewDisableValue="<!-- Reenabled for build : $PackageSourceName -->"
72-
sed -i.bak "s|$OldDisableValue|$NewDisableValue|" "$ConfigFile"
69+
# Remove the disabled entry (including any surrounding comments or whitespace on the same line)
70+
sed -i.bak "/<add key=\"$PackageSourceName\" value=\"true\" \/>/d" "$ConfigFile"
7371

7472
# Add the source name to PackageSources for credential handling
7573
PackageSources+=("$PackageSourceName")

src/Microsoft.DotNet.SetupNugetSources.Tests/FeedEnablingTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,40 @@ public async Task ConfigWithNoDisabledSources_StillAddsInternalFeeds()
175175
"https://pkgs.dev.azure.com/dnceng/internal/_packaging/dotnet6-internal-transport/nuget/v3/index.json",
176176
"should add dotnet6-internal-transport feed");
177177
}
178+
179+
[Fact]
180+
public async Task ConfigWithCommentedOutDisabledDarcIntFeeds_RemovesEntriesAndProducesValidXml()
181+
{
182+
// Arrange - this test covers the issue where commented-out disabled entries would create invalid XML
183+
var originalConfig = @"<?xml version=""1.0"" encoding=""utf-8""?>
184+
<configuration>
185+
<packageSources>
186+
<add key=""dotnet-public"" value=""https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json"" />
187+
<add key=""darc-int-dotnet-roslyn-12345"" value=""https://pkgs.dev.azure.com/dnceng/internal/_packaging/darc-int-dotnet-roslyn-12345/nuget/v3/index.json"" />
188+
</packageSources>
189+
<disabledPackageSources>
190+
<!-- <add key=""darc-int-dotnet-roslyn-12345"" value=""true"" /> -->
191+
</disabledPackageSources>
192+
</configuration>";
193+
var configPath = Path.Combine(_testOutputDirectory, "nuget.config");
194+
await Task.Run(() => File.WriteAllText(configPath, originalConfig));
195+
196+
// Act
197+
var result = await _scriptRunner.RunScript(configPath);
198+
199+
// Assert
200+
result.exitCode.Should().Be(0, "Script should succeed, but got error: {result.error}");
201+
var modifiedConfig = await Task.Run(() => File.ReadAllText(configPath));
202+
203+
// The modified config should be valid XML (this would fail if nested comments were created)
204+
Action parseXml = () => System.Xml.Linq.XDocument.Parse(modifiedConfig);
205+
parseXml.Should().NotThrow("modified config should be valid XML without nested comments");
206+
207+
// The darc-int feed should not be disabled
208+
modifiedConfig.ShouldNotBeDisabled("darc-int-dotnet-roslyn-12345", "darc-int feed should be enabled");
209+
210+
// The commented-out line should be removed entirely (no comment remnants)
211+
modifiedConfig.Should().NotContain("Reenabled for build", "should not add comments when removing disabled entries");
212+
}
178213
}
179214
}

0 commit comments

Comments
 (0)