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

Malformed source URLs in source map cache on Windows #1769

Closed
PaperStrike opened this issue May 21, 2022 · 6 comments · Fixed by #1771
Closed

Malformed source URLs in source map cache on Windows #1769

PaperStrike opened this issue May 21, 2022 · 6 comments · Fixed by #1771
Milestone

Comments

@PaperStrike
Copy link
Contributor

Search Terms

v8 coverage, coverage, source map windows

Expected Behavior

The sources field of the generated source map cache consists of file URLs (e.g., file:///D:/Example/index.ts).

Actual Behavior

Some generated sources field contain malformed file paths (e.g., d:/Example/index.ts).
I think it "malformed" because the disk letter, along with :, is treated like a protocol. At least, it should be in upper case. It will be better if it follows the file:/// prefix like the others.

Steps to reproduce the problem

  1. Enable Node.js coverage output by setting NODE_V8_COVERAGE
  2. Run any .ts file with ts-node
  3. Compare the sources fields in each produced coverage output file with each other.

Minimal reproduction

https://github.com/PaperStrike/ts-node-bug-repro
The workflow does basically the steps above, and prints the malformed URLs.

Specifications

  • ts-node version: 10.8.0
  • node version: 16.15.0
  • TypeScript version: 4.6.4
  • Operating system and version: Windows 11
@PaperStrike
Copy link
Contributor Author

PaperStrike commented May 22, 2022

This is very likely to be caused by that

  • Node.js uses sourcesToAbsolute to parse the paths in the sources field for the source map cache, and as you can see in L181, it relies on the URL constructor,
        return new URL(source, baseURL).href; // baseURL is guaranteed to be a file URL, file:///xxx
  • Node.js current implementation of the constructor will parse the Windows disk letter, along with :, as the protocol,
    > new URL('D:/Example').href
    'd:/Example'
    > new URL('D:/Example', 'file:///Whatever').href
    'd:/Example'

So I suggest we use either file URLs (PaperStrike@134bef2) or relative paths (PaperStrike@2a933ee).

@cspotcode
Copy link
Collaborator

Thanks for this. It'll probably take me a few days before I can properly review, merge, and publish this. The push to prepare and publish 10.8.0 ahead of TS 4.7 was time-consuming, so I've had to switch gears this week and play catch-up on some other stuff.

One thing that I want to make sure we get right:

Node's stack traces from ESM files use file URLs, but from CommonJS files, they use native paths. I remember writing some logic to ensure that we preserve this behavior even after we've source-mapped the stack frames.

Error
  at D:\this\is\a\commonjs\files.cjs:10:10
  at file:///d/this/is/a/esm/file.mjs:10:10

I want to be sure that, after we apply sourcemaps to a stack trace, the CJS files still use native paths, since that's the way vanilla node behaves.

@PaperStrike
Copy link
Contributor Author

@cspotcode Thank you, I hadn't noticed the stack traces of different types of files, and hadn't considered the impact of the PRs on them.

So just now I wrote a test case locally, and luckily found that neither #1770 nor #1771 affects the mapped stack traces. They all start with:

Error: check the output traces
    at createTraces (D:\PR\ts-node-bug-repro\commonjs.cts:2:15)
    at file:///D:/PR/ts-node-bug-repro/esm.mts:3:1

Not posting code for this simple test so as not to waste everyone's time.
Let's check for other possible side effects another day.

@cspotcode
Copy link
Collaborator

Thanks, I will also check our existing tests. They may already be checking for this in some way, and I see both of your pull requests have passing tests on CI.

@cspotcode cspotcode modified the milestones: 10.9.0 or 11.0.0, 10.8.1 May 25, 2022
@cspotcode
Copy link
Collaborator

I attempted the same test you did #1769 (comment)
and got the same result: it seems that paths/URLs in stack traces are correct with #1771.

Since URLs in sourcemaps seems more correct/more sane/more robust than using basename, and since it doesn't seem to be breaking anything, I'm going to merge that one.

crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this issue Jun 17, 2022
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [ts-node](https://typestrong.org/ts-node) ([source](https://github.com/TypeStrong/ts-node)) | devDependencies | patch | [`10.8.0` -> `10.8.1`](https://renovatebot.com/diffs/npm/ts-node/10.8.0/10.8.1) |

---

### Release Notes

<details>
<summary>TypeStrong/ts-node</summary>

### [`v10.8.1`](https://github.com/TypeStrong/ts-node/releases/tag/v10.8.1)

[Compare Source](TypeStrong/ts-node@v10.8.0...v10.8.1)

**Fixed**

-   Fixed [#&#8203;1769](TypeStrong/ts-node#1769): source URLs in source map cache were malformed on Windows, affecting code coverage reports ([#&#8203;1769](TypeStrong/ts-node#1769), [#&#8203;1771](TypeStrong/ts-node#1771)) [@&#8203;cspotcode](https://github.com/cspotcode)
-   Fixed [#&#8203;1778](TypeStrong/ts-node#1778): typechecker was erronously resolving imports from ESM files as if they were from CJS files ([#&#8203;1778](TypeStrong/ts-node#1778), [#&#8203;1782](TypeStrong/ts-node#1782)) [@&#8203;cspotcode](https://github.com/cspotcode)

https://github.com/TypeStrong/ts-node/milestone/14

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox.

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).

Co-authored-by: cabr2-bot <cabr2.help@gmail.com>
Reviewed-on: https://codeberg.org/Calciumdibromid/CaBr2/pulls/1393
Co-authored-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
Co-committed-by: Calciumdibromid Bot <cabr2_bot@noreply.codeberg.org>
@PaperStrike
Copy link
Contributor Author

PaperStrike commented Feb 26, 2023

@cspotcode Before we can test the impacts, could the file url/base name solution be made into an option? This problem is critical and easily reproducible when we use c8 with --exclude-after-remap on Windows, where c8 excludes every file that was processed by ts-node and shows empty results, which is caused by that the mapped paths do not matches the including paths (the case of the disk letter is different). A new option wouldn't be breaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants