diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs
index e7804d091b..aa73a9a039 100644
--- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs
+++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.DotNetToolsJson.cs
@@ -50,6 +50,50 @@ await TestDiscoveryAsync(
);
}
+ [Fact]
+ public async Task DiscoversDependenciesTrailingComma()
+ {
+ await TestDiscoveryAsync(
+ packages: [],
+ workspacePath: "",
+ files: [
+ (".config/dotnet-tools.json", """
+ {
+ "version": 1,
+ "isRoot": true,
+ "tools": {
+ "botsay": {
+ "version": "1.0.0",
+ "commands": [
+ "botsay"
+ ],
+ },
+ "dotnetsay": {
+ "version": "1.0.0",
+ "commands": [
+ "dotnetsay"
+ ],
+ }
+ }
+ }
+ """),
+ ],
+ expectedResult: new()
+ {
+ Path = "",
+ DotNetToolsJson = new()
+ {
+ FilePath = ".config/dotnet-tools.json",
+ Dependencies = [
+ new("botsay", "1.0.0", DependencyType.DotNetTool),
+ new("dotnetsay", "1.0.0", DependencyType.DotNetTool),
+ ]
+ },
+ ExpectedProjectCount = 0,
+ }
+ );
+ }
+
[Fact]
public async Task ReportsFailure()
{
@@ -74,7 +118,7 @@ await TestDiscoveryAsync(
"dotnetsay"
]
}
- }
+ } INVALID JSON
}
"""),
],
diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs
index 35c252d294..ed2ab846f4 100644
--- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs
+++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Discover/DiscoveryWorkerTests.GlobalJson.cs
@@ -40,6 +40,40 @@ await TestDiscoveryAsync(
);
}
+ [Fact]
+ public async Task DiscoversDependencies_HandlesTrailingComma()
+ {
+ await TestDiscoveryAsync(
+ packages: [],
+ workspacePath: "",
+ files: [
+ ("global.json", """
+ {
+ "sdk": {
+ "version": "2.2.104"
+ },
+ "msbuild-sdks": {
+ "Microsoft.Build.Traversal": "1.0.45"
+ },
+ }
+ """),
+ ],
+ expectedResult: new()
+ {
+ Path = "",
+ GlobalJson = new()
+ {
+ FilePath = "global.json",
+ Dependencies = [
+ new("Microsoft.NET.Sdk", "2.2.104", DependencyType.MSBuildSdk),
+ new("Microsoft.Build.Traversal", "1.0.45", DependencyType.MSBuildSdk),
+ ]
+ },
+ ExpectedProjectCount = 0,
+ }
+ );
+ }
+
[Fact]
public async Task ReportsFailure()
{
@@ -50,7 +84,7 @@ await TestDiscoveryAsync(
("global.json", """
{
"sdk": {
- "version": "2.2.104",
+ "version": "2.2.104", INVALID JSON
},
"msbuild-sdks": {
"Microsoft.Build.Traversal": "1.0.45"
diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs
index 09c4cb1aa5..185f4db4f9 100644
--- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs
+++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.DotNetTools.cs
@@ -287,5 +287,89 @@ await TestUpdateForProject("Some.DotNet.Tool", "1.0.0", "1.1.0",
]
);
}
+
+ [Fact]
+ public async Task UpdateSingleDependencyWithTrailingComma()
+ {
+ await TestUpdateForProject("Some.DotNet.Tool", "1.0.0", "1.1.0",
+ packages:
+ [
+ MockNuGetPackage.CreateSimplePackage("Some.Package", "13.0.3", "net8.0"),
+ MockNuGetPackage.CreateDotNetToolPackage("Some.DotNet.Tool", "1.0.0", "net8.0"),
+ MockNuGetPackage.CreateDotNetToolPackage("Some.DotNet.Tool", "1.1.0", "net8.0"),
+ ],
+ // initial
+ projectContents: """
+
+
+ net8.0
+
+
+
+
+
+
+ """,
+ additionalFiles:
+ [
+ (".config/dotnet-tools.json", """
+ {
+ "version": 1,
+ "isRoot": true,
+ "tools": {
+ "some.dotnet.tool": {
+ "version": "1.0.0",
+ "commands": [
+ "some.dotnet.tool"
+ ],
+ },
+ "some-other-tool": {
+ "version": "2.1.3",
+ "commands": [
+ "some-other-tool"
+ ],
+ }
+ }
+ }
+ """)
+ ],
+ // expected
+ expectedProjectContents: """
+
+
+ net8.0
+
+
+
+
+
+
+ """,
+ // expected files no longer have trailing commas in the json
+ additionalFilesExpected:
+ [
+ (".config/dotnet-tools.json", """
+ {
+ "version": 1,
+ "isRoot": true,
+ "tools": {
+ "some.dotnet.tool": {
+ "version": "1.1.0",
+ "commands": [
+ "some.dotnet.tool"
+ ]
+ },
+ "some-other-tool": {
+ "version": "2.1.3",
+ "commands": [
+ "some-other-tool"
+ ]
+ }
+ }
+ }
+ """)
+ ]
+ );
+ }
}
}
diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs
index 8e00119fc6..9537f35fb6 100644
--- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs
+++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core.Test/Update/UpdateWorkerTests.GlobalJson.cs
@@ -226,5 +226,71 @@ await TestUpdateForProject("Some.MSBuild.Sdk", "3.2.0", "4.1.0",
]
);
}
+
+ [Fact]
+ public async Task UpdateDependencyWithTrailingComma()
+ {
+ await TestUpdateForProject("Some.MSBuild.Sdk", "3.2.0", "4.1.0",
+ packages:
+ [
+ MockNuGetPackage.CreateSimplePackage("Some.Package", "13.0.3", "net8.0"),
+ MockNuGetPackage.CreateMSBuildSdkPackage("Some.MSBuild.Sdk", "3.2.0"),
+ MockNuGetPackage.CreateMSBuildSdkPackage("Some.MSBuild.Sdk", "4.1.0"),
+ ],
+ // initial
+ projectContents: """
+
+
+ net8.0
+
+
+
+
+
+
+ """,
+ additionalFiles:
+ [
+ ("global.json", """
+ {
+ "sdk": {
+ "version": "6.0.405",
+ "rollForward": "latestPatch"
+ },
+ "msbuild-sdks": {
+ "Some.MSBuild.Sdk": "3.2.0"
+ },
+ }
+ """)
+ ],
+ // expected
+ expectedProjectContents: """
+
+
+ net8.0
+
+
+
+
+
+
+ """,
+ // expected file no longer has the trailing comma because the parser removes it.
+ additionalFilesExpected:
+ [
+ ("global.json", """
+ {
+ "sdk": {
+ "version": "6.0.405",
+ "rollForward": "latestPatch"
+ },
+ "msbuild-sdks": {
+ "Some.MSBuild.Sdk": "4.1.0"
+ }
+ }
+ """)
+ ]
+ );
+ }
}
}
diff --git a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs
index ec1f10aef8..5ac73cff53 100644
--- a/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs
+++ b/nuget/helpers/lib/NuGetUpdater/NuGetUpdater.Core/Utilities/JsonHelper.cs
@@ -11,6 +11,7 @@ internal static class JsonHelper
public static JsonDocumentOptions DocumentOptions { get; } = new JsonDocumentOptions
{
CommentHandling = JsonCommentHandling.Skip,
+ AllowTrailingCommas = true,
};
public static JsonNode? ParseNode(string content)
@@ -24,6 +25,7 @@ public static string UpdateJsonProperty(string json, string[] propertyPath, stri
var readerOptions = new JsonReaderOptions
{
CommentHandling = JsonCommentHandling.Allow,
+ AllowTrailingCommas = true,
};
var bytes = Encoding.UTF8.GetBytes(json);
var reader = new Utf8JsonReader(bytes, readerOptions);