Navigation Menu

Skip to content
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

Fix merging sourcemaps on Windows #14282

Merged
merged 1 commit into from Feb 17, 2022

Conversation

jridgewell
Copy link
Member

@jridgewell jridgewell commented Feb 17, 2022

Q                       A
Fixed Issues? Fixes #14277
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

On win32 machines, the sourceFileName uses backslash paths like
C:\foo\bar.js. But sourcemaps are always posix paths, so we need to
normalize to regular slashes before we can merge (else we won't find the
source associated with our input map).

This mirrors code done while generating the output map at

: (filename || this._opts.sourceFileName).replace(/\\/g, "/"),

On win32 machines, the sourceFileName uses backslash paths like
`C:\foo\bar.js`. But sourcemaps are always posix paths, so we need to
normalize to regular slashes before we can merge (else we won't find the
source associated with our input map).
This mirrors code done while generating the output map at
https://github.com/babel/babel/blob/5c2fcadc9ae34fd20dd72b1111d5cf50476d700d/packages/babel-generator/src/source-map.ts#L102
@jridgewell jridgewell added PR: Bug Fix 🐛 A type of pull request used for our changelog categories area: sourcemaps labels Feb 17, 2022
@babel-bot
Copy link
Collaborator

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

@jridgewell
Copy link
Member Author

jridgewell commented Feb 17, 2022

I've verified this using GH Actions to patch the node_modules file. Compare https://github.com/jridgewell/babel-7.17.3-sourcemaps-issue/runs/5225317890?check_suite_focus=true to https://github.com/jridgewell/babel-7.17.3-sourcemaps-issue/runs/5225382417?check_suite_focus=true (check the Run cat 'app\\*.map' step):

--- a.json	2022-02-16 19:47:50.000000000 -0500
+++ b.json	2022-02-16 19:47:50.000000000 -0500
@@ -1,10 +1,10 @@
 {
   "version": 3,
   "file": "babel-7.13.3-sourcemaps-issue.js",
-  "mappings": "yBAKIA,QAAQC,IAHA,iB",
+  "mappings": "yBAKEA,QAAQC,IAJD,iB",
   "sources": ["webpack://babel-7.17.3-sourcemaps-issue/./src/index.ts"],
   "sourcesContent": [
-    "\"use strict\";\nfunction welcome(user) {\n    return `Hello, ${user}!`;\n}\n(function () {\n    console.log(welcome(\"world\"));\n})();\n"
+    "function welcome(user: string): string {\r\n  return `Hello, ${user}!`;\r\n}\r\n\r\n(function () {\r\n  console.log(welcome(\"world\"));\r\n})();\r\n"
   ],
   "names": ["console", "log"],
   "sourceRoot": ""

It now correctly shows the original TypeScript code in the sourcesContent and the mappings now correctly points the output string to template literal at line 2 column 9.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks!

@nicolo-ribaudo nicolo-ribaudo merged commit ea1c44e into babel:main Feb 17, 2022
@jridgewell jridgewell deleted the windows-merge-map branch February 17, 2022 17:10
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 20, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 20, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: sourcemaps outdated A closed issue/PR that is archived due to age. Recommended to make a new issue 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.

[Bug]: Invalid sources in WebPack-generated map file
4 participants