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

Do not suppress trim analysis warnings when NativeAOT is enabled #20767

Conversation

simonrozsival
Copy link
Contributor

We noticed we weren't seeing trim analysis warnings in VS Code when PublishAot was set to true. There was a recent change that correctly disabled the suppressions when TrimMode is full. We need to make sure that we're also getting the trim analysis warnings in dotnet build with PublishAot but suppress them when publishing (in that case the warnings will come later from ILC). This PR aligns the behavior of PublishAot=true and TrimMode=true in debug builds.

/cc @ivanpovazan @rolfbjarne @jonathanpeppers

@simonrozsival simonrozsival force-pushed the dev/simonrozsival/do-not-suppress-trim-analysis-warnings-when-nativeaot-is-enabled branch from f220e23 to d44d2c4 Compare June 24, 2024 09:43
@simonrozsival simonrozsival changed the base branch from net9.0 to main June 24, 2024 09:43
@simonrozsival
Copy link
Contributor Author

@rolfbjarne do you think this change (if approved) should go into main or the net9.0 branch?

@vs-mobiletools-engineering-service2

This comment has been minimized.

@simonrozsival
Copy link
Contributor Author

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s), but failed to run 2 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@mandel-macaque
Copy link
Member

/azp run

@mandel-macaque
Copy link
Member

Had to re-trigger, something funny was going on with VSTS.

Copy link

Azure Pipelines successfully started running 2 pipeline(s), but failed to run 1 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@@ -150,13 +150,13 @@
</PropertyGroup>
<PropertyGroup Condition="'$(TrimMode)' == ''">
<!-- Linking is always on for all assemblies when using NativeAOT - this is because we need to modify all assemblies in the linker for them to be compatible with NativeAOT -->
<_DefaultLinkMode Condition="'$(_UseNativeAot)' == 'true'">Full</_DefaultLinkMode>
<_DefaultLinkMode Condition="'$(PublishAot)' == 'true'">Full</_DefaultLinkMode>
Copy link
Member

Choose a reason for hiding this comment

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

This will slow down debug/simulator builds significantly.

How does this work in desktop/console apps? Will setting PublishAot=true enable the trimmer for debug builds as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We wanted to align projects with <PublishAot>true</PublishAot> with projects that have <TrimMode>full</TrimMode>. Now that you pointed this out, I'm realizing that we're telling devs to only enable TrimMode=full in Release builds and not in Debug builds and this change is incorrect.

The goal of this PR is to make sure we don't suppress trimming warnings in Debug builds so I'll make sure to achieve that without changing the trim mode.

/cc @ivanpovazan

Copy link
Contributor

@ivanpovazan ivanpovazan Jun 25, 2024

Choose a reason for hiding this comment

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

How does this work in desktop/console apps? Will setting PublishAot=true enable the trimmer for debug builds as well?

Yes, when we set PublishAot=true debug builds will enable trim analyzers:

