Skip to content
This repository was archived by the owner on Oct 29, 2026. It is now read-only.
This repository was archived by the owner on Oct 29, 2026. It is now read-only.

sentry-vite-plugin: enable source mapping for Web Worker source files #755

Closed

Description

@mattico

I'm using vite and the sentryVitePlugin. My app uses web workers which are imported with new Worker(new URL('./workerLineStats.ts', import.meta.url), { type: 'module' });. Vite understands that and outputs workerLineStats-LjpNa9wZ.js and a .map file.

I'm using DebugIDs for the main app, but those don't work for the web worker files. The debug output from sentry-vite-plugin includes:

[sentry-vite-plugin] Debug: Could not determine debug ID from bundle. This can happen if you did not clean your output folder before installing the Sentry plugin. File will not be source mapped: /workspaces/data-portal/apps/frontend/build/assets/workers/OverallStats.worker-BPsL_pTV.js

Alternatively, uploadLegacySourcemaps could be used for the web worker files, but I'm having trouble making that work as well, see below.


Edit: generalized issue to the root problem of sourcemaps for workers in Vite, rather than the specific suggested fix of uploadLegacySourcemaps globs.

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 9, 2025
  2. self-assigned this
    on Jul 10, 2025
  3. chargome commented on Jul 10, 2025

    @chargome
    Member

    @mattico would generally just providing the asset path work for you?

    uploadLegacySourcemaps: [
      'build/assets/'
    ]
    
  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 10, 2025
  5. mattico commented on Jul 10, 2025

    @mattico
    Author

    @mattico would generally just providing the asset path work for you?

    uploadLegacySourcemaps: [
      'build/assets/'
    ]
    

    I suppose it would, though it would also upload dozens of unnecessary sourcemaps for all the files which have DebugIDs.

  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 10, 2025
  7. Lms24 commented on Jul 11, 2025

    @Lms24
    Member

    Just jumping in here because we talked about this yesterday: The behaviour of the include entry (or just the string as in your case) for uploadLegacySourcemaps always was that users specify a directory and the plugin/sentry-cli would scan this directory, including sub-directories, for .map and .js files to process and upload (see doc here). So, in contrast to the newer assets option for debugIds, glob patterns were never supported (for better of for worse).

    The problem with changing this is that it could break user setups that rely on the specific behaviour, so we're not yet sure if we want to do this.

    In an effort to avoid this potential breakage: is there a way you could build just the worker into a specific sub directory that you can then point the plugin towards? I fully understand this isn't ideal for you and we usually don't want to tell you what to do. Just this case of combined debugID and legacy source maps upload, in the same directory, is a bit of an edge case (something, we admittedly didn't yet come across).

  8. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 11, 2025
  9. mattico commented on Jul 11, 2025

    @mattico
    Author

    The problem with changing this is that it could break user setups that rely on the specific behaviour, so we're not yet sure if we want to do this.

    Understandable. I'll be less prescriptive and let's refocus this issue on the root of the problem: sourcemaps for webworkers in Vite.

    In an effort to avoid this potential breakage: is there a way you could build just the worker into a specific sub directory that you can then point the plugin towards? I fully understand this isn't ideal for you and we usually don't want to tell you what to do. Just this case of combined debugID and legacy source maps upload, in the same directory, is a bit of an edge case (something, we admittedly didn't yet come across).

    I spent some time on this trying to modify Vite's build.rollupOptions, puzzling over why none of the worker files were processed by any of the hooks (assetFileNames, chunkFileNames, entryFileNames). Then I realized Vite has completely separate rollupOptions just for workers 🤦. This config does work to put all the workers output js/map files in a workers subdirectory:

      worker: {
        rollupOptions: {
          output: {
            entryFileNames: 'assets/workers/[name]-[hash].js',
          },
        }
      },

    However I still ran into a snag with uploadLegacySourcemaps. I can't get it to actually upload any sourcemaps. Here's my configuration:

    sentryVitePlugin({
      disable: false,
      org: 'beck-enterprises',
      project: 'data-portal-frontend',
      release: {
        // Uncomment these lines to test locally (with DP_RELEASE=1) without needing an auth token or creating a release
        create: false,
        finalize: false,
        name: `data-portal-frontend@${PACKAGE_VERSION}`,
        uploadLegacySourcemaps: [
          'build/assets/workers/',
          'build/assets/',
          'assets/',
          'assets/workers/',
          path.resolve(__dirname, './build/assets/workers/'),
          path.resolve(__dirname, './build/assets/'),
        ],
      },
      bundleSizeOptimizations: {
        excludeReplayShadowDom: true, // I don't think we have any custom elements/shadow dom
        excludeReplayIframe: true, // We don't have any iframes
      },
      debug: true,
    }),

    Am I missing something? I tried every path I could think of for the legacy source maps to make sure that wasn't the issue. No source maps are uploaded for any of the worker files, and the only mention of them in sentry-vite-plugin's output are the debugId messages:

    [sentry-vite-plugin] Debug: Could not determine debug ID from bundle. This can happen if you did not clean your output folder before installing the Sentry plugin. File will not be source mapped: /workspaces/data-portal/apps/frontend/build/assets/workers/LineRenderer.worker-Dk-urqO_.js
    

    Alternatively, perhaps sentry-vite-plugin could be changed to include DebugIDs in worker source files? Since vite processes worker files with a separate rollup invocation, maybe the sentry-vite-plugin needs to hook into that as well?

  10. 4 remaining items

  11. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 14, 2025
  12. Lms24 commented on Jul 14, 2025

    @Lms24
    Member

    Okay, so I have good and bad news:

    The good ones first:

    • I managed to reproduce the issue of Vite only uploading source maps of the main (thread) bundle but not uploading source maps of the worker
    • I could fix this part quite easily by adding the vite plugin twice to the vite config:
      export default defineConfig({
      build: {
        sourcemap: "hidden",
      }, 
      
      plugins: [
        sentryVitePlugin({
          org: "sentry-sdks",
          project: "javascript",
        }),
      ],
      
      worker: {
        plugins: () => [
          ...sentryVitePlugin({
            org: "sentry-sdks",
            project: "javascript",
          }),
        ],
      },
      });
      Can you give this a try and report if source maps from the worker get uploaded with this config for you?
      (note that the accepted type for worker.plugins must be a function of a flat array, hence I'm spreading the plugin array our sentryVitePlugin function returns).

    The bad news:

    • Unfortunately, due to worker and main thread not sharing the global object, the debugId from the main thread is not picked up from the worker when the main thread catches errors thrown from the worker.

    More good news:

    • You can work around this today, by also calling Sentry.init() from within your worker. There are some drawbacks (see link below) but at least you'll get source-mapped errors.
    • Your case gave me an idea on how we could improve the worker/main thread dynamics when the SDK is only initialized in the main thread. This would solve the drawbacks mentioned in the feature proposal.
      • I have a PoC of this working in the reproduction I linked above. Feel free to check it out. You can also implement this workaround yourself at the moment, with the caveat that this is largely untested and just something I threw together in 5 minutes 😅

    Would appreciate your thoughts as well as some feedback if anything posted here was helpful. Whatever we end up (not) doing, we should definitely update docs for the vite plugin to ensure that the worker case is documented better.

  13. mattico commented on Jul 14, 2025

    @mattico
    Author

    @Lms24

    I appreciate the help! I quite like the solution in #16974. Unfortunately I don't think I'll have time to try any of this out before I'm out of office for the next week or so. Will try after.

  14. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jul 14, 2025
  15. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jul 15, 2025
  16. timfish commented on Jul 15, 2025

    @timfish
    Contributor

    Is there some way we detect when vite creates a chunk for a worker? If there is, maybe we could include that debugId with the parent that loads the worker? That way the debugID would already be where we need it?

  17. timfish commented on Jul 15, 2025

    @timfish
    Contributor

    If we inject the same Sentry plugin instance into both the root and worker config, and have a way to track when it is called for a worker, we can track the debugIds for workers and include them with all other chunks.

    I've just realised it's not that simple because our debugID injection uses new Error().stack to get the script URL and that wouldn't work in this case 😭

    const sentryVite =   sentryVitePlugin({
        org: "sentry-sdks",
        project: "javascript",
    });
    
    export default defineConfig({
      plugins: [sentryVite],
      worker: {
        plugins: () => [sentryVite.worker], // <- we'll know when the plugin is used for a worker...
      },
    });
  18. Lms24 commented on Jul 17, 2025

    @Lms24
    Member

    I've just realised it's not that simple because our debugID injection uses new Error().stack to get the script URL and that wouldn't work in this case 😭

    yes, I think this is a hard limitation for us being smarter at build time 🤔

    If we inject the same Sentry plugin instance into both the root and worker config, and have a way to track when it is called for a worker, we can track the debugIds for workers and include them with all other chunks.

    Even if we could, I'm not sure if timing-wise, we know it early enough to inject anything into the main bundle(s) anymore 🤔 AFACT, the worker build is an isolated build, though happy to be proven wrong here :D

  19. Lms24 commented on Jul 25, 2025

    @Lms24
    Member

    @mattico once you're back: We've updated our web worker docs with the new webWorkerIntegration approach. In there we now also mention that the plugin needs to be passed to Vite's worker build.

    I'll close this issue for now. Let us know if this should be reopened.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions