-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix(hir): aliased native-class import new no longer throws ReferenceError (#8730)
#8738
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
Closed
proggeramlug
wants to merge
1
commit into
PerryTS:main
from
proggeramlug:fix/8730-newthrow-scope-resolution
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix an aliased ESM named import of a Node built-in class (`import { BlockList as Wj4 } from "net"`, `{ AsyncLocalStorage as J_z } from "async_hooks"`, `{ PassThrough as Lrz } from "stream"`) throwing `ReferenceError: identifier is not defined` when constructed. The `new`-lowering already rewrites the alias to the class's export name so construction matches the un-aliased form, but the unresolved-`new` guard added in #8688 re-checked the native-module registry under that rewritten export name — which is keyed on the local import name — and, since these classes are not reified global builtins, fired the nameless throw at module init. The guard now also consults the registry under the original imported identifier, so aliased native-class imports resolve and construct exactly like their un-aliased form. This unblocked the natively compiled Claude Code cli.js 2.1.112 bundle, which crashed at module init on nearly every command. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
crates/perry-hir/tests/aliased_native_new_resolution.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| //! Regression test for #8730: an ALIASED ESM named import of a Node built-in | ||
| //! class (`import { BlockList as Wj4 } from "net"; new Wj4()`) must not lower | ||
| //! `new <alias>()` to the nameless `js_throw_reference_error_unresolved_get` | ||
| //! throw. | ||
| //! | ||
| //! Root cause: the alias-rewrite block in `lower_new` replaces the callee's | ||
| //! `class_name` with the native class's EXPORT name (`Wj4` -> `BlockList`) so | ||
| //! the construction path matches the un-aliased form, but the freshly-added | ||
| //! (#8688) unresolved-`new` guard then consulted `lookup_native_module` under | ||
| //! that rewritten export name — which is not in the registry (it is keyed on | ||
| //! the LOCAL import name). None of these classes are reified global builtins, | ||
| //! so the guard fired and every command threw `ReferenceError: identifier is | ||
| //! not defined` at module init. | ||
|
|
||
| use perry_diagnostics::SourceCache; | ||
| use perry_hir::lower_module; | ||
| use perry_parser::parse_typescript_with_cache; | ||
|
|
||
| const THROW_HELPER: &str = "js_throw_reference_error_unresolved_get"; | ||
|
|
||
| fn lower_debug(src: &str) -> String { | ||
| let src = src.to_string(); | ||
| std::thread::Builder::new() | ||
| .stack_size(32 * 1024 * 1024) | ||
| .spawn(move || { | ||
| let mut cache = SourceCache::new(); | ||
| let parsed = | ||
| parse_typescript_with_cache(&src, "aliased_native_new_resolution.ts", &mut cache) | ||
| .expect("parse should succeed"); | ||
| let module = lower_module(&parsed.module, "test", "aliased_native_new_resolution.ts") | ||
| .expect("lowering should succeed"); | ||
| format!("{module:#?}") | ||
| }) | ||
| .expect("spawn lower thread") | ||
| .join() | ||
| .expect("lower thread panicked") | ||
| } | ||
|
|
||
| #[test] | ||
| fn aliased_native_class_import_does_not_lower_to_nameless_throw() { | ||
| // Each mirrors a real cli.js 2.1.112 shape from #8730 (BlockList/Wj4 built | ||
| // and `.addSubnet`-ed at module init; AsyncLocalStorage/J_z; PassThrough). | ||
| let cases = [ | ||
| ( | ||
| "BlockList", | ||
| r#"import { BlockList as Wj4 } from "net"; | ||
| const b = new Wj4(); | ||
| b.addSubnet("10.0.0.0", 8); | ||
| console.log(b.check("10.1.2.3"));"#, | ||
| ), | ||
| ( | ||
| "AsyncLocalStorage", | ||
| r#"import { AsyncLocalStorage as J_z } from "async_hooks"; | ||
| const s = new J_z(); | ||
| console.log(typeof s.run);"#, | ||
| ), | ||
| ( | ||
| "PassThrough", | ||
| r#"import { PassThrough as Lrz } from "stream"; | ||
| const p = new Lrz(); | ||
| console.log(typeof p.pipe);"#, | ||
| ), | ||
| ]; | ||
|
|
||
| for (label, src) in cases { | ||
| let debug = lower_debug(src); | ||
| assert!( | ||
| !debug.contains(THROW_HELPER), | ||
| "aliased native import `{label}` must construct, not throw the nameless \ | ||
| ReferenceError at module init:\n{debug}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn unaliased_native_class_import_still_constructs() { | ||
| // Control: the un-aliased form was never broken; keep it green so the fix | ||
| // is symmetric across aliased/un-aliased native imports. | ||
| let debug = lower_debug( | ||
| r#"import { BlockList } from "net"; | ||
| const b = new BlockList(); | ||
| b.addSubnet("10.0.0.0", 8); | ||
| console.log(b.check("10.1.2.3"));"#, | ||
| ); | ||
| assert!( | ||
| !debug.contains(THROW_HELPER), | ||
| "un-aliased native import must construct, not throw:\n{debug}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn genuinely_unresolved_new_still_throws() { | ||
| // Positive control: the guard must still fire for a `new` on an identifier | ||
| // that resolves to no binding at all — the fix must not blanket-suppress it. | ||
| let debug = lower_debug(r#"const x = new Totally_Undefined_Constructor_Xyz();"#); | ||
| assert!( | ||
| debug.contains(THROW_HELPER), | ||
| "a genuinely unresolved `new` must still lower to the nameless \ | ||
| ReferenceError throw:\n{debug}" | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Assert the native construction path, not only the absence of the throw helper.
These assertions pass for any non-throwing lowering. A generic
Expr::Newthat still usesWj4could avoidReferenceErrorbut produce the empty placeholder, sob.addSubnet,s.run, orp.pipewould still fail at runtime. Assert the rewritten native export or constructor node in HIR, or add a compiled runtime check.Also applies to: 85-87
🤖 Prompt for AI Agents