dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

[wasm] WasmFilesToIncludeInFileSystem not doing anything: System.IO.FileNotFoundException: IO_FileNotFound_FileName #56715

Open emepetres opened 3 years ago

emepetres commented 3 years ago

Description

Using the item WasmFilesToIncludeInFileSystem to include a file into the wasm vfs does not add it. The file is not shown in FS.root, and triying to reading it from .Net throws the exception System.IO.FileNotFoundException: IO_FileNotFound_FileName.

Having a file.txt in the project root, it can be reproduced doing:

csproj

<ItemGroup>
    <WasmFilesToIncludeInFileSystem Include="file.txt" TargetPath="/file.txt"/>
</ItemGroup>

Program.cs

var txt = File.ReadAllText("file.txt");
Console.WriteLine(txt);

Small sample to reproduce the issue can be found here

Configuration

Using latest runtime sdk 6.0.100-rc.1.21401.2 Using Microsoft.NET.Sdk.BlazorWebAssembly as targets sdk.

<FrameworkReference Update="Microsoft.NETCore.App" RuntimeFrameworkVersion="6.0.0-rc.1.21401.1" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.0-rc.1.21381.3" />

Regression?

I guess it worked at some point, as it is being used in some tests.

Other information

System.AggregateException: One or more errors occurred. (Could not find file '/file.txt'.)
 ---> System.IO.FileNotFoundException: Could not find file '/file.txt'.
