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

[browser] Unnecessary browser and node calls breaking usage in restricted environments #91558

Closed
elringus opened this issue Sep 4, 2023 · 13 comments
Assignees
Labels
arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript os-browser Browser variant of arch-wasm
Milestone

Comments

@elringus
Copy link

elringus commented Sep 4, 2023

I'm trying to build .NET 8 library for VS Code web extension, where neither browser APIs (window/document), nor node modules are available. Previously (in .NET 6) I had to strip generated JS wrapper from various unconditional usages of such APIs. I hoped in .NET 8 it's solved (and I can see lots of changes in this direction), but unfortunately there are stilll issues. For example, createDotnetRuntime function has following (from bundled library sources, so it's bit mangled):

63881f3024a9af0730ba65c668c811cb

document is undefined in VS Code and require('url') causes exception. I'm injecting all the requires resources (runtime.wasm, dotnet.runtime.js, etc) via the assets in config, so no IO should be required to boot the runtime.

Is it possible to guard browser/node-specific calls like this for .NET 8?

The Specific Offenders in .NET 8

Here is the link to the patch I'm using to strip all the offenders: https://github.com/elringus/bootsharp/blob/main/src/cs/Bootsharp.Publish/Pack/ModulePatcher/InternalPatcher.cs After the patch, bundlers are only complaining about module and process unresolved externals (tested with webpack, rollup, esbuild, vite) and the bundled runtime is working in browsers, node, deno, bun and VS Code web extensions.

Regarding module and process — would be optimal to guard them as well, though I wasn't able to find a straightforward way to do that (conditional usage still trigger bundlers). As a workaround, I'm marking them as global (eg, -g process,module in rollup), which seem to satisfy the bundlers.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Sep 4, 2023
@ghost
Copy link

ghost commented Sep 4, 2023

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

Issue Details

Description

I'm trying to build .NET 8 library for VS Code web extension, where neither browser APIs (window/document), nor node modules are available. Previously (in .NET 6) I had to strip generated JS wrapper from various unconditional usages of such APIs. I hoped in .NET 8 it's solved (and I can see lots of changes in this direction), but unfortunately there are stilll issues. For example, createDotnetRuntime function has following (from bundled library sources, so it's bit mangled):

63881f3024a9af0730ba65c668c811cb

document is undefined in VS Code and require('url') causes exception. I'm injecting all the requires resources (runtime.wasm, dotnet.runtime.js, etc) via the assets in config, so no IO should be required to boot the runtime.

Is it possible to guard browser/node-specific calls like this for .NET 8?

Reproduction Steps

Install the VS Code extension and notice Cannot load module 'url'. error in the developer console.

Remove require('url') in createDotnetRuntime funtion, repack the extension and notice that the error is gone.

Expected behavior

No calls to browser or node-specific APIs (eg, window, document, new URL, require(...), etc) are performed when all the required dotnet assets are specified in config.

Actual behavior

Browser and node-specific APIs are used even when all the required dotnet assets are specified in config.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8.0.0-rc.2.23431.9

Other information

No response

Author: Elringus
Assignees: -
Labels:

area-Meta, untriaged

Milestone: -

@elringus elringus changed the title [browser] Unnecessary browser and node calls breaking usage in restricted envisionments [browser] Unnecessary browser and node calls breaking usage in restricted environments Sep 4, 2023
@elringus
Copy link
Author

elringus commented Sep 5, 2023

Trying to find the culprit in the sources, but stuck here: https://github.com/dotnet/runtime/blob/main/src/mono/wasm/runtime/es6/dotnet.es6.lib.js#L60

There are no other " _scriptDir" vars in the codebase, so it must be it, but not sure where require('url') comes from.

@radical radical added the arch-wasm WebAssembly architecture label Sep 5, 2023
@ghost
Copy link

ghost commented Sep 5, 2023

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

Issue Details

Description

I'm trying to build .NET 8 library for VS Code web extension, where neither browser APIs (window/document), nor node modules are available. Previously (in .NET 6) I had to strip generated JS wrapper from various unconditional usages of such APIs. I hoped in .NET 8 it's solved (and I can see lots of changes in this direction), but unfortunately there are stilll issues. For example, createDotnetRuntime function has following (from bundled library sources, so it's bit mangled):

63881f3024a9af0730ba65c668c811cb

document is undefined in VS Code and require('url') causes exception. I'm injecting all the requires resources (runtime.wasm, dotnet.runtime.js, etc) via the assets in config, so no IO should be required to boot the runtime.

Is it possible to guard browser/node-specific calls like this for .NET 8?

Reproduction Steps

Install the VS Code extension and notice Cannot load module 'url'. error in the developer console.

Remove require('url') in createDotnetRuntime funtion, repack the extension and notice that the error is gone.

Expected behavior

No calls to browser or node-specific APIs (eg, window, document, new URL, require(...), etc) are performed when all the required dotnet assets are specified in config.

Actual behavior

Browser and node-specific APIs are used even when all the required dotnet assets are specified in config.

Regression?

No response

Known Workarounds

No response

Configuration

.NET 8.0.0-rc.2.23431.9

Other information

No response

Author: Elringus
Assignees: -
Labels:

arch-wasm, area-Meta, untriaged

Milestone: -

@radical
Copy link
Member

radical commented Sep 5, 2023

cc @pavelsavara @maraf

@pavelsavara
Copy link
Member

require in the emscripten code is quite annoying.
Perhaps it would go away if you pass

  <PropertyGroup>
     <EmccEnvironment>web,worker</EmccEnvironment>
  </PropertyGroup>

But that would make it node incompatible.

For the import.meta.url I thought we solved it by #90392
We are not going back to CJS.

@pavelsavara pavelsavara self-assigned this Sep 5, 2023
@pavelsavara pavelsavara added this to the Future milestone Sep 5, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Sep 5, 2023
@elringus
Copy link
Author

elringus commented Sep 5, 2023

But that would make it node incompatible.

I hope we can me it work in both node, browser and restricted environments w/o special builds for each. This is already working on my end with those 3 modifications I've mentioned above.

For the import.meta.url I thought we solved it by #90392

Yeah, that was essential, but not enough for VS Code, unfortunately (sorry I wasn't able to test at the time, had to rewrite the whole library for .NET 8 and new interop). If those 3 calls to import.meta and new URL could be guarded somehow, it would solve the issue completely. The workaround with stripping them on build post-process will work for the time being.

@pavelsavara
Copy link
Member

Yeah, that was essential, but not enough for VS Code

Could you please point me to more details about VS Code constraints ? Documentation ?

@elringus
Copy link
Author

elringus commented Sep 5, 2023

Here: https://code.visualstudio.com/api/extension-guides/web-extensions#migrate-extension-with-code

Due to V8 cache not available on node (and dotnet runtime requiring it), we also have to use the web format for standalone VS Code environment. Basically, we can't use any import/require, except the pre-defined vscode, which provide limited VS Code API. Also, no window/documents, etc browser-specific stuff.

@pavelsavara
Copy link
Member

Due to V8 cache not available on node (and dotnet runtime requiring it)

Cache API ? This should disable it.

dotnet
    .withConfig({
        cacheBootResources: false,
    })

@elringus
Copy link
Author

elringus commented Sep 5, 2023

Presumably it's something about v8 code caching: https://v8.dev/blog/code-caching I didn't really investigate it properly. Just had invalid host defined options error when attempted to run the extension in node and prompt googling showed that it's because of the cache API not available in modules. The extension I'm working on works fine in web anyway, so I don't really need it.

@pavelsavara
Copy link
Member

Could you please update the top level description and list specific offenders ? That would make at least some of them more actionable.

@elringus
Copy link
Author

elringus commented Sep 5, 2023

Done. Thanks for looking into this!

@pavelsavara
Copy link
Member

Migrated into #80045

@github-actions github-actions bot locked and limited conversation to collaborators Feb 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly architecture area-System.Runtime.InteropServices.JavaScript os-browser Browser variant of arch-wasm
Projects
None yet
Development

No branches or pull requests

3 participants