Skip to content
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

Fix broken tests when running Config Schema Generator on .NET 9 #6312

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bart-vmware
Copy link
Contributor

@bart-vmware bart-vmware commented Oct 15, 2024

Description

Adapt the Config Schema Generator logic to deal with the breaking change in System.Text.Json, reported at dotnet/runtime#108790.

More specifically, this PR fixes the JsonNode handling inside Config Schema Generator to work with .NET 9 RC2. The generator project and its test project now multi-target .NET 8 and 9. However, the consuming projects still execute the .NET 8 version during build.

Before this PR, the generator and its tests only ran on .NET 8, which is why there were no test failures.

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?
    • Yes
      • Link to aspire-docs issue:
    • No

/cc @eerhardt

Microsoft Reviewers: Open in CodeFlow

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Oct 15, 2024
Comment on lines +28 to +32
<ItemGroup Condition="'$(TargetFramework)' == 'net9.0'">
<PackageReference Include="Microsoft.Extensions.Hosting" VersionOverride="9.0.0-rc.2.24473.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" VersionOverride="9.0.0-rc.2.24473.5" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" VersionOverride="9.0.0-rc.2.24473.5" />
</ItemGroup>
Copy link
Contributor Author

@bart-vmware bart-vmware Oct 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a better way. I've tried updating versions.props but that breaks other things, so I've made the changes local. This is needed because the listed packages reference 8.0.0 versions otherwise.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed now. You should be able to revert these changes because of the following:

<PropertyGroup Condition="'$(TargetFramework)' == 'net9.0'">
<!-- Runtime -->
<MicrosoftExtensionsHostingAbstractionsPackageVersion>9.0.0-rc.2.24473.5</MicrosoftExtensionsHostingAbstractionsPackageVersion>
<MicrosoftExtensionsHostingPackageVersion>9.0.0-rc.2.24473.5</MicrosoftExtensionsHostingPackageVersion>

This will automatically reference the 9.0 hosting packages when targeting net9.0.

@joperezr
Copy link
Member

Hello @bart-vmware, thanks for the contribution. This PR is currently set to target release/9.0-rc.1 which is no longer an active branch. Assuming you are okay with me re-targeting it against main ?

@bart-vmware
Copy link
Contributor Author

@joperezr It probably won't work with main as-is, due to different TFMs and version numbers.

Can I update this PR to make it compatible with release/9.0 and target that? If it doesn't meet the bar (no worries), I'd rather wait until the final 9.0 release has shipped, and then update and retarget this PR.

@joperezr
Copy link
Member

We are pretty locked down at this point for release/9.0 branch, so unfortunately wouldn't be able to take this into there. main is already a net9 TFM too, in fact main is our 9.1 branch now, so that would be the best place to target for your PR.

@joperezr
Copy link
Member

Given the above, I'm retargeting this to go against main as the rc.1 branch is frozen anyway.

@@ -78,7 +78,7 @@
<Target Name="CalculateConfigurationSchemaGeneratorPath">
<MSBuild Projects="$(ConfigurationSchemaGeneratorProjectPath)"
Targets="GetTargetPath"
RemoveProperties="TargetFramework">
Properties="$(Properties);TargetFramework=$(DefaultTargetFramework)">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is right. (actually I'm pretty sure it is wrong). Can you explain why you are proposing this change? What doesn't work about the current code?

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>$(DefaultTargetFramework)</TargetFramework>
<TargetFrameworks>$(AllTargetFrameworks)</TargetFrameworks>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need to multi-target the generator. It should only need to target net8.0.

Comment on lines +718 to +720
private static void ReplaceNodeWithKeyCasingChange(JsonObject jsonObject, string key, JsonNode value)
{
#if NET9_0_OR_GREATER
Copy link
Member

@eerhardt eerhardt Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eiriktsarpalis - is this the recommended way to respond to the breaking change? Do we have documentation guidance on the break?

I almost wonder if we should be checking the System.Text.Json major version number instead. Or doing some sort of "runtime capability test" - for example, execute a method that checks for the new behavior and set a bool flag.

The situation that can break this is if you are targeting net8.0, but somehow the 9.0 System.Text.Json nuget package gets pulled into the app (ex. through a transitive reference). In that situation, the #else code will be compiled into the app, but the 9.0 STJ behavior will happen.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I fully understand what tests were broken by the particular change in STJ 9, but per the discussion in dotnet/runtime#108790 (comment) the pre-9 behaviour is undocumented, accidental behaviour that is inconsistent with how all other dictionary types work in the same context.

We've agreed to add a breaking change note in our documentation for completeness, but beyond that I don't think it's appropriate to introduce mitigations down the stack just so that the pre-9 behavior is preserved. Assuming there are failing tests that check against that behavior, then I would recommend just updating the tests.

@eerhardt
Copy link
Member

eerhardt commented Nov 8, 2024

(Sorry for the late response. We were busy locking down for .NET Aspire 9.0, which will be released next week.)

@@ -61,7 +61,7 @@ private void GenerateLogCategories(JsonObject parent)
{
var categoryNode = new JsonObject();
categoryNode["$ref"] = "#/definitions/logLevelThreshold";
propertiesNode[categories[i]] = categoryNode;
ReplaceNodeWithKeyCasingChange(propertiesNode, categories[i], categoryNode);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that this is code copied from the config binder generator, is it appropriate to be making changes locally?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of the code isn't copied from the config binder generator. The part that is copied is inside the https://github.com/dotnet/aspire/tree/main/src/Tools/ConfigurationSchemaGenerator/RuntimeSource directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants