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

Source maps still don't seem to be working #54

Closed
Tracked by #11
kalepail opened this issue Jan 13, 2021 · 14 comments
Closed
Tracked by #11

Source maps still don't seem to be working #54

kalepail opened this issue Jan 13, 2021 · 14 comments

Comments

@kalepail
Copy link

kalepail commented Jan 13, 2021

Followed the guide in the README and the thread but I'm still getting errors when attempting to link to my source map.

Screen Shot 2021-01-12 at 8 34 49 PM

Maybe I'm missing or misunderstanding something still?

addEventListener('fetch', (event) => {
  const sentry = new Toucan({
    dsn: SENTRY_DSN,
    event,
    environment: 'development',
    release: 'pre-release',
    allowedCookies: /(.*)/,
    allowedHeaders: /(.*)/,
    allowedSearchParams: /(.*)/,
    rewriteFrames: {
      root: '/'
    }
  })

  event.respondWith(handleRequest(event, sentry))
})
const SentryWebpackPlugin = require('@sentry/webpack-plugin')

module.exports = {
  entry: './src/index.js',
  target: 'webworker',
  devtool: 'cheap-module-source-map',
  node: {
    fs: 'empty'
  },
  plugins: [
    new SentryWebpackPlugin({
      authToken: 'abc123',
      release: 'pre-release',
      org: 'abc',
      project: '123',
      include: './dist',
      urlPrefix: '/',
    }),
  ],
}
@robertcepa
Copy link
Owner

robertcepa commented Jan 18, 2021

Your config seems OK to me. Have you tried devtool: source-map?

@kalepail
Copy link
Author

I have yes, I can't use that directly though as the output is too large to upload.

✨  Built successfully, built project size is 1 MiB. ⚠️  Your built project has grown past the 1MiB size limit and may fail to deploy. ⚠️ 

Currently I'm getting around this with two different webpack files, one to build and upload the src map and one to just upload the worker code. Works fine.

@kalepail
Copy link
Author

kalepail commented Jan 18, 2021

// wrangler.toml

name = "name"
type = "webpack"
account_id = "account-id-abc123"
workers_dev = true
webpack_config = "webpack.config.js"

[env.srcmap]
webpack_config = "webpack.config.srcmap.js"
// webpack.config.js

const webpack = require('webpack')

module.exports = {
  entry: './src/index.js',
  target: 'webworker',
  node: {
    fs: 'empty'
  },
  module: {
    rules: [
      {
        test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'
      }
    ]
  },
  plugins: [
    new webpack.ExtendedAPIPlugin(),
  ]
}
// webpack.config.srcmap.js

const SentryPlugin = require('webpack-sentry-plugin')
const webpack = require('webpack')

module.exports = {
  entry: './src/index.js',
  target: 'webworker',
  devtool: 'source-map',
  node: {
    fs: 'empty'
  },
  module: {
    rules: [
      {
        test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'
      }
    ]
  },
  plugins: [
    new webpack.ExtendedAPIPlugin(),
    new SentryPlugin({
      apiKey: 'sentry-api-key-abc123',
      organization: 'org-abc123',
      project: 'project-abc123',
      deleteAfterCompile: true,
      release: (hash) => hash
    })
  ],
}
// package.json

{
  "private": true,
  "main": "./src/index.js",
  "scripts": {
    "start": "wrangler dev",
    "deploy": "wrangler build -e srcmap ; wrangler publish"
  },
  "dependencies": {
    ...
  },
  "devDependencies": {
    ...
  }
}
// index.js

...

const sentry = new Toucan({
  pkg,
  event,
  dsn: SENTRY_DSN,
  environment: isDev ? 'development' : 'production',
  release: __webpack_hash__,
  attachStacktrace: true,
  allowedCookies: /(.*)/,
  allowedHeaders: /(.*)/,
  allowedSearchParams: /(.*)/,
  rewriteFrames: {
    root: '/'
  }
})

...
% npm run deploy

> deploy
> wrangler build -e srcmap ; wrangler publish

