getsentry / sentry-javascript

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

Sourcemaps not deleted after upload for Sveltekit deployed to Vercel #12660

Open louiskhub opened 4 months ago

louiskhub commented 4 months ago

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/sveltekit

SDK Version

8.12.0

Framework Version

2.5.7

Link to Sentry event

No response

SDK Setup

In hooks.client.ts:

Sentry.init({
    dsn: PUBLIC_SENTRY_DSN,
    sampleRate: dev ? 0 : 1,
    tracesSampleRate: dev ? 0 : 1,
    replaysSessionSampleRate: environment === 'production' ? 0.1 : 0,
    replaysOnErrorSampleRate: environment === 'production' ? 0.1 : 0,
    integrations: environment === 'production' ? [Sentry.replayIntegration()] : [],
    debug: dev,
    environment,
    normalizeDepth: 10
});

In hooks.server.ts:

Sentry.init({
    dsn: PUBLIC_SENTRY_DSN,
    sampleRate: dev ? 0 : 1,
    tracesSampleRate: dev ? 0 : 1,
    debug: dev,
    environment,
    normalizeDepth: 10
});

My vite.config.ts:

import { paraglide } from '@inlang/paraglide-js-adapter-sveltekit/vite';
import { sentrySvelteKit } from '@sentry/sveltekit';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';

// @ts-expect-error mode is not defined in vite config
export default ({ mode }) => {
    return defineConfig({
        build: { sourcemap: true },
        plugins: [
            paraglide({
                project: './project.inlang',
                outdir: './src/lib/paraglide'
            }),
            sentrySvelteKit({
                sourceMapsUploadOptions: {
                    org: '4eign',
                    project: 'platform',
                    authToken: process.env.SENTRY_AUTH_TOKEN_USER,
                    sourcemaps: {
                        filesToDeleteAfterUpload: [ './**/*.map' ]
                    }
                },
                debug: mode === 'development',
                adapter: 'vercel'
            }),
            sveltekit()
        ],
        test: {
            include: ['src/**/*.{test,spec}.{js,ts}']
        }
    });
};

Steps to Reproduce

  1. Deploy to vercel (running vite build)
  2. Expect that sourcemaps have been deleted and are not visible in browser devtools
  3. Observe that:
    • they have not been deleted (still visible in vercel source output)
    • they are still visible in browser devtools

Expected Result

Deleted sourcemaps

Actual Result

Visible sourcemaps

s1gr1d commented 4 months ago

Hello, thanks for reporting! Can you please share a reproduction example? This can be a GitHub repository, Stackblitz, Codesandbox or similar. This will help us investigate the issue.

jerriclynsjohn commented 4 months ago

I'm facing a similar situation, same config as above but my build breaks with the following error

