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

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

Open
jamalbendadi opened this issue Aug 29, 2023 · 33 comments
Open

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

jamalbendadi opened this issue Aug 29, 2023 · 33 comments
Assignees
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView area-single-project Splash Screen, Multi-Targeting, MauiFont, MauiImage, MauiAsset, Resizetizer p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint platform/macOS 🍏 macOS / Mac Catalyst s/triaged Issue has been reviewed t/bug Something isn't working

Comments

@jamalbendadi
Copy link

jamalbendadi commented Aug 29, 2023

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 jamalbendadi added the t/bug Something isn't working label Aug 29, 2023
@drasticactions
Copy link
Contributor

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
Copy link
Author

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]

@samhouts samhouts added area-blazor Blazor Hybrid / Desktop, BlazorWebView Blazor ❤️ MAUI Issues in MAUI functionality that affect Blazor, but are not bugs in Blazor itself labels Aug 30, 2023
@Eilon Eilon added area-single-project Splash Screen, Multi-Targeting, MauiFont, MauiImage, MauiAsset, Resizetizer and removed area-blazor Blazor Hybrid / Desktop, BlazorWebView labels Aug 30, 2023
theofanis added a commit to bol-project/bol-wallet that referenced this issue Jan 2, 2024
…lties

# Conflicts:
#	BolWallet/BolWallet.csproj

Also, fixes appsetting.json reference and moves TestAccount.json to root to overcome known issue dotnet/maui#17078
@cmydur
Copy link

cmydur commented Jan 9, 2024

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

@Don09877890
Copy link

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

@cmydur
Copy link

cmydur commented Jan 24, 2024

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

@samhouts samhouts added the platform/macOS 🍏 macOS / Mac Catalyst label Feb 2, 2024
@datvm
Copy link

datvm commented Feb 5, 2024

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
Copy link

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 jaosnz-rep added s/triaged Issue has been reviewed s/try-latest-version Please try to reproduce the potential issue on the latest public version labels Feb 21, 2024
@ghost

This comment was marked as outdated.

@ghost ghost closed this as completed Feb 28, 2024
@dhindrik
Copy link
Contributor

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

@Brosten

This comment was marked as outdated.

@drasticactions
Copy link
Contributor

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

This comment was marked as outdated.

@spajce
Copy link

spajce commented Apr 13, 2024

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

@spajce
Copy link

spajce commented Apr 13, 2024

@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
Copy link

datvm commented Apr 15, 2024

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

LiamMorrow added a commit to LiamMorrow/LiftLog that referenced this issue Jul 11, 2024
@LiamMorrow
Copy link

LiamMorrow commented Jul 11, 2024

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
Copy link

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
Copy link

ngochivinh commented Jul 24, 2024

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
Copy link

ngochivinh commented Jul 24, 2024

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
Copy link

datvm commented Aug 1, 2024

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

@Marckxp
Copy link

Marckxp commented Aug 6, 2024

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
Copy link

datvm commented Aug 8, 2024

@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
Copy link

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
Copy link

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 samhouts removed the Blazor ❤️ MAUI Issues in MAUI functionality that affect Blazor, but are not bugs in Blazor itself label Aug 21, 2024
@samhouts
Copy link
Member

@mkArtakMSFT Any thoughts on this one? Thanks!

@PureWeen PureWeen added this to the .NET 8 SR9 milestone Aug 23, 2024
@PureWeen PureWeen added the p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint label Aug 23, 2024
@gautambjain
Copy link

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
Copy link

datvm commented Aug 24, 2024

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
Copy link
Member

@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
Copy link

malsabi commented Oct 16, 2024

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

@Marini88
Copy link

Marini88 commented Nov 4, 2024

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

@marcos-dev8
Copy link

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.

RoyalScribblz pushed a commit to fM4s0n/Electrify that referenced this issue Nov 7, 2024
fM4s0n pushed a commit to fM4s0n/Electrify that referenced this issue Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Blazor Hybrid / Desktop, BlazorWebView area-single-project Splash Screen, Multi-Targeting, MauiFont, MauiImage, MauiAsset, Resizetizer p/1 Work that is important, and has been scheduled for release in this or an upcoming sprint platform/macOS 🍏 macOS / Mac Catalyst s/triaged Issue has been reviewed t/bug Something isn't working
Projects
Status: Cut from this Milestone
Development

No branches or pull requests