✨  Built successfully, built project size is 1 MiB. ⚠️  Your built project has grown past the 1MiB size limit and may fail to deploy. ⚠️ 
✨  Built successfully, built project size is 315 KiB.
✨  Successfully published your script to
 https://project-name.workers.dev

@olizilla
Copy link
Contributor

olizilla commented Sep 29, 2021

i've been wrestling with source-maps today too, and I found that I could not get source maps to work with CF Workers + SentryWebpackPlugin + Toucan unless I removed the //# sourceMappingURL= link from the end of the js bundle by setting devtool: hidden-source-map in the webpack config.

We're using webpack 5, and wrangler 1.19 and have configured wrangler to treat the project as type = "javascript".

We configure the SentryWebpackPlugin like this:

  new SentryWebpackPlugin({
    release: gitRevisionPlugin.version(),
    include: './dist',
    urlPrefix: '/',
    org: '***',
    project: process.env.SENTRY_PROJECT,
    authToken: process.env.SENTRY_TOKEN
  })

and Toucan like this:

new Toucan({
    dsn: SENTRY_DSN,
    context: event,
    allowedHeaders: ['user-agent'],
    allowedSearchParams: /(.*)/,
    debug: false,
    rewriteFrames: {
      root: '/'
    },
    environment: ENV,
    release: VERSION,
    pkg
  })

which means we generate the bundle js and the source map in the dist dir, and both files get uploaded to sentry and then cloudflare on publish. The source maps show up on the release in Sentry but they just would not work until I switched the webpack devtool from devtool: source-map devtool: hidden-source-map, which only removes //# sourceMappingURL= link from the bundle.

Is that expected? I'd love to understand what is going on there. From the README for toucan it seem like that is not normal.

olizilla added a commit to web3-storage/web3.storage that referenced this issue Sep 30, 2021
Match up the release name reported during deploy with the release name reported at run time. This PR lines things up so releases appear in sentry like `web3-api@<semver>` which it automagically parses nicely. We currently we deploy as `<sort git sha>` but we report as `api-<semver>`.

see: https://sentry.io/organizations/protocol-labs-it/releases/web3-api%403.5.1/?project=5866408

- `SENTRY_RELEASE` env var is set during the build, and used for both creating the release at build time and on error reports so they get matched up and our sourcemap is used to unravel the stacktrace.
- Rename the bundle to be `worker.js` as that is what cloudflare renames (!?) it to on deploy, and errors are reported as coming from `worker.js` in production, so that has to match or else the source map is not applied.
- remove the `//# sourceMappingURL=...` link from the end of the js bundle by switching to `devtool: hidden-source-map` in the webpack config. I have no idea whey this is necessary. see: robertcepa/toucan-js#54 (comment)
- add commit info to releases in a way that sentry understands, and can match up to our repo with `setCommits: { auto: true }` see: https://github.com/getsentry/sentry-webpack-plugin#optionssetcommits

This fixes our double release counting in sentry and make source maps work for _much error readability_!

<img width="848" alt="Screenshot 2021-09-29 at 16 24 53" src="https://user-images.githubusercontent.com/58871/135340773-7f321748-4aae-401e-9dc9-c4ae5d19a696.png">

I have also connected up the repo in Sentry so we can see the commits that go in to the release.

<img width="364" alt="Screenshot 2021-09-29 at 21 09 37" src="https://user-images.githubusercontent.com/58871/135341267-d67026d7-881f-4f7b-a314-fbc102c75d37.png">


### Follow on work

- We can spare ourselves from cf worker upload errors by just not uploading our 4MB source map as part of deployment. Use the webpack remove files plugin to delete the source map after it has been uploaded to sentry, as we're just not using in cloudflare! would fix #499 see: #506 
- Add COMMITSHA and BRANCH env vars, and a `/version` endpoint to report it as nft.storage does. https://github.com/ipfs-shipyard/nft.storage/blob/7102ad5fc06d90a85027e40fa0aece3429b833e7/packages/api/src/index.js#L58-L64

License: (Apache-2.0 AND MIT)
Signed-off-by: Oli Evans <oli@tableflip.io>
@robertcepa
Copy link
Owner

