getsentry / sentry-javascript

Official Sentry SDKs for JavaScript
https://sentry.io
MIT License
7.98k stars 1.57k forks source link

NuxtJS: Source map deletion after upload #13632

Closed SVV-team closed 2 weeks ago

SVV-team commented 1 month ago

Problem Statement

If source map upload enabled, in production we can see source code (Google Chrome > DevTools > Source) (This is not good)

When source map disable its look like

Solution Brainstorm

Maybe we can generate source map to another folder using separate command?

chargome commented 1 month ago

Hey @SVV-team, assuming you are using the @sentry/nuxt sdk you can make use of https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#sourcemapsfilestodeleteafterupload

SVV-team commented 1 month ago

Hey @SVV-team, assuming you are using the @sentry/nuxt sdk you can make use of https://www.npmjs.com/package/@sentry/vite-plugin/v/2.14.2#sourcemapsfilestodeleteafterupload

Deletion working, but.... Used config

 sentry: {
    debug: true,
    sourceMapsUploadOptions: {
      sourcemaps: {
        filesToDeleteAfterUpload: ['.output/**/*.map'],
      }
    },
  },

After deploy to production sentry handle many errors ENOENT: no such file or directory, open '.output/public/content/B8u3bkn1.js.map'

Image

andreiborza commented 1 month ago

hi @SVV-team, could you please link to one of those issues?

SVV-team commented 1 month ago

hi @SVV-team, could you please link to one of those issues?

https://svv-team.sentry.io/share/issue/db391adafe1148bda0acdf3c9f36fe61/

chargome commented 1 month ago

@SVV-team would mind sharing your nuxt.config.ts?

SVV-team commented 1 month ago

@SVV-team would mind sharing your nuxt.config.ts?

export default defineNuxtConfig({
  modules: [
    '@pinia/nuxt',
    '@nuxtjs/i18n', 
    '@nuxtjs/robots',
    '@nuxt/image',
    '@nuxtjs/device',
    'nuxt-gtag',
    'nuxt-schema-org',
    'nuxt-multi-tenancy',
    '@nuxt/scripts',
    '@nuxt/icon',
    'nuxt-rating',
    '@sentry/nuxt/module',
  ],

  sentry: {
    debug: true,
    sourceMapsUploadOptions: {
      org: '',
      project: '',
      sourcemaps: {
        filesToDeleteAfterUpload: ['.output/**/*.map'],
      },
      authToken:'',
    },
  },

  runtimeConfig: {
    public: {
    ....
    },
  }
})
s1gr1d commented 1 month ago

When disabling sourcemaps, the content folder is still visible in the devtools. I don't know what content does exactly under the hood but can you try not deleting every sourcemap in .output? Does it make a difference if you keep content?

afdolriski commented 3 weeks ago

same here, looks like there are reference to source map from the generated file: 1RmAovpz.js:

import{aO as t,aC as n,aD as o,aR as r}from"./DIuzRh7i.js";(function(){try.....
//# sourceMappingURL=1RmAovpz.js.map
chargome commented 2 weeks ago

@afdolriski can you paste your nuxt config here?

rymdkapten commented 2 weeks ago

Hi,

running into a similiar issue, but using Nuxt 3, I believe the issue is the same though. The sourcemap URL is appended to each file as such:

//# sourceMappingURL=filename.js.map

There is an option in both nitro and vite that can be set to hide these; in case you don't want to release them publicly.

For vite it is set like such in nuxt.config.ts:

vite: {
  build: {
    sourcemap: 'hidden',
  },
}

And for nitro (note the capital M here):

nitro: {
  sourceMap: 'hidden',
},

However, it seems like the @sentry/nuxt module overwrites both these options.

Vite is overwritten here: https://github.com/getsentry/sentry-javascript/blob/4c0c25cbfbbbb9c71fcf1a7a5a62cf82c086457e/packages/nuxt/src/vite/sourceMaps.ts#L22

While nitro is not exactly overwritten, but another output option is added here: https://github.com/getsentry/sentry-javascript/blob/4c0c25cbfbbbb9c71fcf1a7a5a62cf82c086457e/packages/nuxt/src/vite/sourceMaps.ts#L46

Unsure what exactly happens next, but it seems like since the sourceMappingURL reference exist, Nuxt will add them in the route table (or const assets) in .output\server\chunks\runtime.mjs.

If we then decide to remove or exclude the sourcemap-files from the release, it causes some issues when e.g. inspecting the JS-files in browser dev tools since this will cause it to try to request the referenced sourcemap-URL. Since the file does not exist on the server, the runtime throws a nasty ENOENT: no such file or directory, open '/home/site/wwwroot/.output/public/_nuxt/filename.js.map'.

Perhaps a solution would be to first check if the user has already set the sourcemaps options and respect them before setting them to true?

andreiborza commented 2 weeks ago

@rymdkapten thanks for investigating. We'll discuss an approach forward wrt to forcing sourcemap creation to true. I personally think hidden is a better default, but we'll discuss respecting user settings fully.

rymdkapten commented 2 weeks ago

@andreiborza No worries, I just hope it helps! I do agree that hidden seem to be a better default.

Lms24 commented 2 weeks ago

Hey everyone, we discussed this internally today and opened https://github.com/getsentry/sentry-javascript/issues/13993 to track how we will generate source maps going forward. TL;DR: We'll no longer override explicitly disabled source maps. If no source map generation option is set, we'll enable hidden source maps and delete them afterwards.

s1gr1d commented 2 weeks ago

Closing this as this was fixed in this PR: https://github.com/getsentry/sentry-javascript/pull/14020

Feel free to re-open this issue if you are experiencing problems. This will be released with 8.35.0