ivanpovazan@EONE-MAC-ARM64 ~/tmp/abcd $ dotnet build -c Debug -p:PublishAot=true
Restore complete (0.7s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  abcd succeeded with 1 warning(s) (0.4s) → bin/Debug/net9.0/abcd.dll
    /Users/ivan/tmp/abcd/Program.cs(9,21): warning IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicProperties' in call to 'System.Type.GetProperties()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Build succeeded with 1 warning(s) in 1.3s

Workload updates are available. Run `dotnet workload list` for more information.
ivanpovazan@EONE-MAC-ARM64 ~/tmp/abcd $ rm -rf bin obj                          
ivanpovazan@EONE-MAC-ARM64 ~/tmp/abcd $ dotnet build -c Debug -p:PublishAot=false
Restore complete (0.5s)
You are using a preview version of .NET. See: https://aka.ms/dotnet-support-policy
  abcd succeeded (0.4s) → bin/Debug/net9.0/abcd.dll

Build succeeded in 1.1s

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

In the case of NativeAOT builds, the trim warnings will be produced either by the ILLink roslyn analyzer in Debug builds
or by the ILC when publishing. We want to suppress the ILLink warnings in all build configurations to avoid duplicates.
-->
<SuppressTrimAnalysisWarnings Condition="'$(PublishAot)' == 'true'">true</SuppressTrimAnalysisWarnings>
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be:

Suggested change
<SuppressTrimAnalysisWarnings Condition="'$(PublishAot)' == 'true'">true</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition="'$(PublishAot)' == 'true' And '$(_IsPublishing)' != 'true'">true</SuppressTrimAnalysisWarnings>

As we could have a project file with both '$(TrimMode)' == 'full' and '$(PublishAot)' == 'true'.
If that is the case and we do dotnet build, ILC will not run, so we want warnings from ILLink.
When both PublishAot and _IsPublishing are true aka _UseNativeAot=true we already suppress the warnings in props, so we got that covered.

FWIW, to avoid confusion, I think we should rename _UseNativeAot to something like _PublishWithNativeAot or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing this condition won't make any difference, but it might make it a bit more confusing and harder to follow the values of SuppressTrimAnalysisWarnings. "Why shouldn't this be set to true when publishing?" I don't have a strong preference but I would lean towards keeping the condition simple.

I agree that _UseNativeAot is a bit confusing. It's a combination of PublishAot and _IsPublishing so I suggest _IsPublishingUsingNativeAot or _IsPublishingWithNativeAot.

Copy link
Contributor

@ivanpovazan ivanpovazan Jul 3, 2024

Choose a reason for hiding this comment

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

Changing this condition won't make any difference

Yes, you are right, my suggestion was not correct, and it should include check on TrimMode as well.

With the current state of the condition, users who have their project files configured like:

<TrimMode>full</TrimMode>
<PublishAot>true</PublishAot>

and do dotnet build will not see any warnings:

  • from ILLink, because we suppress warnings here based on just PublishAot
  • from ILC, because they are running build instead of publish

tests/dotnet/UnitTests/ProjectTest.cs Show resolved Hide resolved
Comment on lines 530 to 532
<!-- Suppress trimmer warnings unless we're trimming all assemblies -->
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == '' And '$(TrimMode)' == 'full'">false</SuppressTrimAnalysisWarnings>
<SuppressTrimAnalysisWarnings Condition="'$(SuppressTrimAnalysisWarnings)' == ''">true</SuppressTrimAnalysisWarnings>
Copy link
Member

Choose a reason for hiding this comment

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

Yes, it looks like all the SuppressTrimAnalysisWarnings property setters here are no-ops, because SuppressTrimAnalysisWarnings shouldn't be empty at this point.

Then below, add the _ExtraTrimmerArgs like this:

<_ExtraTrimmerArgs Condition="'$(SuppressTrimAnalysisWarnings)' == 'true' Or '$(_UseNativeAot)' == 'true'">$(_ExtraTrimmerArgs) --notrimwarn</_ExtraTrimmerArgs>

This way we'll:

  • Suppress ILLink's trimmer warnings when using NativeAOT
  • Honor SuppressTrimAnalysisWarnings otherwise.

@rolfbjarne
Copy link
Member

Btw the test now fails with:

Xamarin.Tests.DotNetProjectTest.BuildMyNativeAotAppWithTrimAnalysisWarning(iOS,"iossimulator-arm64"): Warning count
Expected: 1
But was: 2

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@simonrozsival
Copy link
Contributor Author

@rolfbjarne with the changes in the last comment, the new tests are now passing (finally). There are just failing Windows tests but I saw this on other PRs and the error message hints that this is an infra issue and not related to this PR:

error VSX1002: The .NET runtime 8.0.1 or newer could not be found in the remote host under '/Users/builder/Library/Caches/Xamarin/XMA/SDKs/dotnet/dotnet'. Please install it or try setting a different path using the MSBuild property "DotNetRuntimePath".

Comment on lines +530 to +536
<!--
In the case of NativeAOT builds, the trim warnings will be produced either by the ILLink roslyn analyzer in Debug builds
or by the ILC when publishing. We want to suppress the ILLink warnings in all build configurations to avoid duplicates.
We choose not to suppress the ILLink warnings when TrimMode is set to 'full' and we're not publishing. In this scenario
ILC will not run at a later stage of the build.
-->
<SuppressTrimAnalysisWarnings Condition="'$(PublishAot)' == 'true' And '$(_IsPublishing)' != 'true' And '$(TrimMode)' != 'full'">true</SuppressTrimAnalysisWarnings>
Copy link
Contributor

Choose a reason for hiding this comment

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

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [CI Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent
Hash: [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Tests on macOS X64 - Mac Sonoma (14) passed 💻

All tests on macOS X64 - Mac Sonoma (14) passed.

Pipeline on Agent
Hash: 190e0b53394ab28a4884408de42d358b2a9c3c92 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

❌ [CI Build] Windows Integration Tests failed ❌

❌ Failed ❌

Pipeline on Agent
Hash: 190e0b53394ab28a4884408de42d358b2a9c3c92 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Tests on macOS M1 - Mac Ventura (13) passed 💻

All tests on macOS M1 - Mac Ventura (13) passed.

Pipeline on Agent
Hash: 190e0b53394ab28a4884408de42d358b2a9c3c92 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [CI Build] Tests on macOS M1 - Mac Monterey (12) passed 💻

All tests on macOS M1 - Mac Monterey (12) passed.

Pipeline on Agent
Hash: 190e0b53394ab28a4884408de42d358b2a9c3c92 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: test results.

🎉 All 170 tests passed 🎉

Tests counts

✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 6 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 7 tests passed. Html Report (VSDrops) Download
✅ generator: All 2 tests passed. Html Report (VSDrops) Download
✅ install-source: All 1 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 7 tests passed. Html Report (VSDrops) Download
✅ introspection: All 8 tests passed. Html Report (VSDrops) Download
✅ linker: All 65 tests passed. Html Report (VSDrops) Download
✅ mac-binding-project: All 1 tests passed. Html Report (VSDrops) Download
✅ mmp: All 2 tests passed. Html Report (VSDrops) Download
✅ mononative: All 6 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 8 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 9 tests passed. Html Report (VSDrops) Download
✅ monotouch (tvOS): All 11 tests passed. Html Report (VSDrops) Download
✅ monotouch (watchOS): All 4 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ mtouch: All 1 tests passed. Html Report (VSDrops) Download
✅ xammac: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 8 tests passed. Html Report (VSDrops) Download
✅ xtro: All 2 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 190e0b53394ab28a4884408de42d358b2a9c3c92 [PR build]

@rolfbjarne rolfbjarne removed request for mauroa and emaf July 3, 2024 16:21
@rolfbjarne
Copy link
Member

The windows test failure is a known issue.

@rolfbjarne rolfbjarne merged commit 68897de into net9.0 Jul 3, 2024
28 checks passed
@rolfbjarne rolfbjarne deleted the dev/simonrozsival/do-not-suppress-trim-analysis-warnings-when-nativeaot-is-enabled branch July 3, 2024 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants