Repository navigation
resolver: mark the Node builtin polyfills as free of side effects #46783
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
Changes from 2 commits
b88081c
2f772af
ed41e0d
d3c8fe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use crate::fs; | ||
| use crate::package_json::{PackageJSON, SideEffects}; | ||
| use crate::package_json::PackageJSON; | ||
| use bun_ast::SideEffects; | ||
| use bun_options_types::bundle_enums::ModuleType; | ||
|
|
||
| pub const IMPORT_PATH: &[u8] = b"/bun-vfs$$/node_modules/"; | ||
|
|
@@ -15,6 +16,7 @@ pub(crate) struct FallbackModule { | |
| // PackageJSONs and we hand out `&'static` borrows into it. | ||
| pub package_json: &'static PackageJSON, | ||
| pub code: fn() -> &'static str, | ||
| pub side_effects: SideEffects, | ||
| } | ||
|
|
||
| // This workaround exists to allow bun_core::runtime_embed_file to work. | ||
|
|
@@ -78,10 +80,14 @@ struct FallbackSpec { | |
| pretty: &'static [u8], | ||
| pkgjson_path: &'static [u8], | ||
| code: fn() -> &'static str, | ||
| side_effects: SideEffects, | ||
| } | ||
|
|
||
| macro_rules! fallback_spec { | ||
| ($name:literal, $code_path:literal) => { | ||
| fallback_spec!($name, $code_path, SideEffects::NoSideEffectsPureData) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Browser builds with --splitting that use a Node polyfill can now run the polyfill after a later-imported file with side effects, so source evaluation order is no longer honored. Every polyfill except sys is declared NoSideEffectsPureData at src/resolver/node_fallbacks.rs:88, and the linker's chunk_of_file in findAllImportedPartsInJSOrder.rs:30 treats such a chunk as running nothing and moves its import behind chunks that do. Polyfills like url, util, buffer and timers read globals at load, so a setup file imported after them can now replace or freeze what they capture. Fix: keep the tree-shaking value for dropping unused imports but have chunk ordering treat polyfill files as running code at load, so a used polyfill keeps its source position. Why this was flaggedA browser build with splitting: true and several entry points where entry.js has Verification: src/resolver/node_fallbacks.rs:88 defaults every polyfill to
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is still open, and it is the question in the PR description. The branch is back to the resolver-only change, so a used polyfill orders like a I built the variant that you describe: a separate value that chunk ordering reads as code that runs at load. A self-review rejected it. It needs 128 added source lines of linker state, and it keeps the polyfill in a build that uses a polyfill which the unused one requires. The notes in the description have its numbers. A maintainer decides which of the two to take. |
||
| }; | ||
| ($name:literal, $code_path:literal, $side_effects:expr) => { | ||
| FallbackSpec { | ||
| name: $name.as_bytes(), | ||
| index_path: ::const_format::concatcp!("/bun-vfs$$/node_modules/", $name, "/index.js") | ||
|
|
@@ -94,6 +100,7 @@ macro_rules! fallback_spec { | |
| ) | ||
| .as_bytes(), | ||
| code: create_source_code_getter!($code_path), | ||
| side_effects: $side_effects, | ||
| } | ||
| }; | ||
| } | ||
|
|
@@ -116,7 +123,9 @@ static SPECS: [FallbackSpec; 23] = [ | |
| fallback_spec!("querystring", "node-fallbacks/querystring.js"), | ||
| fallback_spec!("stream", "node-fallbacks/stream.js"), | ||
| fallback_spec!("string_decoder", "node-fallbacks/string_decoder.js"), | ||
| fallback_spec!("sys", "node-fallbacks/sys.js"), | ||
| // Keeps its side effects: it is `export * from "util"`, and the linker does not | ||
| // initialize a `require()`d `util` for a binding imported through a file without them. | ||
|
Comment on lines
+126
to
+127
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need a paragraph-long comment to justify why the workaround is OK, the code is wrong — fix the code
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. The exception for |
||
| fallback_spec!("sys", "node-fallbacks/sys.js", SideEffects::HasSideEffects), | ||
| fallback_spec!("timers", "node-fallbacks/timers.js"), | ||
| fallback_spec!("tty", "node-fallbacks/tty.js"), | ||
| fallback_spec!("url", "node-fallbacks/url.js"), | ||
|
|
@@ -155,7 +164,6 @@ fn init_modules() { | |
| // main_fields/browser_map are never read on this code path; | ||
| // Default::default() fills them safely. | ||
| source: bun_ast::Source::init_path_string(spec.pkgjson_path, b""), | ||
| side_effects: SideEffects::False, | ||
| ..Default::default() | ||
| }, | ||
| fs::Path::init_with_namespace_virtual(spec.index_path, b"node", spec.pretty), | ||
|
|
@@ -170,13 +178,14 @@ fn init_modules() { | |
| unsafe { | ||
| *MODULES.get() = Some(modules); | ||
| let modules_ref: &'static [FallbackEntry] = (*MODULES.get()).as_deref().unwrap(); | ||
| for (name, pkg, path, code) in modules_ref.iter() { | ||
| for ((name, pkg, path, code), spec) in modules_ref.iter().zip(&SPECS) { | ||
| m.put_assume_capacity( | ||
| name, | ||
| FallbackModule { | ||
| path: *path, | ||
| package_json: pkg, | ||
| code: *code, | ||
| side_effects: spec.side_effects, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.