dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.42k stars 4.76k forks source link

Error: AOT Support throws the error "PublishTrimmed is implied by native compilation and cannot be disabled". #109452

Open sudharsan-narayanan opened 3 weeks ago

sudharsan-narayanan commented 3 weeks ago

Description

Once created the WinUI project in Visual Studio, Just upgrade the SDK version. Added the PublishAOT tag as true. Tried to publish the exe through the below command,

dotnet publish -c release -r win-x64

but it throws the error in output window

C:\Users\.nuget\packages\microsoft.dotnet.ilcompiler\8.0.8\build\Microsoft.NETCore.Native.Publish.targets(55,5): Error : PublishTrimmed is implied by native compilation and cannot be disabled.

Reproduction Steps

NA

Expected behavior

Once added the PublishAOT tag in project, the project should works as expected in AOT.

Actual behavior

Throws error while running the project

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

huoyaoyuan commented 3 weeks ago

What's your .NET SDK and WinUI version?

Sergio0694 commented 3 weeks ago

Also did you check you don't have PublishTrimmed hardcoded to false in your .pubxml files?

MichalStrehovsky commented 3 weeks ago

This can only be reached if something sets PublishTrimmed to false. The default value is no value and we then set it to true in ILCompiler targets.

It is likely set to false somewhere in your code. If you can't find it, can you produce a binlog and attach it here? Pass -bl to the publish command line and attach the msbuild.binlog file.

sudharsan-narayanan commented 3 weeks ago

Hi All,

I have ensured and not used the PublishTrimmed as false in my simple sample. But it throws the error in sample.

Using the WinUI 3 and latest version windows SDK.

Please find my simple sample below,

AOTSample.zip

Can you please check this sample?

MichalStrehovsky commented 2 weeks ago

Can you please check this sample?

It doesn't repro with this sample from either dotnet publish or VS publish. Do you have a binlog?

sudharsan-narayanan commented 2 weeks ago

It doesn't repro with this sample from either dotnet publish or VS publish.

Can you please share the command used to create the exe? or give me the guidelines to ensure AOT support in right way

Do you have a binlog?

yes, I have exe file, but it doesn't launch.

MichalStrehovsky commented 2 weeks ago

Can you please share the command used to create the exe? or give me the guidelines to ensure AOT support in right way

I just typed dotnet publish after unzipping your repro file. There's nothing else to it. Same in Visual Studio, although the publish profiles in the repro ZIP file looked non-sensical (they referred to RIDs that don't exist and the SDK errors out with a message they say the RID doesn't exist - I had to change it to e.g. win-x64).

yes, I have exe file, but it doesn't launch.

I didn't try launching anything either, this issue is about a compile-time error message.

sudharsan-narayanan commented 1 week ago

Hi Team,

Thanks for the help and suggestion.

For using AOT support in WinUI 3 application, is there any restriction to avoid issues or warnings for launching the application smoothly?

Currently, facing issue while launching the package.

Without any controls -- > Launch properly. With few controls -- > Package created through project with AOT support() was not launching properly.

Any suggestion or restriction should follow?

jkotas commented 1 week ago

Any suggestion or restriction should follow?

The most common problem are trim and AOT compatibility issues in the code. You can read more about it at https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/fixing-warnings .

Do you see any warnings from the AOT compiler during the application build?

sudharsan-narayanan commented 6 days ago

Any suggestion or restriction should follow?

The most common problem are trim and AOT compatibility issues in the code. You can read more about it at https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/fixing-warnings .

Do you see any warnings from the AOT compiler during the application build?

Not during the application build. I can face while launching the application through package.

App Unhandled Exception - System.NotSupportedException: COM Interop requires ComWrapper instance registered for marshalling. at System.Runtime.InteropServices.ComWrappers.ComObjectForInterface(IntPtr) + 0x76 at WinRT.IObjectReference.AsInterface[TInterface]() + 0x113 at WinRT.CastExtensions.AsTInterface + 0xce

MichalStrehovsky commented 6 days ago

Are you using WinAppSDK 1.6 or later version?

@Sergio0694 does this failure mode sound familiar to you?

Sergio0694 commented 6 days ago

I assume they're hitting this path. Unfortunately that doesn't warn, because "[ComImport] is technically AOT compatible if you setup the right ComWrappers instance", so that's why the code is only crashing at runtime. @sudharsan-narayanan make sure to replace all [ComImport] interfaces in your app with [GeneratedComInterface] instead to make sure things work correctly. Alternatively, you should in theory be able to configure ComWrappers to work correctly there on Native AOT, but I haven't tried that myself and I'm not familiar with that configuration.

cc. @manodasanW thoughts?

manodasanW commented 5 days ago

I would recommend replacing the uses of ComImport attribute in your scenario with GeneratedComInterface attribute and its source generated support. That should get you past this issue.