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

[wrangler] feat: source map logged strings #4423

Merged
merged 2 commits into from
Dec 15, 2023
Merged

Conversation

mrbbot
Copy link
Contributor

@mrbbot mrbbot commented Nov 10, 2023

Fixes #3383 and closes #3739.

What this PR solves / how to test:

Previously, Wrangler would only apply source mapping to uncaught exceptions. This meant if you caught an exception and logged its stack trace, the call sites would reference built JavaScript files as opposed to source files. This change looks for stack traces in logged messages, and tries to source map them.

Note source mapping is only applied when outputting logs. Error#stack does not return a source mapped stack trace. This means the actual runtime value of new Error().stack and the output from console.log(new Error().stack) may be different.

To test this, run the following worker with wrangler dev:

interface Env {
}

export default <ExportedHandler<Env>>{
  fetch() {
    console.log(new Error("1"));
    console.log(new Error("2").stack);
    console.log({ error: new Error("3").stack });
  }
}

Note how source mapping is applied in all cases.

Author has addressed the following:

  • Tests
    • Included
    • Not necessary because:
  • Changeset (Changeset guidelines)
    • Included
    • Not necessary because:
  • Associated docs
    • Issue(s)/PR(s):
    • Not necessary because:

Note for PR author:

We want to celebrate and highlight awesome PR review! If you think this PR received a particularly high-caliber review, please assign it the label highlight pr review so future reviewers can take inspiration and learn from it.

Copy link

changeset-bot bot commented Nov 10, 2023

🦋 Changeset detected

Latest commit: fac2f99

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Nov 10, 2023

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7210693986/npm-package-wrangler-4423

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/7210693986/npm-package-wrangler-4423

Or you can use npx with this latest build directly:

npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7210693986/npm-package-wrangler-4423 dev path/to/script.js
Additional artifacts:
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7210693986/npm-package-miniflare-4423
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7210693986/npm-package-cloudflare-pages-shared-4423
npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/7210693986/npm-package-create-cloudflare-4423

Note that these links will no longer work once the GitHub Actions artifact expires.


wrangler@3.20.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20231030.4
workerd 1.20231030.0 1.20231030.0
workerd --version 1.20231030.0 2023-10-30

|

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

@RamIdeas RamIdeas force-pushed the hide-workerd-logs branch 4 times, most recently from 67b8104 to 664e4a4 Compare November 20, 2023 15:50
Base automatically changed from hide-workerd-logs to main November 20, 2023 16:42
@mrbbot mrbbot force-pushed the bcoll/source-mapped-strings branch from 1bf14b4 to 70d6a5d Compare November 28, 2023 10:32
Copy link

codecov bot commented Nov 28, 2023

Codecov Report

Merging #4423 (0fd9b75) into main (6639468) will decrease coverage by 0.12%.
Report is 2 commits behind head on main.
The diff coverage is 36.61%.

❗ Current head 0fd9b75 differs from pull request most recent head fac2f99. Consider uploading reports for the commit fac2f99 to get more accurate results

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4423      +/-   ##
==========================================
- Coverage   75.55%   75.44%   -0.12%     
==========================================
  Files         242      242              
  Lines       12909    12957      +48     
  Branches     3320     3331      +11     
==========================================
+ Hits         9753     9775      +22     
- Misses       3156     3182      +26     
Files Coverage Δ
packages/wrangler/src/dev/miniflare.ts 60.93% <66.66%> (+0.18%) ⬆️
packages/wrangler/src/dev/inspect.ts 6.99% <33.33%> (+0.56%) ⬆️
packages/wrangler/src/sourcemap.ts 23.30% <35.38%> (+21.60%) ⬆️

... and 1 file with indirect coverage changes

@mrbbot mrbbot force-pushed the bcoll/source-mapped-strings branch from 70d6a5d to af01853 Compare November 28, 2023 10:42
@mrbbot mrbbot changed the title [WIP] feat: source map strings [wrangler] feat: source map strings Nov 28, 2023
@mrbbot mrbbot changed the title [wrangler] feat: source map strings [wrangler] feat: source map logged strings Nov 28, 2023
@mrbbot mrbbot marked this pull request as ready for review November 28, 2023 10:43
@mrbbot mrbbot requested a review from a team as a code owner November 28, 2023 10:43
@mrbbot
Copy link
Contributor Author

mrbbot commented Nov 28, 2023

Looks like source mapping is slightly broken on Windows. 😕 Will investigate this when I'm next in the office.

@petebacondarwin
Copy link
Contributor

Looks like source mapping is slightly broken on Windows. 😕 Will investigate this when I'm next in the office.

Equally I could take a look for you when I am next at home.

Co-authored-by: Steve Lam <stevyo99@yahoo.ca>
@mrbbot mrbbot force-pushed the bcoll/source-mapped-strings branch from fa9de6b to 0fd9b75 Compare December 14, 2023 13:20
Use `@cspotcode/source-map-support` instead of `source-map-support`.
This has some additional fixes for resolving paths on Windows, and is
used by `ts-node`.
@mrbbot mrbbot force-pushed the bcoll/source-mapped-strings branch from 0fd9b75 to fac2f99 Compare December 14, 2023 13:52
@mrbbot mrbbot closed this Dec 14, 2023
@mrbbot mrbbot reopened this Dec 14, 2023
@mrbbot
Copy link
Contributor Author

mrbbot commented Dec 14, 2023

Fixed this on Windows with fac2f99. It looks like source-map-support has a few bugs resolving paths on Windows, and isn't really maintained anymore. I've switched to @cspotcode/source-map-support which has the required fixes, and is used by ts-node. 👍

@BrandonNoad
Copy link

@mrbbot will these changes help out at all with logging stack traces for Cloudflare Pages apps (deployed with wrangler pages deploy)?

e.g. #4234

@mrbbot
Copy link
Contributor Author

mrbbot commented Dec 15, 2023

@BrandonNoad unfortunately not. Deployed Workers and Pages Functions do not support source mapping yet.

@petebacondarwin petebacondarwin added the e2e Run e2e tests on a PR label Dec 15, 2023
Copy link
Contributor

@petebacondarwin petebacondarwin left a comment

Choose a reason for hiding this comment

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

LGTM - just added e2e test label.

@petebacondarwin
Copy link
Contributor

E2Es are green and I tried this out on a local Windows machine. Ship it!

@mrbbot mrbbot merged commit a94ef57 into main Dec 15, 2023
22 checks passed
@mrbbot mrbbot deleted the bcoll/source-mapped-strings branch December 15, 2023 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
e2e Run e2e tests on a PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Wrangler 3 and Wrangler 2 both point to incorrect source code in stack traces
3 participants