Repository navigation
transpile: report a file: URL as a stripped script's sourceURL #800
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
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
11fbbe9
transpile: report a file: URL as a stripped script's sourceURL
colinhacks f66d3cb
transpile: spell the sourceURL for the host Node's escape band
colinhacks a36e3e6
transpile: cover the host-band plumbing end-to-end, record Cause 7 fixed
colinhacks 29fa6d8
wiki: state which Cause 7 symptom carries a committed test
colinhacks 44dbef4
Merge remote-tracking branch 'origin/main' into ts-sourceurl-fileurl
colinhacks 8d8e379
Merge remote-tracking branch 'origin/main' into ts-sourceurl-fileurl
colinhacks 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
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,132 @@ | ||
| //! A transpiled TypeScript file's script identity is the `file:` URL Node's own | ||
| //! type-stripping reports, not a bare filesystem path. | ||
| //! | ||
| //! nub appends a `//# sourceURL` to every transpiled body, and that value is what | ||
| //! the inspector reports as `Debugger.scriptParsed`'s `url` — the key editors and | ||
| //! DevTools match breakpoints against. Node reports the module's URL there | ||
| //! (`pathToFileURL(filename).href`, via `convertCJSFilenameToURL`); emitting the | ||
| //! path instead gave every `.ts` file a different identity under nub than under | ||
| //! `node`. Upstream pins the same contract in | ||
| //! `test/parallel/test-inspector-strip-types.js`, including `hasSourceURL`. | ||
| //! | ||
| //! Deliberately NOT asserted here: the rendered stack frame. nub runs with | ||
| //! `--enable-source-maps`, and Node's source-map frame formatter strips the | ||
| //! `file://` scheme off the mapped source by design | ||
| //! (`prepare_stack_trace.js`, `originalSourceNoScheme`), so a mapped frame shows a | ||
| //! bare path whatever the sourceURL says. The inspector URL is the surface this | ||
| //! bug is actually about. | ||
| //! | ||
| //! The fixture supplies its OWN expectation — `pathToFileURL(__filename).href` | ||
| //! plus URLs resolved against it — so the assertion is a differential against | ||
| //! Node's URL for the same files rather than path arithmetic repeated in the | ||
| //! test. Each fixture directory is run TWICE against a private cache directory: | ||
| //! the sourceURL is baked into the cached body, so a cold transform and a warm | ||
| //! cache hit are separate claims. | ||
|
|
||
| use std::path::{Path, PathBuf}; | ||
| use std::process::Command; | ||
|
|
||
| fn nub_binary() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_nub")) | ||
| } | ||
|
|
||
| fn host_node_usable() -> bool { | ||
| Command::new("node") | ||
| .arg("--version") | ||
| .output() | ||
| .is_ok_and(|out| out.status.success()) | ||
| } | ||
|
|
||
| /// A `.ts` entry that pulls in one dependency per module format, so all three | ||
| /// load paths (entry, `require` of `.cts`, `import` of `.mts`) are covered by one | ||
| /// process. `Debugger.enable` is posted before either dependency loads, which is | ||
| /// what makes their `scriptParsed` notifications observable. | ||
| const PROBE: &str = r#"const inspector = require("node:inspector"); | ||
| const { pathToFileURL } = require("node:url"); | ||
|
|
||
| const session = new inspector.Session(); | ||
| session.connect(); | ||
| const seen: Record<string, boolean> = {}; | ||
| session.on("Debugger.scriptParsed", ({ params }: any) => { | ||
| if (/\.[cm]?ts$/.test(params.url)) seen[params.url] = params.hasSourceURL; | ||
| }); | ||
| session.post("Debugger.enable", async () => { | ||
| require("./dep.cts"); | ||
| await import("./dep.mts"); | ||
| const self: string = pathToFileURL(__filename).href; | ||
| const expected: string[] = [self, new URL("dep.cts", self).href, new URL("dep.mts", self).href]; | ||
| console.log(JSON.stringify({ expected, seen })); | ||
| }); | ||
| "#; | ||
|
|
||
| /// Write the fixture into `dir`, run it, and assert every TypeScript file in the | ||
| /// graph reported Node's own URL for itself as its script identity. | ||
| fn assert_script_urls(dir: &Path, cache: &Path) { | ||
| std::fs::create_dir_all(dir).unwrap(); | ||
| std::fs::write(dir.join("package.json"), r#"{ "type": "commonjs" }"#).unwrap(); | ||
| std::fs::write(dir.join("probe.ts"), PROBE).unwrap(); | ||
| std::fs::write( | ||
| dir.join("dep.cts"), | ||
| "const y: number = 2;\nmodule.exports = y;\n", | ||
| ) | ||
| .unwrap(); | ||
| std::fs::write(dir.join("dep.mts"), "export const z: number = 3;\n").unwrap(); | ||
|
|
||
| for pass in ["cold", "warm"] { | ||
| let output = Command::new(nub_binary()) | ||
| .arg("probe.ts") | ||
| .current_dir(dir) | ||
| .env("XDG_CACHE_HOME", cache) | ||
| .output() | ||
| .expect("failed to spawn nub"); | ||
| assert!( | ||
| output.status.success(), | ||
| "nub exited {:?} ({pass}) in {}\nstderr: {}", | ||
| output.status, | ||
| dir.display(), | ||
| String::from_utf8_lossy(&output.stderr) | ||
| ); | ||
| let stdout = String::from_utf8_lossy(&output.stdout); | ||
| let parsed: serde_json::Value = serde_json::from_str(stdout.trim()) | ||
| .unwrap_or_else(|e| panic!("fixture must emit JSON ({pass}), got {stdout:?}: {e}")); | ||
|
|
||
| for expected in parsed["expected"].as_array().unwrap() { | ||
| let href = expected.as_str().unwrap(); | ||
| assert!( | ||
| href.starts_with("file:///"), | ||
| "({pass}) the fixture's own oracle must be a file: URL, got {href:?}" | ||
| ); | ||
| assert_eq!( | ||
| parsed["seen"][href], | ||
| serde_json::json!(true), | ||
| "({pass}) {href} must reach the inspector under Node's own URL for it, \ | ||
| with hasSourceURL set.\n reported: {}", | ||
| parsed["seen"] | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// The baseline contract, on a path with nothing to escape. | ||
| #[test] | ||
| fn transpiled_typescript_reports_its_file_url_to_the_inspector() { | ||
| if !host_node_usable() { | ||
| return; | ||
| } | ||
| let temp = tempfile::tempdir().unwrap(); | ||
| assert_script_urls(&temp.path().join("plain"), &temp.path().join("cache")); | ||
| } | ||
|
|
||
| /// The percent-encoding has to match Node's `pathToFileURL` too, not merely carry | ||
| /// a `file://` prefix: the fixture's expectations come from Node, so a naive | ||
| /// `format!("file://{path}")` passes the test above and fails here. Unix-only — | ||
| /// `%`, `#` and `?` are not usable in a Windows filename. | ||
| #[cfg(unix)] | ||
| #[test] | ||
| fn percent_encoded_path_matches_node() { | ||
| if !host_node_usable() { | ||
| return; | ||
| } | ||
| let temp = tempfile::tempdir().unwrap(); | ||
| assert_script_urls(&temp.path().join("a b%c#d?e"), &temp.path().join("cache")); | ||
|
pullfrog[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
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
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.