Skip to content

fix: improve inputSourceMap URL handling#17999

Merged
JLHwung merged 5 commits into
babel:mainfrom
JLHwung:improve-input-source-map-handling
May 20, 2026
Merged

fix: improve inputSourceMap URL handling#17999
JLHwung merged 5 commits into
babel:mainfrom
JLHwung:improve-input-source-map-handling

Conversation

@JLHwung
Copy link
Copy Markdown
Contributor

@JLHwung JLHwung commented May 13, 2026

Q                       A
Fixed Issues? Improve handling of source map URL
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In this PR we improve the handling of source map url, especially for relative URL containing ...

For these URLs, Babel now will find the closest package.json directory or options.root, whichever is closer, and only read the source map file if it comes from that directory.

I will prepare a backport PR if we agree on the current design. The backport will be non-trivial as we have to switch find-up-simple for [email protected] with the stopAt support and then manually polyfill the syntax for Node.js 6.

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented May 14, 2026

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61556

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 14, 2026

Open in StackBlitz

commit: 05dd840

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves resolution of inputSourceMap URLs that contain .., by restricting reads to the closest ancestor package.json directory (or options.root, whichever is closer). The source-map reading logic is moved out of normalize-file.ts into a new platform-conditional module so the browser build can stub it out.

Changes:

  • New helper read-input-source-map-file.ts (with a browser variant that throws) that guards reads against escaping the package/root boundary using find-up-simple.
  • normalize-file.ts now delegates to that helper via a #transformation/read-input-source-map-file subpath import.
  • New test suite and many fixtures covering downward, upward-within-root/package, and upward-escaping cases.

Reviewed changes

Copilot reviewed 15 out of 40 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
packages/babel-core/src/transformation/read-input-source-map-file.ts New helper that resolves and validates the source-map path against package/root boundary
packages/babel-core/src/transformation/read-input-source-map-file-browser.ts Browser stub that throws
packages/babel-core/src/transformation/normalize-file.ts Replaces inline fs read with helper call
packages/babel-core/package.json Adds find-up-simple dep and subpath imports entry
packages/babel-core/test/input-source-map-resolution.js New test suite covering boundary scenarios
packages/babel-core/test/fixtures/input-source-map-resolution/** Fixture trees for each scenario
yarn.lock Lockfile update for find-up-simple@^1.0.1
Files not reviewed (3)
  • packages/babel-core/test/fixtures/input-source-map-resolution/invalid-relative-up-across-root/root/sub/input.js: Language not supported
  • packages/babel-core/test/fixtures/input-source-map-resolution/relative-down-into-node-modules/input.js: Language not supported
  • packages/babel-core/test/fixtures/input-source-map-resolution/relative-down/input.js: Language not supported

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

"browser": "./src/transformation/read-input-source-map-file-browser.ts",
"default": "./src/transformation/read-input-source-map-file.ts"
},
"types": "./lib/index.d.ts",
Copy link
Copy Markdown
Contributor Author

@JLHwung JLHwung May 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types maps to ./lib/index.d.ts because the bundle-dts step bundles every declarations into lib/index.d.ts so the published file does not contain ./lib/transformation/read-input-source-map-file.d.ts.

Comment thread packages/babel-core/src/transformation/read-input-source-map-file.ts Outdated
Comment on lines +14 to +19
const inputMapPath = path.resolve(path.dirname(filename), inputMapURL);
if (inputMapURL.includes("..")) {
const inputPackageJSONPath = findUpSync("package.json", {
cwd: path.dirname(filename),
stopAt: root,
});
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value of root is cwd and generally the input filename will be within cwd, or within root for monorepo's usage. If user misconfigured root, that means they are intentionally compiling something out of the monorepo boundary, in this case I don't think we should do anything.

Comment on lines +9 to +15
function getInputMapPath(
filename: string,
root: string,
inputMapURL: string,
): string | null {
const inputMapPath = path.resolve(path.dirname(filename), inputMapURL);
if (inputMapURL.includes("..")) {
Comment thread packages/babel-core/test/input-source-map-resolution.js
Comment thread packages/babel-core/test/input-source-map-resolution.js
relativeToInputFileDir.startsWith("..") ||
path.isAbsolute(relativeToInputFileDir)
) {
const inputPackageJSONPath = findUpSync("package.json", {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Babel 7, can we manually iterate up the file system?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to start from https://github.com/sindresorhus/find-up-simple/blob/f10133c55dcbf36f84a246c6f1bbfed178dcb774/index.js#L36 and shrink functions that we don't need, so it would be something like find-up-simple-node-6. We could manually iterate, too but then we will have to add extra tests for the different implementation.

@JLHwung JLHwung force-pushed the improve-input-source-map-handling branch from b4bc4d6 to 05dd840 Compare May 15, 2026 13:53
Copy link
Copy Markdown
Member

@liuxingbaoyu liuxingbaoyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .map extension might be an exception to skip the check. (Let's see if anyone complains about this check.)

Comment on lines +35 to +37
debug(
`discarding input sourcemap "${inputMapPath}" outside of package root "${inputFileRoot}"`,
);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users seem to be unable to see this message by default, which I'm worried will cause confusion.

Copy link
Copy Markdown
Contributor Author

@JLHwung JLHwung May 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The input sourcemap merging errors, such as invalid source map format, are printed to the debug as well, since this branch will only be reached when there is parent traversal in the source map url, which is a pretty rare use case, I think it is fine to leave it as-is. We can improve in the future when there is user feedback.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall we previously discussed printing out invalid source map warning messages in Babel 8. :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I think that should be addressed in another PR.

@jridgewell jridgewell dismissed their stale review May 19, 2026 14:55

Resolved confusion.

@JLHwung JLHwung merged commit 25f2e4a into babel:main May 20, 2026
56 checks passed
@JLHwung JLHwung deleted the improve-input-source-map-handling branch May 20, 2026 18:02
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories and removed PR: Needs Docs labels May 20, 2026
@JLHwung
Copy link
Copy Markdown
Contributor Author

JLHwung commented May 20, 2026

Docs PR is ready, ptal: babel/website#3204

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants