-
-
Notifications
You must be signed in to change notification settings - Fork 158
fix(compile): namespace import of a CJS default-function links its direct call (#6586) #6601
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
Merged
proggeramlug
merged 2 commits into
PerryTS:main
from
proggeramlug:fix/6586-cjs-fjs-module
Jul 18, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
151 changes: 151 additions & 0 deletions
151
crates/perry/tests/issue_6586_namespace_cjs_default_import.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,151 @@ | ||
| //! Regression test for #6586: a namespace import of a CommonJS module whose | ||
| //! `module.exports` value is itself the export | ||
| //! (`module.exports = function equal(){}`) — TypeScript's | ||
| //! `esModuleInterop=false` interop shape — must bind the namespace local to the | ||
| //! default export so a DIRECT call of it (`equal(a, b)`) links. | ||
| //! | ||
| //! This is the wall that blocks `fast-json-stringify` (via `ajv`): ajv's | ||
| //! `lib/compile/resolve.ts` does | ||
| //! | ||
| //! ```ts | ||
| //! import * as equal from "fast-deep-equal" | ||
| //! import * as traverse from "json-schema-traverse" | ||
| //! // ... | ||
| //! traverse(schema, {allKeys: true}, (sch) => { /* ... */ }) | ||
| //! if (!equal(sch1, sch2)) throw ambiguos(ref) | ||
| //! ``` | ||
| //! | ||
| //! where both deps are pure CJS `module.exports = function`. Pre-fix, the | ||
| //! namespace binding had no `import_function_prefixes` entry, so the direct | ||
| //! calls fell through to bare `equal` / `traverse` externs and the link died | ||
| //! with `Undefined symbols: "_equal", "_traverse"`. A DEFAULT import of the | ||
| //! same module (`import equal from "..."`) always linked — the fix routes the | ||
| //! namespace whole-value binding to the module's `default` symbol the same way. | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
|
|
||
| fn perry_bin() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_perry")) | ||
| } | ||
|
|
||
| #[test] | ||
| fn namespace_import_of_cjs_default_function_links_and_calls() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let root = dir.path(); | ||
|
|
||
| std::fs::write( | ||
| root.join("package.json"), | ||
| r#"{ | ||
| "name": "ns-cjs-default", | ||
| "private": true, | ||
| "perry": { | ||
| "compilePackages": ["fakeequal", "faketraverse", "fakecls"], | ||
| "allow": { "compilePackages": ["fakeequal", "faketraverse", "fakecls"] } | ||
| } | ||
| }"#, | ||
| ) | ||
| .expect("write consumer package.json"); | ||
|
|
||
| // fast-deep-equal shape: a bare `module.exports = function`. | ||
| let equal_pkg = root.join("node_modules").join("fakeequal"); | ||
| std::fs::create_dir_all(&equal_pkg).expect("mkdir fakeequal"); | ||
| std::fs::write( | ||
| equal_pkg.join("package.json"), | ||
| r#"{ "name": "fakeequal", "version": "1.0.0", "main": "index.js" }"#, | ||
| ) | ||
| .expect("write fakeequal package.json"); | ||
| std::fs::write( | ||
| equal_pkg.join("index.js"), | ||
| "'use strict';\nmodule.exports = function equal(a, b) { return a === b; };\n", | ||
| ) | ||
| .expect("write fakeequal index.js"); | ||
|
|
||
| // json-schema-traverse shape: `module.exports = fn` PLUS a | ||
| // `module.exports.default = fn` self-reference (a named `default` on top of | ||
| // the CJS default value). | ||
| let traverse_pkg = root.join("node_modules").join("faketraverse"); | ||
| std::fs::create_dir_all(&traverse_pkg).expect("mkdir faketraverse"); | ||
| std::fs::write( | ||
| traverse_pkg.join("package.json"), | ||
| r#"{ "name": "faketraverse", "version": "1.0.0", "main": "index.js" }"#, | ||
| ) | ||
| .expect("write faketraverse package.json"); | ||
| std::fs::write( | ||
| traverse_pkg.join("index.js"), | ||
| "'use strict';\nfunction traverse(schema, cb) { cb(schema); return schema.n * 2; }\nmodule.exports = traverse;\nmodule.exports.default = traverse;\n", | ||
| ) | ||
| .expect("write faketraverse index.js"); | ||
|
|
||
| // A CJS default that is a CLASS (`module.exports = class Foo {}`) — | ||
| // exercises the class/metadata propagation for the namespace binding so | ||
| // `new ns(...)` resolves an ImportedClass entry instead of a phantom | ||
| // `perry_fn_<mod>__default` function wrapper. | ||
| let cls_pkg = root.join("node_modules").join("fakecls"); | ||
| std::fs::create_dir_all(&cls_pkg).expect("mkdir fakecls"); | ||
| std::fs::write( | ||
| cls_pkg.join("package.json"), | ||
| r#"{ "name": "fakecls", "version": "1.0.0", "main": "index.js" }"#, | ||
| ) | ||
| .expect("write fakecls package.json"); | ||
| std::fs::write( | ||
| cls_pkg.join("index.js"), | ||
| "'use strict';\nmodule.exports = class Box { constructor(x) { this.x = x; } doubled() { return this.x * 2; } };\n", | ||
| ) | ||
| .expect("write fakecls index.js"); | ||
|
|
||
| // Consumer imports both as namespaces and CALLS them directly — the exact | ||
| // ajv `resolve.ts` shape. | ||
| let entry = root.join("main.ts"); | ||
| std::fs::write( | ||
| &entry, | ||
| r#" | ||
| import * as equal from "fakeequal"; | ||
| import * as traverse from "faketraverse"; | ||
| import * as Box from "fakecls"; | ||
|
|
||
| const eq: boolean = (equal as any)({ a: 1 }, { a: 1 } as any) === false; | ||
| console.log("equal:", (equal as any)(1, 1), (equal as any)(1, 2)); | ||
|
|
||
| let seen = 0; | ||
| const doubled: number = (traverse as any)({ n: 21 }, (_s: any) => { seen++; }); | ||
| console.log("traverse:", doubled, "seen:", seen); | ||
| console.log("eq_ref:", eq); | ||
|
|
||
| // `new` through a namespace binding whose CJS default is a class. | ||
| const b: any = new (Box as any)(21); | ||
| console.log("box:", b.doubled()); | ||
| "#, | ||
| ) | ||
| .expect("write entry"); | ||
|
|
||
| let output = root.join("main_bin"); | ||
| let compile = Command::new(perry_bin()) | ||
| .current_dir(root) | ||
| .arg("compile") | ||
| .arg(&entry) | ||
| .arg("-o") | ||
| .arg(&output) | ||
| .output() | ||
| .expect("run perry compile"); | ||
| assert!( | ||
| compile.status.success(), | ||
| "perry compile failed (namespace-import-of-CJS-default link wall regressed?)\nstdout:\n{}\nstderr:\n{}", | ||
| String::from_utf8_lossy(&compile.stdout), | ||
| String::from_utf8_lossy(&compile.stderr) | ||
| ); | ||
|
|
||
| let run = Command::new(&output).output().expect("run compiled binary"); | ||
| let stdout = String::from_utf8_lossy(&run.stdout); | ||
| assert!( | ||
| run.status.success(), | ||
| "compiled binary failed\nstatus: {:?}\nstdout:\n{}\nstderr:\n{}", | ||
| run.status, | ||
| stdout, | ||
| String::from_utf8_lossy(&run.stderr) | ||
| ); | ||
| assert_eq!( | ||
| stdout, "equal: true false\ntraverse: 42 seen: 1\neq_ref: true\nbox: 42\n", | ||
| "namespace import of a CJS default function/class must resolve the default export" | ||
| ); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.