elringus / bootsharp

Compile C# solution into single-file ES module with auto-generated JavaScript bindings and type definitions
https://sharp.elringus.com
MIT License
673 stars 36 forks source link

Access local file system from .Net #114

Closed rossknudsen closed 1 year ago

rossknudsen commented 1 year ago

I'm working on a VS Code extension and writing the base implementation in .Net and calling from the extension in Typescript. I'm having trouble accessing local files from the .Net code using System.IO.* API. E.g. calling File.Exists() on a local Windows file path returns false for a valid path. I'm guessing that I don't have permission to access the file from the running process or the path needs to be modified. Any ideas?

rossknudsen commented 1 year ago

Some experimentation:

using System.IO;
Directory.GetCurrentDirectory(); // => "/"
Directory.Exists(Directory.GetCurrentDirectory()); // => true
Directory.GetFiles("/").Length; // => 0
string.Join(",", Directory.GetDirectories("/")); // => "/tmp,/home,/dev,/proc"
string.Join(",", Directory.GetDirectories("/home")); // => "/home/web_user"
rossknudsen commented 1 year ago

Looks like the environment is sandboxed and you need to pass in a mapping for the WASM when initializing. I'd say you'd want to extend BootData with an option to pass through the options, but I couldn't see where the changes would be made (somewhere in dotnet.js I think).

elringus commented 1 year ago

Don't think nodejs's FS will work, even if you're not targeting web extensions because of the security restrictions. You'd have to use VS Code's abstraction layer APIs to access the files.