Skip to content

[nextjs] deleteSourcemapsAfterUpload leaves a source map it fails to delete in the build output and logs it at debug level only #26960

Backlog

Description

@YevheniiKotyrlo

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

11.20.0 (develop at 489a157 has the same tree)

Framework Version

Next 16.4.6 (Turbopack)

Link to Sentry event

No response

Reproduction Example/SDK Setup

https://gist.github.com/YevheniiKotyrlo/b47f3e4a1c33e34feb71ee7db2361730

The next.config.mjs the reproducer writes:

import { chmodSync, readdirSync, writeFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { withSentryConfig } from '@sentry/nextjs/config';

const nextConfig = {
  productionBrowserSourceMaps: true,
  compiler: {
    runAfterProductionCompile: ({ distDir, projectDir }) => {
      const chunks = resolve(projectDir, distDir, 'static', 'chunks');
      const maps = readdirSync(chunks).filter(name => name.endsWith('.map'));
      chmodSync(chunks, 0o555);
      writeFileSync(join(projectDir, 'locked-maps.json'), JSON.stringify({ chunks, maps }));
    },
  },
};

export default withSentryConfig(nextConfig, {
  sourcemaps: { deleteSourcemapsAfterUpload: true },
  telemetry: false,
});

Steps to Reproduce

  1. Configure a Next.js app with productionBrowserSourceMaps: true and withSentryConfig(nextConfig, { sourcemaps: { deleteSourcemapsAfterUpload: true } }).
  2. Make the client source maps undeletable before the SDK deletes them. The config above does it in its own compiler.runAfterProductionCompile hook, which runs right before the SDK's, by making .next/static/chunks read-only.
  3. Run next build.

The gist runs these steps: clone it and run node repro.mjs on Linux or macOS as a user other than root (my run: docker run --rm -u node -v ./:/gist:ro -w /tmp node:26.12.1 node /gist/repro.mjs in the clone). It installs [email protected] and the latest @sentry/nextjs in a new temporary folder, builds with no auth token (nothing is uploaded; the SDK deletes the maps all the same), and counts how many of the maps the hook locked are still in .next/static/chunks after the build. It exits 0 when the build output names a map it could not delete, 1 when the maps stay and the output names none of them, 2 when a step fails. node repro.mjs file:/abs/sentry-nextjs.tgz @sentry/bundler-plugins=file:/abs/sentry-bundler-plugins.tgz runs packages packed from a checkout.

Expected Result

The build reports each source map it could not delete as an error and fails by default, as deleteFromOutput in the same file does when the same fs.promises.rm fails (

async deleteFromOutput(sourceMapPaths) {
try {
await Promise.all(
sourceMapPaths.map(async sourceMapPath => {
try {
await fs.promises.rm(sourceMapPath, { force: true });
} catch (e) {
logger.error(`Could not delete source map ${sourceMapPath} from the build output.`);
throw e;
}
}),
);
} catch (e) {
sentryScope.captureException('Error deleting source maps after upload');
await safeFlushTelemetry(sentryClient);
// Throwing by default: a map left in the output could leak to production.
handleRecoverableError(e, true);
}
},
). A map that is already gone still passes, as { force: true } lets it, and an errorHandler in the options still decides whether the build fails.

Actual Result

next build exits 0, every map stays in .next/static/chunks, and nothing in the output says so:

node v26.12.1, linux x64, uid 1000

1. npm install @sentry/nextjs@latest [email protected] [email protected] [email protected]
   installed: @sentry/nextjs 11.20.0, @sentry/bundler-plugins 11.20.0, next 16.4.6, react 19.3.1

2. next build (productionBrowserSourceMaps: true; withSentryConfig with sourcemaps.deleteSourcemapsAfterUpload: true; the app's runAfterProductionCompile hook makes .next/static/chunks read-only before @sentry/nextjs deletes its source maps)
   next build exited 0
   the hook made .next/static/chunks read-only, with 7 source maps in it: 0xvgwj92hbc31.js.map, 131kgzbuq92zu.js.map, 1jibdzavc374r.js.map, 1z_gsvi_wtuoo.js.map, 24rwc4j2i6_n6.js.map, 2gwr7q9afrpqd.js.map, 3zalnv52zv1-9.js.map
   @sentry/nextjs lines in the build output:
     [@sentry/nextjs - After Production Compile] Warning: No auth token provided. Will not upload source maps. Please set the `authToken` option. You can find information on how to generate a Sentry auth token here: https://docs.sentry.io/api/auth/
   build output lines naming one of those maps: none

3. .next/static/chunks after the build: 7 of 7 source maps still there

Summary: @sentry/nextjs 11.20.0, @sentry/bundler-plugins 11.20.0, next 16.4.6, react 19.3.1. next build exited 0; 7 of 7 source maps stayed in .next/static/chunks, and the build output names none of them. Exit 1.

deleteArtifacts, which @sentry/nextjs calls after the upload (

await sentryBuildPluginManager.deleteArtifacts();
), catches each failed fs.promises.rm and logs it with logger.debug (
await Promise.all(
filePathsToDelete.map(filePathToDelete =>
fs.promises.rm(filePathToDelete, { force: true }).catch(e => {
// This is allowed to fail - we just don't do anything
logger.debug(`An error occurred while attempting to delete asset: ${filePathToDelete}`, e);
}),
),
);
), so its own try and handleRecoverableError never see the failure.

Additional Context

That .catch came in with getsentry/sentry-javascript-bundler-plugins#373, for an ENOENT that failed the build (getsentry/sentry-javascript-bundler-plugins#370); it catches every other error the same way.

Priority

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it.

Activity

  1. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 28, 2026
  2. linear-code commented on Nov 28, 2026

    @linear-code
  3. timfish commented on Nov 28, 2026

    @timfish
    Collaborator

    The reproduction for this issue is completely synthetic and the issue description is a huge wall of text that doesn't clearly describe what the actual issue is.

    Why would sourcemaps not be deletable? How does this reproduce in regular usage?

    Also deleteSourcemapsAfterUpload is about to be deprecated. Does this issue also apply to the new sourcemaps: { delete: true }?

  4. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions