maui icon indicating copy to clipboard operation
maui copied to clipboard

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

Open jamalbendadi opened this issue 1 year ago • 15 comments

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

  • if possible change the target framework to <TargetFramework>net8.0-maccatalyst</TargetFramework>

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

jamalbendadi avatar Aug 29 '23 19:08 jamalbendadi

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)

drasticactions avatar Aug 30 '23 06:08 drasticactions

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]

jamalbendadi avatar Aug 30 '23 07:08 jamalbendadi

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

cmydur avatar Jan 09 '24 18:01 cmydur

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

Don09877890 avatar Jan 24 '24 19:01 Don09877890

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

cmydur avatar Jan 24 '24 19:01 cmydur

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.

datvm avatar Feb 05 '24 13:02 datvm

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

jaosnz-rep avatar Feb 21 '24 09:02 jaosnz-rep

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.

ghost avatar Feb 21 '24 09:02 ghost

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

dhindrik avatar Mar 21 '24 14:03 dhindrik

So do I. Please reopen.

Brosten avatar Mar 25 '24 07:03 Brosten

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.

drasticactions avatar Apr 04 '24 09:04 drasticactions

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

datvm avatar Apr 11 '24 14:04 datvm

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

spajce avatar Apr 13 '24 08:04 spajce

@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

spajce avatar Apr 13 '24 11:04 spajce

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

datvm avatar Apr 15 '24 17:04 datvm

@drasticactions it's getting closed again :(

datvm avatar Apr 23 '24 01:04 datvm

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

aritchie avatar May 01 '24 18:05 aritchie

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

LiamMorrow avatar Jul 11 '24 13:07 LiamMorrow

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.

grumpymonk avatar Jul 23 '24 13:07 grumpymonk

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 avatar Jul 24 '24 00:07 ngochivinh

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!

ngochivinh avatar Jul 24 '24 06:07 ngochivinh

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

datvm avatar Aug 01 '24 15:08 datvm

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 />

Marckxp avatar Aug 06 '24 23:08 Marckxp

@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.

datvm avatar Aug 08 '24 19:08 datvm

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.

ktravelet avatar Aug 09 '24 15:08 ktravelet

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.

DaveHogan avatar Aug 18 '24 00:08 DaveHogan

@mkArtakMSFT Any thoughts on this one? Thanks!

samhouts avatar Aug 21 '24 22:08 samhouts

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>

gautambjain avatar Aug 24 '24 08:08 gautambjain

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.

datvm avatar Aug 24 '24 14:08 datvm