dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.83k stars 1.67k forks source link

Maui Hybrid: build fails when adding json file to ./Resources/Raw #17078

Open jamalbendadi opened 10 months ago

jamalbendadi commented 10 months ago

Description

I am trying to create an appsettings.json file for my maui-blazor application. I'm doing this by adding it to my Resources/Raw folder.

the file is already marked as an asset through this line <MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />

This is happening on a mac and as far as i know only with json files, when i rename it to .txt, the project builds.

This does not happen on a normal maui application, only with maui-hybrid did the problem occur.

Steps to Reproduce

1. Create app with dotnet new maui-hybrid

2. Add a json file to the Resources/Raw folder

3. Build with dotnet build or build with IDE

The error message i am getting is: Resources/Raw/aps.json : error : The path '../../../../../../../../../Users/xxxxx/Desktop/Werk/projecten/App/Resources/Raw/appsettings.json' would result in a file outside of the app bundle and cannot be used. [/Users/xxxxx/Desktop/Werk/projecten/App/App.csproj] Resources/Raw/aps.json : error : [/Users/xxxxx/Desktop/Werk/projecten/App/App.csproj]

Link to public reproduction project repository

No response

Version with bug

8.0.0-preview.7.8842

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

macOS

Affected platform versions

14.0

Did you find any workaround?

Rename it to .txt i guess

Relevant log output

No response

drasticactions commented 10 months ago

https://github.com/jamesmontemagno/dotnet-maui-configuration/blob/master/MauiApp27/MauiProgram.cs#L21-L26

As a workaround, you could move appsettings.json to your project root and make it an embedded resource, then load it from there instead.

I don't think this error is maui blazor specific, you would hit the same issue if you had a json file in that folder in a MAUI template app too (those also support appsettings.json as well)

jamalbendadi commented 10 months ago

On the latest preview version, this works using maui without blazor. Using an embedded resource in my case (maui-blazor) also fails with the same error message.

appsettings.json : error : The path '../../../../../../../../../Users/xxxxx/test/appsettings.json' would result in a file outside of the app bundle and cannot be used. [/Users/jamalbendadi/test/test.csproj::TargetFramework=net8.0-maccatalyst] appsettings.json : error : [/Users/xxxxx/test/test.csproj::TargetFramework=net8.0-maccatalyst]

cmydur commented 5 months ago

Facing the same issue, it works fine on Android but fails for Ios build

Don09877890 commented 5 months ago

I'm also facing this same issue when I have an appsettings.Development.json

cmydur commented 5 months ago

Just rename it to .txt, it will work. It is not ideal, but it will atleast get you going, until the issue is fixed

datvm commented 5 months ago

I am facing the same issue while compiling for net8.0-maccatalyst on Mac only. The files are intentionally set to EmbeddedResource for usage by end products:

    <ItemGroup>
        <Content Remove="Texts\**" />
        <EmbeddedResource Include="Texts\**" />
    </ItemGroup>

But for each file I receive an error:

Texts/common-ar.json : error : The path '../../../../../../../../../<MyFolder>/Texts/common-ar.json' would result in a file outside of the app bundle and cannot be used. [/Users/<MyFolder>.csproj::TargetFramework=net8.0-maccatalyst]
Texts/common-ar.json : error :          [/Users/<MyFolder>.csproj::TargetFramework=net8.0-maccatalyst]

Also this is happening for a MAUI library project. On Windows the project compiles successfully.

jaosnz-rep commented 4 months ago

Verified this issue on .NET 8 MAUI and MAUI Blazor app with Visual Studio for Mac 17.6.9 (build 415), not repro on maccatalyst platform and can be built successfully. image

ghost commented 4 months ago

Hi @jamalbendadi. We have added the "s/try-latest-version" label to this issue, which indicates that we'd like you to try and reproduce this issue on the latest available public version. This can happen because we think that this issue was fixed in a version that has just been released, or the information provided by you indicates that you might be working with an older version.

You can install the latest version by installing the latest Visual Studio (Preview) with the .NET MAUI workload installed. If the issue still persists, please let us know with any additional details and ideally a reproduction project provided through a GitHub repository.

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

dhindrik commented 3 months ago

I have the same problem with the latest version of Maui and Visual Studio.

Brosten commented 3 months ago

So do I. Please reopen.

drasticactions commented 3 months ago

This is still happening with ios and Mac Catalyst, specifically with MAUI Blazor projects. I think it could be due to the "Microsoft.NET.Sdk.Razor" SDK doing weird stuff. Clearly, people are having issues with this, so I'm reopening it.

datvm commented 2 months ago

@drasticactions is this a faulty bot action? Why is this closed?

spajce commented 2 months ago

is there any workaround on this? I am on MAUI Blazor projects, WinUI and android

spajce commented 2 months ago

@drasticactions is this a faulty bot action? Why is this closed?

I think I found the workaround, Embedded resource the json file and register it with the help of package Microsoft.Extensions.Options.ConfigurationExtensions. tested on Android and WinUI

            // Load appsettings.json
            var a = Assembly.GetExecutingAssembly();
            using var stream = a.GetManifestResourceStream("MauiAppWithMudBlazor.appsettings.json");

            var config = new ConfigurationBuilder()
                .AddJsonStream(stream)
                .Build();

            builder.Configuration.AddConfiguration(config);

            builder.Services.Configure<AppSettings>(config.GetSection("AppSettings"))
                 .AddSingleton(s => s.GetRequiredService<IOptions<AppSettings>>().Value)
                 .AddSingleton<IAppSettings>(s => s.GetRequiredService<IOptions<AppSettings>>().Value);

https://github.com/spajce/.NET-MAUI-Blazor-Hybrid-app-with-MudBlazor

datvm commented 2 months ago

@spajce I am not sure how that helps. My files are not configuration files and they are already EmbededResources.

datvm commented 2 months ago

@drasticactions it's getting closed again :(

aritchie commented 2 months ago

Add the following to your csproj to workaround this issue for now:

<ItemGroup>
    <Content Remove="**\appsettings.json" />
    <Content Remove="**\appsettings.*.json" />
</ItemGroup>

The razor SDK seems to eat everything it can