File name: '/file.txt'
   at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) in System.Private.CoreLib.dll:token 0x6000009+0xc
   at Interop.CheckIo(Error error, String path, Boolean isDirectory, Func`2 errorRewriter) in System.Private.CoreLib.dll:token 0x600000a+0x4
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) in System.Private.CoreLib.dll:token 0x60000a1+0x54
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x60000a5+0xb
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005c25+0x31
   at System.IO.Strategies.UnixFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005c43+0x0
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005bd2+0x0
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005bca+0x7
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005815+0x16
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) in System.Private.CoreLib.dll:token 0x6005813+0x0
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) in System.Private.CoreLib.dll:token 0x6005812+0x0
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005811+0x0
   at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005964+0x1b
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005960+0x0
   at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks) in System.Private.CoreLib.dll:token 0x600595f+0x0
   at System.IO.File.InternalReadAllText(String path, Encoding encoding) in System.Private.CoreLib.dll:token 0x600579b+0x0
   at System.IO.File.ReadAllText(String path) in System.Private.CoreLib.dll:token 0x6005799+0x26
   at Test.Main(String[] args) in Wasm.FileSystem.Sample.dll:token 0x6000001+0x127
ghost commented 3 years ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Using the item `WasmFilesToIncludeInFileSystem` to include a file into the wasm vfs does not add it. The file is not shown in FS.root, and triying to reading it from .Net throws the exception `System.IO.FileNotFoundException: IO_FileNotFound_FileName`. Having a _file.txt_ in the project root, it can be reproduced doing: csproj ```xml ``` Program.cs ```csharp var txt = File.ReadAllText("file.txt"); Console.WriteLine(txt); ``` Small sample to reproduce the issue can be found [here](https://github.com/emepetres/dotnet-wasm-sample/tree/main/src/filesystem) ### Configuration Using latest runtime sdk 6.0.100-rc.1.21401.2 Using Microsoft.NET.Sdk.BlazorWebAssembly as targets sdk. ``` ``` ### Regression? I guess it worked at some point, as it is being used in some tests. ### Other information ``` System.AggregateException: One or more errors occurred. (Could not find file '/file.txt'.) ---> System.IO.FileNotFoundException: Could not find file '/file.txt'. File name: '/file.txt' at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) in System.Private.CoreLib.dll:token 0x6000009+0xc at Interop.CheckIo(Error error, String path, Boolean isDirectory, Func`2 errorRewriter) in System.Private.CoreLib.dll:token 0x600000a+0x4 at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) in System.Private.CoreLib.dll:token 0x60000a1+0x54 at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x60000a5+0xb at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005c25+0x31 at System.IO.Strategies.UnixFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005c43+0x0 at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005bd2+0x0 at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005bca+0x7 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005815+0x16 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) in System.Private.CoreLib.dll:token 0x6005813+0x0 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) in System.Private.CoreLib.dll:token 0x6005812+0x0 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005811+0x0 at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005964+0x1b at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005960+0x0 at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks) in System.Private.CoreLib.dll:token 0x600595f+0x0 at System.IO.File.InternalReadAllText(String path, Encoding encoding) in System.Private.CoreLib.dll:token 0x600579b+0x0 at System.IO.File.ReadAllText(String path) in System.Private.CoreLib.dll:token 0x6005799+0x26 at Test.Main(String[] args) in Wasm.FileSystem.Sample.dll:token 0x6000001+0x127 ```
Author: emepetres
Assignees: -
Labels: `area-System.IO`, `untriaged`
Milestone: -
adamsitnik commented 3 years ago

Using the item WasmFilesToIncludeInFileSystem to include a file into the wasm vfs does not add it. The file is not shown in FS.root

@marek-safar @akoeplinger what would be the best repo|area for this issue? It seems to be realted to build toolchain rather than System.IO itself

ghost commented 3 years ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

Issue Details
### Description Using the item `WasmFilesToIncludeInFileSystem` to include a file into the wasm vfs does not add it. The file is not shown in FS.root, and triying to reading it from .Net throws the exception `System.IO.FileNotFoundException: IO_FileNotFound_FileName`. Having a _file.txt_ in the project root, it can be reproduced doing: csproj ```xml ``` Program.cs ```csharp var txt = File.ReadAllText("file.txt"); Console.WriteLine(txt); ``` Small sample to reproduce the issue can be found [here](https://github.com/emepetres/dotnet-wasm-sample/tree/main/src/filesystem) ### Configuration Using latest runtime sdk 6.0.100-rc.1.21401.2 Using Microsoft.NET.Sdk.BlazorWebAssembly as targets sdk. ``` ``` ### Regression? I guess it worked at some point, as it is being used in some tests. ### Other information ``` System.AggregateException: One or more errors occurred. (Could not find file '/file.txt'.) ---> System.IO.FileNotFoundException: Could not find file '/file.txt'. File name: '/file.txt' at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirectory, Func`2 errorRewriter) in System.Private.CoreLib.dll:token 0x6000009+0xc at Interop.CheckIo(Error error, String path, Boolean isDirectory, Func`2 errorRewriter) in System.Private.CoreLib.dll:token 0x600000a+0x4 at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String path, OpenFlags flags, Int32 mode) in System.Private.CoreLib.dll:token 0x60000a1+0x54 at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x60000a5+0xb at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005c25+0x31 at System.IO.Strategies.UnixFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005c43+0x0 at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005bd2+0x0 at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005bca+0x7 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize) in System.Private.CoreLib.dll:token 0x6005815+0x16 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options) in System.Private.CoreLib.dll:token 0x6005813+0x0 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) in System.Private.CoreLib.dll:token 0x6005812+0x0 at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005811+0x0 at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005964+0x1b at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) in System.Private.CoreLib.dll:token 0x6005960+0x0 at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks) in System.Private.CoreLib.dll:token 0x600595f+0x0 at System.IO.File.InternalReadAllText(String path, Encoding encoding) in System.Private.CoreLib.dll:token 0x600579b+0x0 at System.IO.File.ReadAllText(String path) in System.Private.CoreLib.dll:token 0x6005799+0x26 at Test.Main(String[] args) in Wasm.FileSystem.Sample.dll:token 0x6000001+0x127 ```
Author: emepetres
Assignees: -
Labels: `arch-wasm`, `untriaged`
Milestone: -
akoeplinger commented 3 years ago

hm yeah looks like we don't have a good area tag. /cc @lewing @radical

pavelsavara commented 3 years ago

I noticed that sdk-manifests\6.0.100\microsoft.net.workload.mono.toolchain\WorkloadManifest.targets sets WasmGenerateAppBundle to false for blazor project. Therefore _WasmGenerateAppBundle in /src/mono/wasm/build/WasmApp.targets is not running. That is, even when I set WasmGenerateAppBundle to true in my project.

radical commented 3 years ago

@(WasmFilesToIncludeInFileSystem) is not used by blazor. It uses @(Content) items, and that's what the non-blazor projects will start using too.

lewing commented 3 years ago

We should add warnings and notes in the docs for items that don't apply to blazor

emepetres commented 3 years ago

So @radical, If I understood correctly, having a file in sample-data/file.txt I guess I should do something like this in blazor:

Directory.CreateDirectory("sample-data");
using (var httpResponse = await httpClient.GetAsync("sample-data/file.txt", HttpCompletionOption.ResponseHeadersRead))
using(var st = await httpResponse.Content.ReadAsStreamAsync())
using (var fs = File.OpenWrite("sample-data/file.txt"))
{
    await st.CopyToAsync(fs);
}

var file = File.ReadAllText("sample-data/file.txt");
Console.WriteLine(file);

However it is a shame that emscripten preload is not currently compatible with blazor wasm sdk, any plans to allow it?

radical commented 3 years ago

Blazor team would be able to answer that best. @captainsafia ?

captainsafia commented 3 years ago

@javiercn has been playing around with static assets + Blazor which might be relevant here. AFAIK, we don't do anything special for AoT here.

javiercn commented 3 years ago

We don't know about the WasmFilesToIncludeInFileSystem item group, so we don't even look at it.

@lewing in general we want to be good citizens here and make sure we support things like these, we just need to know we need to add them :). We can also work out a mechanic to try and pick these things automagically.

lewing commented 2 years ago

@radical lets add this to the bundler tasks