error during build:
Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(sentry-file-deletion-plugin) writeBundle
    at process.handleBeforeExit (file:///....................../sample-app/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/es/shared/node-entry.js:20111:28)
    at Object.onceWrapper (node:events:633:26)
    at process.emit (node:events:530:35)
    at process._0x24b8d6 [as emit] (/Users/...../.vscode/extensions/wallabyjs.console-ninja-1.0.326/out/buildHook/index.js:1:219762)
 ELIFECYCLE  Command failed with exit code 1.
failed to wait for command termination: exit status 1
louiskhub commented 4 months ago

Hello, thanks for reporting! Can you please share a reproduction example? This can be a GitHub repository, Stackblitz, Codesandbox or similar. This will help us investigate the issue.

Yes, will do, as soon as I find time.

jerriclynsjohn commented 4 months ago

Here's a repro repo https://github.com/jerriclynsjohn/sveltekit-sentry-integration.git

Sentry Sourcemaps upload + delete bug

Config + Error

export default defineConfig({
    plugins: [sentrySvelteKit({
        debug: true,
        sourceMapsUploadOptions: {
            telemetry: false,
            org: "sample-org",
            project: "sourcemap",
            authToken: process.env.SENTRY_AUTH_TOKEN,
            sourcemaps: {
                filesToDeleteAfterUpload: [`./svelte-kit/**/*.map`]
            }
        },
    }), sveltekit()]
});
[Sentry SvelteKit Plugin] Detected SvelteKit vercel adapter
[Source Maps Plugin] Enabeling source map generation
vite v5.3.2 building SSR bundle for production...
✓ 98 modules transformed.
[sentry-vite-plugin] Debug: Waiting for dependencies on generated files to be freed before deleting...
x Build failed in 4.23s
[Source Maps Plugin] Looking up source maps in /Users/............/CodeBase/TestBase/Svelte/sentry/upload-sourcemap/.svelte-kit/output
[Source Maps Plugin] Flattening source maps
[sentry-vite-plugin] Debug: No `sourcemaps.assets` option provided, falling back to uploading detected build artifacts.
> Found 24 files
> Analyzing 24 sources
> Analyzing completed in 0.026s
> Adding source map references
> Bundling completed in 0.021s
> Bundled 24 files for upload
> Bundle ID: 1c962ae5-34c3-5020-b2d5-a55e6b8eeb26
> Optimizing completed in 0.001s
> Uploading completed in 0.983s
> Uploaded files to Sentry
> Processing completed in 0.45s
> File upload complete (processing pending on server)
> Organization: zipper-school
> Project: sourcemap
> Release: 572ba3bce3414f329700f0f5ff7fa082
> Dist: None
> Upload type: artifact bundle

Source Map Upload Report
  Scripts
    ~/1179ab9f-8021-446d-8010-c8c41fcb5fff-3.js (sourcemap at internal.js.map, debug id 1179ab9f-8021-446d-8010-c8c41fcb5fff)
    ~/199572b9-3da7-4cd5-b860-323b3c15a2cf-0.js (sourcemap at _sentry-release-injection-file.js.map, debug id 199572b9-3da7-4cd5-b860-323b3c15a2cf)
    ~/307a417a-3e1a-4049-aa29-11ea8dd16b47-7.js (sourcemap at layout.svelte.js.map, debug id 307a417a-3e1a-4049-aa29-11ea8dd16b47)
    ~/46e0bb51-ffad-46eb-8d10-1f4ab4a931b8-4.js (sourcemap at ssr.js.map, debug id 46e0bb51-ffad-46eb-8d10-1f4ab4a931b8)
    ~/52f9c181-b447-45f0-9a06-131f2e3f614c-5.js (sourcemap at _server.js.map, debug id 52f9c181-b447-45f0-9a06-131f2e3f614c)
    ~/7c165ca5-709e-4d1b-b9d8-483a11152de2-9.js (sourcemap at _page.svelte.js.map, debug id 7c165ca5-709e-4d1b-b9d8-483a11152de2)
    ~/a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c-10.js (sourcemap at index.js.map, debug id a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c)
    ~/abf06399-4103-4360-b840-e4a6374ad440-8.js (sourcemap at _page.svelte.js.map, debug id abf06399-4103-4360-b840-e4a6374ad440)
    ~/bae66a35-3eec-43be-ab49-e3d845cb9f67-6.js (sourcemap at error.svelte.js.map, debug id bae66a35-3eec-43be-ab49-e3d845cb9f67)
    ~/bdc72d9c-6d59-400c-9a20-b852086412f2-1.js (sourcemap at exports.js.map, debug id bdc72d9c-6d59-400c-9a20-b852086412f2)
    ~/e5c5a9b4-9400-4c39-a906-86b84bf93c76-11.js (sourcemap at internal.js.map, debug id e5c5a9b4-9400-4c39-a906-86b84bf93c76)
    ~/f188f204-5d53-48f9-95dc-730a3a4e6d08-2.js (sourcemap at hooks.server.js.map, debug id f188f204-5d53-48f9-95dc-730a3a4e6d08)
  Source Maps
    ~/1179ab9f-8021-446d-8010-c8c41fcb5fff-3.js.map (debug id 1179ab9f-8021-446d-8010-c8c41fcb5fff)
    ~/199572b9-3da7-4cd5-b860-323b3c15a2cf-0.js.map (debug id 199572b9-3da7-4cd5-b860-323b3c15a2cf)
    ~/307a417a-3e1a-4049-aa29-11ea8dd16b47-7.js.map (debug id 307a417a-3e1a-4049-aa29-11ea8dd16b47)
    ~/46e0bb51-ffad-46eb-8d10-1f4ab4a931b8-4.js.map (debug id 46e0bb51-ffad-46eb-8d10-1f4ab4a931b8)
    ~/52f9c181-b447-45f0-9a06-131f2e3f614c-5.js.map (debug id 52f9c181-b447-45f0-9a06-131f2e3f614c)
    ~/7c165ca5-709e-4d1b-b9d8-483a11152de2-9.js.map (debug id 7c165ca5-709e-4d1b-b9d8-483a11152de2)
    ~/a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c-10.js.map (debug id a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c)
    ~/abf06399-4103-4360-b840-e4a6374ad440-8.js.map (debug id abf06399-4103-4360-b840-e4a6374ad440)
    ~/bae66a35-3eec-43be-ab49-e3d845cb9f67-6.js.map (debug id bae66a35-3eec-43be-ab49-e3d845cb9f67)
    ~/bdc72d9c-6d59-400c-9a20-b852086412f2-1.js.map (debug id bdc72d9c-6d59-400c-9a20-b852086412f2)
    ~/e5c5a9b4-9400-4c39-a906-86b84bf93c76-11.js.map (debug id e5c5a9b4-9400-4c39-a906-86b84bf93c76)
    ~/f188f204-5d53-48f9-95dc-730a3a4e6d08-2.js.map (debug id f188f204-5d53-48f9-95dc-730a3a4e6d08)
[sentry-vite-plugin] Info: Successfully uploaded source maps to Sentry
error during build:
Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(sentry-file-deletion-plugin) writeBundle
    at process.handleBeforeExit (file:///Users/.........../CodeBase/TestBase/Svelte/sentry/upload-sourcemap/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/es/shared/node-entry.js:20111:28)
    at Object.onceWrapper (node:events:633:26)
    at process.emit (node:events:530:35)
    at process._0x24b8d6 [as emit] (/Users/............/.vscode/extensions/wallabyjs.console-ninja-1.0.326/out/buildHook/index.js:1:219762)
 ELIFECYCLE  Command failed with exit code 1.

Config + Error for when using unstable_sentryVitePluginOptions

export default defineConfig({
    plugins: [sentrySvelteKit({
        debug: true,
        sourceMapsUploadOptions: {
            telemetry: false,
            org: "sample-org",
            project: "sourcemap",
            authToken: process.env.SENTRY_AUTH_TOKEN,
            unstable_sentryVitePluginOptions: {
                sourcemaps: {
                    filesToDeleteAfterUpload: [`./svelte-kit/**/*.map`]
                }
            }
        },
    }), sveltekit()]
});
[Sentry SvelteKit Plugin] Detected SvelteKit vercel adapter
[Source Maps Plugin] Enabeling source map generation
vite v5.3.2 building SSR bundle for production...
✓ 98 modules transformed.
[sentry-vite-plugin] Debug: Waiting for dependencies on generated files to be freed before deleting...
x Build failed in 3.33s
[Source Maps Plugin] Looking up source maps in /Users/........./CodeBase/TestBase/Svelte/sentry/upload-sourcemap/.svelte-kit/output
[Source Maps Plugin] Flattening source maps
[sentry-vite-plugin] Debug: No `sourcemaps.assets` option provided, falling back to uploading detected build artifacts.
> Found 24 files
> Analyzing 24 sources
> Analyzing completed in 0.007s
> Adding source map references
> Bundling completed in 0.037s
> Bundled 24 files for upload
> Bundle ID: b917013f-6693-5670-8d74-e121275809a4
> Optimizing completed in 0.001s
> Uploading completed in 1.084s
> Uploaded files to Sentry
> Processing completed in 0.421s
> File upload complete (processing pending on server)
> Organization: zipper-school
> Project: sourcemap
> Release: d7cda3db7ab04505a053615b536914f7
> Dist: None
> Upload type: artifact bundle

Source Map Upload Report
  Scripts
    ~/3016a615-3e45-40be-9134-eb4f9cf670b8-0.js (sourcemap at _sentry-release-injection-file.js.map, debug id 3016a615-3e45-40be-9134-eb4f9cf670b8)
    ~/307a417a-3e1a-4049-aa29-11ea8dd16b47-7.js (sourcemap at layout.svelte.js.map, debug id 307a417a-3e1a-4049-aa29-11ea8dd16b47)
    ~/46e0bb51-ffad-46eb-8d10-1f4ab4a931b8-4.js (sourcemap at ssr.js.map, debug id 46e0bb51-ffad-46eb-8d10-1f4ab4a931b8)
    ~/52f9c181-b447-45f0-9a06-131f2e3f614c-5.js (sourcemap at _server.js.map, debug id 52f9c181-b447-45f0-9a06-131f2e3f614c)
    ~/7c165ca5-709e-4d1b-b9d8-483a11152de2-9.js (sourcemap at _page.svelte.js.map, debug id 7c165ca5-709e-4d1b-b9d8-483a11152de2)
    ~/a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c-10.js (sourcemap at index.js.map, debug id a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c)
    ~/abf06399-4103-4360-b840-e4a6374ad440-8.js (sourcemap at _page.svelte.js.map, debug id abf06399-4103-4360-b840-e4a6374ad440)
    ~/b77c062d-e457-46f4-8c91-687510444041-3.js (sourcemap at internal.js.map, debug id b77c062d-e457-46f4-8c91-687510444041)
    ~/bae66a35-3eec-43be-ab49-e3d845cb9f67-6.js (sourcemap at error.svelte.js.map, debug id bae66a35-3eec-43be-ab49-e3d845cb9f67)
    ~/bdc72d9c-6d59-400c-9a20-b852086412f2-1.js (sourcemap at exports.js.map, debug id bdc72d9c-6d59-400c-9a20-b852086412f2)
    ~/e5c5a9b4-9400-4c39-a906-86b84bf93c76-11.js (sourcemap at internal.js.map, debug id e5c5a9b4-9400-4c39-a906-86b84bf93c76)
    ~/f188f204-5d53-48f9-95dc-730a3a4e6d08-2.js (sourcemap at hooks.server.js.map, debug id f188f204-5d53-48f9-95dc-730a3a4e6d08)
  Source Maps
    ~/3016a615-3e45-40be-9134-eb4f9cf670b8-0.js.map (debug id 3016a615-3e45-40be-9134-eb4f9cf670b8)
    ~/307a417a-3e1a-4049-aa29-11ea8dd16b47-7.js.map (debug id 307a417a-3e1a-4049-aa29-11ea8dd16b47)
    ~/46e0bb51-ffad-46eb-8d10-1f4ab4a931b8-4.js.map (debug id 46e0bb51-ffad-46eb-8d10-1f4ab4a931b8)
    ~/52f9c181-b447-45f0-9a06-131f2e3f614c-5.js.map (debug id 52f9c181-b447-45f0-9a06-131f2e3f614c)
    ~/7c165ca5-709e-4d1b-b9d8-483a11152de2-9.js.map (debug id 7c165ca5-709e-4d1b-b9d8-483a11152de2)
    ~/a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c-10.js.map (debug id a232d7b6-1ee5-4f34-b8cf-5dddc1a9a65c)
    ~/abf06399-4103-4360-b840-e4a6374ad440-8.js.map (debug id abf06399-4103-4360-b840-e4a6374ad440)
    ~/b77c062d-e457-46f4-8c91-687510444041-3.js.map (debug id b77c062d-e457-46f4-8c91-687510444041)
    ~/bae66a35-3eec-43be-ab49-e3d845cb9f67-6.js.map (debug id bae66a35-3eec-43be-ab49-e3d845cb9f67)
    ~/bdc72d9c-6d59-400c-9a20-b852086412f2-1.js.map (debug id bdc72d9c-6d59-400c-9a20-b852086412f2)
    ~/e5c5a9b4-9400-4c39-a906-86b84bf93c76-11.js.map (debug id e5c5a9b4-9400-4c39-a906-86b84bf93c76)
    ~/f188f204-5d53-48f9-95dc-730a3a4e6d08-2.js.map (debug id f188f204-5d53-48f9-95dc-730a3a4e6d08)
[sentry-vite-plugin] Info: Successfully uploaded source maps to Sentry
error during build:
Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
(sentry-file-deletion-plugin) writeBundle
    at process.handleBeforeExit (file:///Users/............/CodeBase/TestBase/Svelte/sentry/upload-sourcemap/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/es/shared/node-entry.js:20111:28)
    at Object.onceWrapper (node:events:633:26)
    at process.emit (node:events:530:35)
    at process._0x24b8d6 [as emit] (/Users/........../.vscode/extensions/wallabyjs.console-ninja-1.0.326/out/buildHook/index.js:1:219762)
 ELIFECYCLE  Command failed with exit code 1.
lforst commented 4 months ago

Can you guys try version 8.13.0 of the SDK? Thanks!

jerriclynsjohn commented 4 months ago

Can you guys try version 8.13.0 of the SDK? Thanks!

it is 8.13.0 as shown in the repro repo

"dependencies": {
        "@sentry/sveltekit": "^8.13.0",
        "dotenv": "^16.4.5"
    }
lforst commented 4 months ago

We'll take a look at this soon.

jerriclynsjohn commented 4 months ago

@lforst Any updates, mate? Let me know if we can play around with a workaround while you look into it.

lforst commented 4 months ago

We just found the cause for this but a fix might still take a while. My recommended workaround for now is to not use filesToDeleteAfterUpload and instead delete sourcemaps manually before deploying.

For the interested and for posterity: This issue is caused because the Sveltekit SDK moves the upload plugin into bundle close hook. This causes the created upload plugin task to never complete (because it hasn't run yet) when the deletion plugin runs - basically causing a deadlock.

jerriclynsjohn commented 4 months ago

We just found the cause for this but a fix might still take a while. My recommended workaround for now is to not use filesToDeleteAfterUpload and instead delete sourcemaps manually before deploying.

For the interested and for posterity: This issue is caused because the Sveltekit SDK moves the upload plugin into bundle close hook. This causes the created upload plugin task to never complete (because it hasn't run yet) when the deletion plugin runs - basically causing a deadlock.

I'm deploying using Vercel, is there a way to delete the source map there.

louiskhub commented 4 months ago

We just found the cause for this but a fix might still take a while. My recommended workaround for now is to not use filesToDeleteAfterUpload and instead delete sourcemaps manually before deploying. For the interested and for posterity: This issue is caused because the Sveltekit SDK moves the upload plugin into bundle close hook. This causes the created upload plugin task to never complete (because it hasn't run yet) when the deletion plugin runs - basically causing a deadlock.

I'm deploying using Vercel, is there a way to delete the source map there.

Same here, everything I tried did not really work out

Lms24 commented 4 months ago

If I'm not mistaken, you should be able to delete source maps in vercel as locally, for example by modifying the build script in your package.json. This example might not 1:1 represent your build command but something along the lines

"build": "vite build && rimraf **/*.js.map"

should do the trick for now.

je-movers-market commented 4 months ago

FWIW @Lms24 that doesn't seem to work, I'm using:

"build": "vite build && rm -rf **/*.js.map"

with a Vercel deployment, and the .svelte files are still visible in the dev tools. I'm assuming if I simply set sourcemap: false in my Vite config that'll disable any Sentry uploads completely?

Lms24 commented 3 months ago

@je-movers-market what's a bit weird to be honest. I would have expected this to work flawlessly. Sounds to me like vercel doesn't permit file operations 🤔 Do you get any errors from the rm -rf operation? @lforst might have come across this before.

If you want to completely disable emitting and uploading source maps, disable source map uploading and set sourcemap: false or don't set it at all (vite won't emit source maps in prod builds by default). Note that only setting sourcemap: false will not suffice because our plugin overrides this (something we might look into to making it smarter in the future).

lforst commented 3 months ago

Hi, sorry for having you left waiting. I will ping the Vercel folks about why it seems like sourcemaps can't be deleted.

I have thought about another solution for you though: If you want you can add a rewrite to your vercel.json that redirects any requests to .map files to a non-existing endpoint.

The rewrite in the vercel.json should probably look like this:

{
  "rewrites": [
    {
      "source": "/:anyPath*.map",
      "destination": "/404doesNotExist",
    }
  ]
}
rombrom commented 2 months ago

We ran into the same issue. @lforst's snippet almost works. Instead of rewrites you need redirects:

{
  "redirects": [
    {
      "source": "/:anyPath*.map",
      "destination": "/404doesNotExist",
    }
  ]
}
je-movers-market commented 2 months ago

Can confirm the above is working with redirects, although went with this as Vercel seems to now just use 'path' instead of 'anyPath' and 423 seems more correct here:

"redirects": [
        {
            "source": "/:path*.map",
            "destination": "/inaccessible",
            "statusCode": 423
        }
    ]
ProdanSergey commented 2 months ago

This suggestion https://github.com/getsentry/sentry-javascript/issues/12660#issuecomment-2312154392 seems to not solve the same problem in my case. I had to disable sourcemaps at all following the @lforst tip. I hope this will be resolved soon.

Lms24 commented 2 months ago

Hi, @ProdanSergey no updates at this time. To our knowledge, this unfortunately is still a limitation of the Vercel platform

dummdidumm commented 2 months ago

Running a script that uses Node methods to remove files does work for me. Running this ...

// rm.js
import fs from "fs";
import path from "path";
import { cwd } from "process";

function deleteSourceMaps(directoryPath) {
  const files = fs.readdirSync(directoryPath);
  files.forEach((file) => {
    const filePath = path.join(directoryPath, file);
    const stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      deleteSourceMaps(filePath);
    } else if (file.endsWith(".js.map")) {
      try {
        fs.unlinkSync(filePath);
        // console.log("File deleted:", filePath);
      } catch (err) {
        console.error("Error deleting file:", err);
      }
    }
  });
}

deleteSourceMaps(path.join(cwd(), ".svelte-kit"));
deleteSourceMaps(path.join(cwd(), ".vercel"));

... via "build": "vite build && node ./rm.js works for me

dummdidumm commented 2 months ago

It turns out that using rm -rf does work, but you need to a) explicitly enable the glob pattern support first and b) prepend a folder to the glob. So the following works: shopt -s globstar && rm -rf .vercel/**/*.js.map

Lms24 commented 2 months ago

Thanks for taking a look @dummdidumm!

ProdanSergey commented 1 month ago

Hi, @ProdanSergey no updates at this time. To our knowledge, this unfortunately is still a limitation of the Vercel platform

Hello, @Lms24. Is Vercel team aware of it?

lforst commented 1 month ago

@ProdanSergey the comment above is from a Vercel employee.

dollannn commented 3 weeks ago

Hello, I am having the same issue related to sourcemap deletion but not vercel. I am only getting this error while enabling sourcemap deletion with the same glob pattern as OP.

error during build:
17.27 Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit:
17.27 (sentry-file-deletion-plugin) writeBundle
17.27     at process.handleBeforeExit (file:///app/node_modules/.pnpm/rollup@4.19.0/node_modules/rollup/dist/es/shared/node-entry.js:20145:28)
17.27     at Object.onceWrapper (node:events:633:26)
17.27     at process.emit (node:events:530:35)

Also using sveltekit on windows, macos and linux

lforst commented 3 weeks ago

@dollannn please open a separate issue with reproduction steps. Thank you!