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

PublishTrimmed/TrimMode support? #44

Open
Lawls91 opened this issue May 7, 2024 · 17 comments
Open

PublishTrimmed/TrimMode support? #44

Lawls91 opened this issue May 7, 2024 · 17 comments

Comments

@Lawls91
Copy link

Lawls91 commented May 7, 2024

Hey,

I am trying to use the library, which works fine untrimmed. Unfortunately we are having issues with TrimmedMode with .NET7/8, in which the app crashes at launch.

I have managed to resolve this by using TrimMode partial as suggested here microsoft/WindowsAppSDK#2478 (comment) which has the app running but none of the strings are being loaded in. Its tricky to debug as its occuring after a publish.

Does WinUI3Localizer support trimmed apps / partially trimmed apps with .NET 7/8?

@AndrewKeepCoding
Copy link
Owner

Hi! I'm away from my laptop for a few days. I've never tried it with trimming. I'll take a look when I'm back. ;)

@AndrewKeepCoding
Copy link
Owner

Can you create and share a reproducible project on GitHub?

@Lawls91
Copy link
Author

Lawls91 commented May 20, 2024

Hey sorry I've not got around to this yet, I will try and get something for you this week

@Lawls91
Copy link
Author

Lawls91 commented Jul 17, 2024

Here is an attached reproducible project, sorry for the delay!

LocalizerTest.zip

You should be able to run this sln, F5 to run locally which will load the nav bar content and the title bar.

Then in Developer Powershell in VisualStudio, run

dotnet publish .\LocalizerTestApp\LocalizerTestApp.csproj /p:PublishProfile=.\Properties/PublishProfiles/win-x64.pubxml

and run the exe from that directory. Result is none of the strings get loaded. You can see some trim warnings coming out of the publish step also. Turning off partial trim causes the app to crash instead of just not loading.

Let me know if I can provide anything else to assist

@Lawls91
Copy link
Author

Lawls91 commented Aug 7, 2024

@AndrewKeepCoding, is there anything else I can provide to help you out with this? Is the test project OK?

@AndrewKeepCoding
Copy link
Owner

Hi @Lawls91!
I'm sorry for the late response. I must have missed this notification.

I tried to reproduce the issue but as you can see, the strings are loaded as expected.

image

Can you make sure that the Strings folder and its Resources.resw files exist aside the EXE?

@Lawls91
Copy link
Author

Lawls91 commented Aug 8, 2024

Hey @AndrewKeepCoding , no problem! I cant claim to understand the trim process fully, but I think that the assemblies in the /bin/Release folder are created before the trim?

Please after running the above dotnet publish step run the exe from the trimmed output directory. Should be:

SOLUTION_DIRECTORY/x64/LocalizerTestApp/LocalizerTestApp.exe

From there, when you run it, the strings dont display. The Strings directory is alongside it also

Let me know if you are able to see this behaviour also

@AndrewKeepCoding
Copy link
Owner

It works. Can you add a logger from the builder?
Or add the following in the HomePage so we can see inside the localizer?

<Page
    x:Class="LocalizerTestApp.Pages.HomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="88"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Text="Home" FontSize="28" />
        <StackPanel Grid.Row="1">
            <TextBlock x:Name="StringsFolderPath"/>
            <TextBlock x:Name="CurrentLanguage"/>
            <ListView x:Name="AvailableLanguages"/>
            <ListView x:Name="LanguageDictionaries"/>
        </StackPanel>
    </Grid>
</Page>
public HomePage()
{
    InitializeComponent();
    Loaded += HomePage_Loaded;
}

private void HomePage_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    StringsFolderPath.Text = Path.Combine(AppContext.BaseDirectory, "Strings");
    CurrentLanguage.Text = Localizer.Get().GetCurrentLanguage();
    AvailableLanguages.ItemsSource = Localizer.Get().GetAvailableLanguages();
    LanguageDictionaries.ItemsSource = Localizer.Get().GetLanguageDictionaries().FirstOrDefault().GetItems().Select(item => item.Value);
}

@Lawls91
Copy link
Author

Lawls91 commented Aug 12, 2024

@AndrewKeepCoding accessing by code works (the "APP TITLE BAR" works for example and thats setup in the constructor of MainWindow.xaml.cs). It appears to just be strings that are setup through the XAML directly that dont work. I added that extra stuff for you, you can see it is outputting, but the strings for the nav bar still arent (when using the trimmed published exe, as mentioned the exe inside the project folder works, its the one off the solutions directory that doesnt)

image

I've attached a prebuilt copy, if you are launching it from SOLUTION_DIRECTORY/x64/LocalizerTestApp/LocalizerTestApp.exe already, then maybe its related to the compiler? Please can you run this and check behaviour? (Sorry, had to double compress it with 7z for github)

LocalizerTestApp.7z.gz

@Lawls91
Copy link
Author

Lawls91 commented Aug 19, 2024

Hey @AndrewKeepCoding just wondering if you missed my latest reply?

@AndrewKeepCoding
Copy link
Owner

In a couple of days, I'll have access to a pc for testing. The thing is that I just can't run files on the laptop I use for work. 😅

@Lawls91
Copy link
Author

Lawls91 commented Aug 19, 2024

Makes sense, no problem, thanks for the reply!

Let me know if theres anything I can provide to help. I've tested on a couple other machines with some of our other devs and get the same behaviour, whereby the output in SOLUTION_DIRECTORY/x64 doesnt work, but it does in SOLUTION_DIRECTORY/PROJECT_DIRECTORY/bin/Release (assume thats not Trimmed yet in there)

@AndrewKeepCoding
Copy link
Owner

@Lawls91 I finally managed to reproduce the issue. Even though the localizer has the resources loaded as expected, it’s failing to get the properties from the controls. It seems that this is because ‘Reflection’ doesn’t work well with trimming. See here. I'm not sure if we'll have a better way to handle this in the future or not.

I'm sorry I couldn't bring better news after all.

@Lawls91
Copy link
Author

Lawls91 commented Aug 23, 2024

@AndrewKeepCoding Is it worth investigating opting the localizer modules out of trimming? Reading this, it seems like partial trimming should work with that package just set to not trim (I've not really dug into that though)

https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-8-0#trimming-granularity

@AndrewKeepCoding
Copy link
Owner

I tried it but still didn't work...😵

@AndrewKeepCoding
Copy link
Owner

@Lawls91 Can you try trimming with the WinUI3Localizer sample app? It seems to be working.

@AndrewKeepCoding
Copy link
Owner

Hi @Lawls91 !
The WinUI3Localizer definitely has issues with trimming.

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

No branches or pull requests

2 participants