-
Notifications
You must be signed in to change notification settings - Fork 576
[msbuild] Merge runtimeconfig.dev.json into the runtime configuration. Fixes #26330 #26332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
348dfbe
[msbuild] Merge runtimeconfig.dev.json into the runtime configuration.
rolfbjarne f35ef87
[msbuild] Address review: drop null-forgiving operator, guard parsing…
rolfbjarne eb03d7b
[msbuild] Address review: use Stream overloads for reading/writing json.
rolfbjarne File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
msbuild/Xamarin.MacDev.Tasks/Tasks/MergeRuntimeConfigFiles.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.IO; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
|
|
||
| using Microsoft.Build.Framework; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.MacDev.Tasks { | ||
| // This task merges the 'runtimeOptions.configProperties' from a '*.runtimeconfig.dev.json' file on top of | ||
| // the ones from the main '*.runtimeconfig.json' file, writing the merged result to an output file. | ||
| // | ||
| // We do this because we hand the runtime configuration directly to the runtime (bypassing hostfxr), and | ||
| // hostfxr is what would normally merge the dev file (which contains Debug-only switches such as Hot Reload) | ||
| // into the main runtime configuration. The dev values win, matching hostfxr's behavior. | ||
| public class MergeRuntimeConfigFiles : XamarinTask { | ||
| [Required] | ||
| public string RuntimeConfigFile { get; set; } = ""; | ||
|
|
||
| public string? RuntimeConfigDevFile { get; set; } | ||
|
|
||
| [Required] | ||
| public string OutputFile { get; set; } = ""; | ||
|
|
||
| static readonly JsonDocumentOptions documentOptions = new JsonDocumentOptions { | ||
| AllowTrailingCommas = true, | ||
| CommentHandling = JsonCommentHandling.Skip, | ||
| }; | ||
|
|
||
| public override bool Execute () | ||
| { | ||
| var mainNode = JsonNode.Parse (File.ReadAllText (RuntimeConfigFile), documentOptions: documentOptions); | ||
| if (mainNode is not JsonObject mainObject) { | ||
| Log.LogError (MSBStrings.E7185 /* The runtime configuration file '{0}' is not a valid JSON object. */, RuntimeConfigFile); | ||
| return false; | ||
| } | ||
|
|
||
| if (!string.IsNullOrEmpty (RuntimeConfigDevFile) && File.Exists (RuntimeConfigDevFile)) { | ||
| var devNode = JsonNode.Parse (File.ReadAllText (RuntimeConfigDevFile!), documentOptions: documentOptions); | ||
| var devConfigProperties = (devNode as JsonObject)? ["runtimeOptions"]? ["configProperties"] as JsonObject; | ||
| if (devConfigProperties is not null && devConfigProperties.Count > 0) { | ||
| var runtimeOptions = mainObject ["runtimeOptions"] as JsonObject; | ||
| if (runtimeOptions is null) { | ||
| runtimeOptions = new JsonObject (); | ||
| mainObject ["runtimeOptions"] = runtimeOptions; | ||
| } | ||
|
|
||
| var configProperties = runtimeOptions ["configProperties"] as JsonObject; | ||
| if (configProperties is null) { | ||
| configProperties = new JsonObject (); | ||
| runtimeOptions ["configProperties"] = configProperties; | ||
| } | ||
|
|
||
| foreach (var property in devConfigProperties) { | ||
| configProperties [property.Key] = property.Value is null ? null : JsonNode.Parse (property.Value.ToJsonString ()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var outputDirectory = Path.GetDirectoryName (OutputFile); | ||
| if (!string.IsNullOrEmpty (outputDirectory)) | ||
| Directory.CreateDirectory (outputDirectory!); | ||
|
|
||
| File.WriteAllText (OutputFile, mainObject.ToJsonString (new JsonSerializerOptions { WriteIndented = true })); | ||
|
rolfbjarne marked this conversation as resolved.
Outdated
|
||
|
|
||
| return !Log.HasLoggedErrors; | ||
| } | ||
| } | ||
| } | ||
114 changes: 114 additions & 0 deletions
114
tests/msbuild/Xamarin.MacDev.Tasks.Tests/TaskTests/MergeRuntimeConfigFilesTaskTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.IO; | ||
|
|
||
| using NUnit.Framework; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Xamarin.MacDev.Tasks { | ||
|
|
||
| // Note: we can't use System.Text.Json types here to inspect the output, because the | ||
| // Xamarin.MacDev.Tasks assembly under test is ILMerged and also exposes System.Text.Json, | ||
| // which causes ambiguous type references. We assert on the raw JSON text instead. | ||
| [TestFixture] | ||
| public class MergeRuntimeConfigFilesTaskTest : TestBase { | ||
|
|
||
| string RunMerge (string mainJson, string? devJson) | ||
| { | ||
| var tmp = Cache.CreateTemporaryDirectory (); | ||
| var mainFile = Path.Combine (tmp, "app.runtimeconfig.json"); | ||
| File.WriteAllText (mainFile, mainJson); | ||
|
|
||
| string? devFile = null; | ||
| if (devJson is not null) { | ||
| devFile = Path.Combine (tmp, "app.runtimeconfig.dev.json"); | ||
| File.WriteAllText (devFile, devJson); | ||
| } | ||
|
|
||
| var outputFile = Path.Combine (tmp, "obj", "runtimeconfig.merged.json"); | ||
|
|
||
| var task = CreateTask<MergeRuntimeConfigFiles> (); | ||
| task.RuntimeConfigFile = mainFile; | ||
| task.RuntimeConfigDevFile = devFile; | ||
| task.OutputFile = outputFile; | ||
|
|
||
| ExecuteTask (task); | ||
|
|
||
| Assert.That (outputFile, Does.Exist, "output file created"); | ||
| return File.ReadAllText (outputFile); | ||
| } | ||
|
|
||
| [Test] | ||
| public void MergesDevPropertiesOntoMain () | ||
| { | ||
| var mainJson = @"{ | ||
| ""runtimeOptions"": { | ||
| ""tfm"": ""net10.0"", | ||
| ""configProperties"": { | ||
| ""OnlyInMain"": true, | ||
| ""InBoth"": ""main-value"" | ||
| } | ||
| } | ||
| }"; | ||
| var devJson = @"{ | ||
| ""runtimeOptions"": { | ||
| ""configProperties"": { | ||
| ""OnlyInDev"": ""dev-only"", | ||
| ""InBoth"": ""dev-value"" | ||
| } | ||
| } | ||
| }"; | ||
|
|
||
| var merged = RunMerge (mainJson, devJson); | ||
|
|
||
| // (a) properties only in main are preserved | ||
| Assert.That (merged, Does.Contain ("\"OnlyInMain\": true"), "OnlyInMain preserved"); | ||
| // (b) properties only in dev are added | ||
| Assert.That (merged, Does.Contain ("\"OnlyInDev\": \"dev-only\""), "OnlyInDev added"); | ||
| // (c) properties in both are taken from dev (dev wins) | ||
| Assert.That (merged, Does.Contain ("\"InBoth\": \"dev-value\""), "dev wins for InBoth"); | ||
| Assert.That (merged, Does.Not.Contain ("main-value"), "main value for InBoth is gone"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void NoDevFile () | ||
| { | ||
| var mainJson = @"{ | ||
| ""runtimeOptions"": { | ||
| ""configProperties"": { | ||
| ""OnlyInMain"": true | ||
| } | ||
| } | ||
| }"; | ||
|
|
||
| var merged = RunMerge (mainJson, null); | ||
|
|
||
| Assert.That (merged, Does.Contain ("\"OnlyInMain\": true"), "OnlyInMain preserved"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void DevFileWithoutConfigProperties () | ||
| { | ||
| var mainJson = @"{ | ||
| ""runtimeOptions"": { | ||
| ""configProperties"": { | ||
| ""OnlyInMain"": true | ||
| } | ||
| } | ||
| }"; | ||
| var devJson = @"{ | ||
| ""runtimeOptions"": { | ||
| ""additionalProbingPaths"": [ ""/some/path"" ] | ||
| } | ||
| }"; | ||
|
|
||
| var merged = RunMerge (mainJson, devJson); | ||
|
|
||
| Assert.That (merged, Does.Contain ("\"OnlyInMain\": true"), "OnlyInMain preserved"); | ||
| // The dev file had no configProperties, so nothing from it should have leaked in. | ||
| Assert.That (merged, Does.Not.Contain ("additionalProbingPaths"), "dev-only non-configProperties content is not merged"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.