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
22.24k stars 1.76k forks source link

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

Open jamalbendadi opened 1 year ago

jamalbendadi commented 1 year 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 1 year 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 1 year 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 10 months ago

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

Don09877890 commented 9 months ago

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

cmydur commented 9 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 9 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 8 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 8 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 7 months ago

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

Brosten commented 7 months ago

So do I. Please reopen.

drasticactions commented 7 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 7 months ago

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

spajce commented 7 months ago

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

spajce commented 7 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 7 months ago

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

datvm commented 6 months ago

@drasticactions it's getting closed again :(

aritchie commented 6 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

LiamMorrow commented 4 months ago

Might be potentially helpful, but it is happening in my Hybrid project too when building for ios with a .json file in my Raw Assets.

Here is the run: https://github.com/LiamMorrow/LiftLog/actions/runs/9891533607

Changing from a .json to a .txt did fix it

grumpymonk commented 3 months ago

Also just ran into this building a Blazor ("Hybrid") app for iOS. I had two JSON files in my Resources\Raw folder, and the build failed. Changing the file extension to ".txt" allowed the compilation. It would be nice to have this resolved for the sake of code/architecture maintenance and readability, so that someone doesn't come into a code base to be surprised that a TXT file is actually JSON content.

ngochivinh commented 3 months ago

I got the issue appsettings.json : error : The path '../../../../../../../../../Users/xxxxx/Projects/SalesMauiSource/SalesMauiApp/appsettings.json' would result in a file outside of the app bundle and cannot be used. When build .net maui blazor hybid for IOS on Visual Studio Code in Mac Os. Can you support me resolve it? Thanks you!

image

ngochivinh commented 3 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

Hi @Redth, It can build successfully but can't read value in appsettings.json => The IOS application can't start!

datvm commented 3 months ago

This bug is also preventing me from compiling iOS apps with google-services.json file on Mac.

Marckxp commented 3 months ago

Here is the correct code, Maui uses "Condition" to everything, just put "Remove" over "Include", Working on Mac iOS simulator and Windows Android Simulator, i don't tried out on Mac Android simulator because just need iOS on it.

On IOS has to be 2x with "Content" and "GoogleServicesJson" if you use MauiMTAdmob plugin.

  <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
    <AndroidAsset Remove="Platforms\iOS\GoogleService-Info.plist" />
    <GoogleServicesJson Include="Platforms\Android\google-services.json" />
  <ItemGroup />

  <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
    <Content Remove="Platforms\Android\google-services.json" />
    <GoogleServicesJson Remove="Platforms\Android\google-services.json" />
    <AndroidAsset Include="Platforms\iOS\GoogleService-Info.plist" />
  <ItemGroup />
datvm commented 3 months ago

@Marckxp this doesn't work (just tested). The mere presence of the file would result in the error no matter if your project refers to it or not. See above comments.

ktravelet commented 3 months ago

I'm hitting this too. I wanted to include a json file in the Raw folder but it errors. Changing to a txt fixes the issue.

DaveHogan commented 2 months ago

I'm also encountering this with json files and maui hybrid. Yes, changing the file extension to .txt is a temporary workaround but this is a major pain point.

samhouts commented 2 months ago

@mkArtakMSFT Any thoughts on this one? Thanks!

gautambjain commented 2 months ago

For google-services.json error, adding following lines worked for me. Thanks @Marckxp for showing the direction.

 <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-android'">
    <GoogleServicesJson Include="google-services.json" />
  </ItemGroup>

  <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
    <Content Remove="google-services.json" />
  </ItemGroup>
datvm commented 2 months ago

UPDATE: confirmed that google-services.json is good (i.e. no error happens). My error was due to tsconfig.json, and google-services.json error happen because I put back the file as <None ... />. Sorry I didn't read the error message clearly enough. Still this is an issue with other JSON files.

mkArtakMSFT commented 1 month ago

@javiercn can you please add this to your todo list. Want to make sure we understand what's causing the issue and direct it as necessary. We should try to address this in .NET 10 first, and then patch, depending on the fix.

malsabi commented 4 weeks ago

I'm also having this issue using MAUI Blazer Hybrid when using appsettings.json as an embedded resources. Please fix it.

Marini88 commented 1 week ago

Was there a fix to the appsettings.json bug using Maui Blazor Hybrid? It's November 2024!

marcos-dev8 commented 1 week ago

Hello guys! Here at the company we had a similar problem and we had to solve it with relative paths.

When the iOS build is done, the entire .net debug structure is in the wrong folder.

If you open the bin folder after the iOS build, you will see that the following folders have been created, Debug and iOS.

As iOS is built in Objective C, the multi-threaded behavior structure is not as well accepted as in C# or Java. So, MAUI separates the worlds.

In this separation, this problem occurs.

To resolve this, all iOS assets need to be placed within the Platforms/iOS/Resources folder. This way the build will be able to resolve the paths to the assets.

Image

And maybe, very maybe, you may have the LongPathEnabled problem, if you are using Windows.

Follow the step-by-step instructions on this Autodesk link and magically your project will work!

P.S: Builds will have to be via CLI.