@olizilla Do you have .map file along with your compiled .js file in your dist folder when you use devtool: source-map? If yes, and assuming your worker script is named worker.js, is your worker.js referencing the correct map file? It should look like this: sourceMappingURL=worker.js.map. Also check if both worker.js and worker.js.map show in Sentry under your project's source maps (or release artifacts if you use older version of Sentry):
Screen Shot 2021-10-02 at 10 37 38 AM

@xmflsct
Copy link

xmflsct commented Jan 29, 2022

Seems also not working with modules.

@olizilla
Copy link
Contributor

Aye, we just switched to esm modules on cloudflare, and our source maps are no longer showing up on Sentry.

@olizilla
Copy link
Contributor

I got it working with esm by using the rewriteFrames.iteratee property to strip a leading . character from the Stackframe.filename that we were seeing on errors sent from cloudflare to sentry.

rewriteFrames: {
      // strip . from start of the filename ./worker.mjs as set by cloudflare, to make absolute path `/worker.mjs`
      iteratee: (frame) => ({ ...frame, filename: frame.filename.substring(1) })
    },

happy sourcemap'd sentry error from an es module flavour worker

Screenshot 2022-02-25 at 22 01 21 copy

Before this change the raw JSON view of an error on Sentry showed that the filename property would be set to ./worker.js

unhappy sentry errror

  "exception": {
    "values": [
      {
        "type": "Error",
        "value": "A deliberate error!",
        "stacktrace": {
          "frames": [
            {
              "function": "async Object.fetch",
              "filename": "./worker.mjs",
              "abs_path": "./worker.mjs",

If I configure Toucan with rewriteFrames.root: '/' to prepend / to the filename, then Sentry shows, the filename and abs_path as "/./worker.mjs" and the only way to get Sentry to locate the uploaded bundle was to also set the urlPrefix option one uploading the files to Sentry to the same like urlPrefix: ~/./ which seemed kinda gross, but worked... I opted for just stripping the . from ./worker.mjs in the Toucan config as it seemed neater, as it also meant I could avoid specifying the urlPrefix when uploading them as the default is ~/ which works as, i learnt recently:

as ~/ is wildcard matcher for any protocol+host pair.
– getsentry/sentry-cli#894 (comment)

@ptim
Copy link

ptim commented Mar 1, 2022

I got it working with esm by using the rewriteFrames.iteratee property to strip a leading . character from the Stackframe.filename that we were seeing on errors sent from cloudflare to sentry.

Thanks so much - this fixed the issue for me, too 🙏

I'm building with esbuild (so no webpack plugin as mentioned in docs), so it was additionally necessary for me to set
--urlPrefix '/'

@olizilla
Copy link
Contributor

olizilla commented Jun 29, 2022

This broke again when we upgraded to wrangler v2... still investigating.

@olizilla
Copy link
Contributor

OK! It seems that Cloudflare does it's own bundling of your worker code.

For your source map to work you need to disable that new feature in your wrangler.toml by setting no_bundle = true.

🎩 Hat tip to @hugomrdias for the discovery in nftstorage/nft.storage#2094

@olizilla
Copy link
Contributor

olizilla commented Sep 6, 2022

We also had to revert the rewriteFrames config listed in #54 (comment) back to the simpler root: / form, and update our bundle name to be worker.js

rewriteFrames: {
  root: '/'
},

see: web3-storage/web3.storage#1838

@robertcepa
Copy link
Owner

robertcepa commented Dec 6, 2022

This should be a solved problem in 3.0.0. Mind giving it a shot? I added starters for various bundlers in https://github.com/robertcepa/toucan-js/tree/master/examples

In 3.0.0 you shouldn't need to use rewriteFrames or configure root in source map plugin, things should just work™

@gobengo
Copy link

gobengo commented Apr 14, 2023

This should be a solved problem in 3.0.0. Mind giving it a shot?

@robertcepa I updated our project to use toucan-js@3.x and emulated the wrangler-basic example (omitting wrangler.toml no_bundle i.e. no_bundle=false).
It seems to be working!

Thanks for all your advice. 🙏

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

No branches or pull requests

6 participants