|
| 1 | +//! Regression test for #8749: a Node builtin named import used from a |
| 2 | +//! `compilePackages` dependency must retain its runtime binding. |
| 3 | +//! |
| 4 | +//! `@hono/node-server` imports `createServer` from `http`, selects it through a |
| 5 | +//! module-scope fallback (`options.createServer || createServerHTTP`), and calls |
| 6 | +//! the selected function later from `serve()`. App-level imports already |
| 7 | +//! worked; the binding was lost specifically while compiling the dependency. |
| 8 | +
|
| 9 | +use std::path::PathBuf; |
| 10 | +use std::process::Command; |
| 11 | + |
| 12 | +fn perry_bin() -> PathBuf { |
| 13 | + PathBuf::from(env!("CARGO_BIN_EXE_perry")) |
| 14 | +} |
| 15 | + |
| 16 | +#[test] |
| 17 | +fn builtin_named_import_survives_module_scope_fallback_in_compiled_package() { |
| 18 | + let dir = tempfile::tempdir().expect("tempdir"); |
| 19 | + let root = dir.path(); |
| 20 | + |
| 21 | + std::fs::write( |
| 22 | + root.join("package.json"), |
| 23 | + r#"{ |
| 24 | + "name": "compiled-builtin-import-consumer", |
| 25 | + "private": true, |
| 26 | + "type": "module", |
| 27 | + "perry": { |
| 28 | + "compilePackages": ["fake-node-server"], |
| 29 | + "allow": { "compilePackages": ["fake-node-server"] } |
| 30 | + } |
| 31 | +}"#, |
| 32 | + ) |
| 33 | + .expect("write consumer package.json"); |
| 34 | + |
| 35 | + let pkg = root.join("node_modules").join("fake-node-server"); |
| 36 | + std::fs::create_dir_all(&pkg).expect("mkdir fake-node-server"); |
| 37 | + std::fs::write( |
| 38 | + pkg.join("package.json"), |
| 39 | + r#"{ |
| 40 | + "name": "fake-node-server", |
| 41 | + "version": "1.0.0", |
| 42 | + "type": "module", |
| 43 | + "exports": "./index.mjs" |
| 44 | +}"#, |
| 45 | + ) |
| 46 | + .expect("write dependency package.json"); |
| 47 | + std::fs::write( |
| 48 | + pkg.join("index.mjs"), |
| 49 | + r#" |
| 50 | +import { createServer as createServerHTTP } from "http"; |
| 51 | +import { createServer as createServerNodeHTTP } from "node:http"; |
| 52 | +
|
| 53 | +const options = {}; |
| 54 | +const selectedHTTP = options.createServer || createServerHTTP; |
| 55 | +const selectedNodeHTTP = options.createServer || createServerNodeHTTP; |
| 56 | +
|
| 57 | +export function inspectBindings() { |
| 58 | + const serverHTTP = selectedHTTP({}, () => {}); |
| 59 | + const serverNodeHTTP = selectedNodeHTTP({}, () => {}); |
| 60 | + return [ |
| 61 | + typeof createServerHTTP, |
| 62 | + typeof createServerNodeHTTP, |
| 63 | + typeof selectedHTTP, |
| 64 | + typeof selectedNodeHTTP, |
| 65 | + typeof serverHTTP.listen, |
| 66 | + typeof serverNodeHTTP.listen, |
| 67 | + ].join(","); |
| 68 | +} |
| 69 | +"#, |
| 70 | + ) |
| 71 | + .expect("write compiled dependency"); |
| 72 | + |
| 73 | + let entry = root.join("main.ts"); |
| 74 | + std::fs::write( |
| 75 | + &entry, |
| 76 | + r#" |
| 77 | +import { inspectBindings } from "fake-node-server"; |
| 78 | +console.log(inspectBindings()); |
| 79 | +process.exit(0); |
| 80 | +"#, |
| 81 | + ) |
| 82 | + .expect("write entry"); |
| 83 | + |
| 84 | + let output = root.join("main_bin"); |
| 85 | + let compile = Command::new(perry_bin()) |
| 86 | + .current_dir(root) |
| 87 | + .arg("compile") |
| 88 | + .arg(&entry) |
| 89 | + .arg("-o") |
| 90 | + .arg(&output) |
| 91 | + .arg("--no-cache") |
| 92 | + .output() |
| 93 | + .expect("run perry compile"); |
| 94 | + assert!( |
| 95 | + compile.status.success(), |
| 96 | + "perry compile failed\nstdout:\n{}\nstderr:\n{}", |
| 97 | + String::from_utf8_lossy(&compile.stdout), |
| 98 | + String::from_utf8_lossy(&compile.stderr) |
| 99 | + ); |
| 100 | + |
| 101 | + let run = Command::new(&output).output().expect("run compiled binary"); |
| 102 | + assert!( |
| 103 | + run.status.success(), |
| 104 | + "compiled binary failed\nstatus: {:?}\nstdout:\n{}\nstderr:\n{}", |
| 105 | + run.status, |
| 106 | + String::from_utf8_lossy(&run.stdout), |
| 107 | + String::from_utf8_lossy(&run.stderr) |
| 108 | + ); |
| 109 | + assert_eq!( |
| 110 | + String::from_utf8_lossy(&run.stdout), |
| 111 | + "function,function,function,function,function,function\n", |
| 112 | + "both builtin spellings must stay bound through the dependency's module global" |
| 113 | + ); |
| 114 | +} |
0 commit comments