-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
node compat: Dynamic import leads to missing globals #20028
Comments
The contents of the file are: "use strict";
/**
* Node.js implementation of browser `btoa` function.
*
* @packageDocumentation
* @internal
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.base64Encode = void 0;
/**
* @internal
*/
function base64Encode(str) {
return Buffer.from(str).toString("base64");
}
exports.base64Encode = base64Encode;
//# sourceMappingURL=btoa.js.map⏎ I think this might be the limitation Luca mentioned, but I'm not sure. |
No, this doesn't look related. Please bisect if you think it's related to my globals PR. I think what is happening is that I am about 98% confident this bug was already present when I implemented |
Ah, thanks for the pointer, that's probably related to #15826 then. |
Minimal reproduce. Seems like something depends on simple AST, actually. But should implement as AST-independent. OK when ==> foo.test.ts <==
import kleur from 'npm:kleur@3.0.0'
Deno.test('x', () => {
console.log({ kleur })
kleur.red('1x')
})
==> test.ts <==
await import('./foo.test.ts') Error when ==> foo.test.ts <==
import kleur from 'npm:kleur@3.0.0'
Deno.test('x', () => {
console.log({ kleur })
kleur.red('1x')
})
==> test.ts <==
await import(`./foo.test.ts`) |
I've been able to reproduce this this with both Deno 1.35.3 and 1.36.0 and found that the globals for Node packages are set right if you statically import those packages before they are dynamically imported. williamhorning/deno-dynamic-import-globals has an example of this using undici. |
@williamhorning Thank you! I've been looking for a temporary fix. Importing any npm module before the dynamic import did the trick. So I'm basically doing |
FYI you should be able to work-around the problem by importing any built-in |
To fix bugs around detection of when node emulation is required, we will just eagerly initialize it. The improvements we make to reduce the impact of the startup time: - [x] Process stdin/stdout/stderr are lazily created - [x] node.js global proxy no longer allocates on each access check - [x] Process checks for `beforeExit` listeners before doing expensive shutdown work - [x] Process should avoid adding global event handlers until listeners are added Benchmarking this PR (`89de7e1ff`) vs main (`41cad2179`) ``` 12:36 $ third_party/prebuilt/mac/hyperfine --warmup 100 -S none './deno-41cad2179 run ./empty.js' './deno-89de7e1ff run ./empty.js' Benchmark 1: ./deno-41cad2179 run ./empty.js Time (mean ± σ): 24.3 ms ± 1.6 ms [User: 16.2 ms, System: 6.0 ms] Range (min … max): 21.1 ms … 29.1 ms 115 runs Benchmark 2: ./deno-89de7e1ff run ./empty.js Time (mean ± σ): 24.0 ms ± 1.4 ms [User: 16.3 ms, System: 5.6 ms] Range (min … max): 21.3 ms … 28.6 ms 126 runs ``` Fixes #20142 Fixes #15826 Fixes #20028
To fix bugs around detection of when node emulation is required, we will just eagerly initialize it. The improvements we make to reduce the impact of the startup time: - [x] Process stdin/stdout/stderr are lazily created - [x] node.js global proxy no longer allocates on each access check - [x] Process checks for `beforeExit` listeners before doing expensive shutdown work - [x] Process should avoid adding global event handlers until listeners are added Benchmarking this PR (`89de7e1ff`) vs main (`41cad2179`) ``` 12:36 $ third_party/prebuilt/mac/hyperfine --warmup 100 -S none './deno-41cad2179 run ./empty.js' './deno-89de7e1ff run ./empty.js' Benchmark 1: ./deno-41cad2179 run ./empty.js Time (mean ± σ): 24.3 ms ± 1.6 ms [User: 16.2 ms, System: 6.0 ms] Range (min … max): 21.1 ms … 29.1 ms 115 runs Benchmark 2: ./deno-89de7e1ff run ./empty.js Time (mean ± σ): 24.0 ms ± 1.4 ms [User: 16.3 ms, System: 5.6 ms] Range (min … max): 21.3 ms … 28.6 ms 126 runs ``` Fixes denoland#20142 Fixes denoland#15826 Fixes denoland#20028
To fix bugs around detection of when node emulation is required, we will just eagerly initialize it. The improvements we make to reduce the impact of the startup time: - [x] Process stdin/stdout/stderr are lazily created - [x] node.js global proxy no longer allocates on each access check - [x] Process checks for `beforeExit` listeners before doing expensive shutdown work - [x] Process should avoid adding global event handlers until listeners are added Benchmarking this PR (`89de7e1ff`) vs main (`41cad2179`) ``` 12:36 $ third_party/prebuilt/mac/hyperfine --warmup 100 -S none './deno-41cad2179 run ./empty.js' './deno-89de7e1ff run ./empty.js' Benchmark 1: ./deno-41cad2179 run ./empty.js Time (mean ± σ): 24.3 ms ± 1.6 ms [User: 16.2 ms, System: 6.0 ms] Range (min … max): 21.1 ms … 29.1 ms 115 runs Benchmark 2: ./deno-89de7e1ff run ./empty.js Time (mean ± σ): 24.0 ms ± 1.4 ms [User: 16.3 ms, System: 5.6 ms] Range (min … max): 21.3 ms … 28.6 ms 126 runs ``` Fixes #20142 Fixes #15826 Fixes #20028
A dynamic import that is not statically analysable breaks npm detection which leads to node globals missing.
This was originally reported on the Fresh repo denoland/fresh#1577 . In Fresh the
dev.ts
entry point calls a dynamic import where the specifier is not statically analysable:Reproduction repo: https://github.com/marvinhagemeister/deno-npm-bug
Steps to reproduce
deno run -A foo.ts
The text was updated successfully, but these errors were encountered: