sentry-javascript: next build broken when wrapped with withSentryConfig

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which package are you using?

@sentry/nextjs

SDK Version

7.30.0

Framework Version

nextjs 13.1.2

Link to Sentry event

N/A

SDK Setup

// next.config.js
// some properties omitted to keep it short

/** @type {import('next').NextConfig} */
const moduleExports = {
  reactStrictMode: true,

  swcMinify: false,

  experimental: {
    fallbackNodePolyfills: false,
  },

  eslint: {
    ignoreDuringBuilds: true,
  },
  typescript: {
    ignoreBuildErrors: true,
  },

  distDir: "build",

  poweredByHeader: false,

  sentry: {
    hideSourceMaps: true,
    widenClientFileUpload: true,
    tunnelRoute: "/api/sentry-tunnel",
  },
};

const sentryWebpackPluginOptions = {
  silent: true,
  release: "some-generated-string",
  errorHandler: (err, invokeErr, compilation) => {
    compilation.warnings.push("Sentry CLI Plugin: " + err.message);
  },
};

const wrapper =
  process.env.ANALYZE === "1"
    ? require("@next/bundle-analyzer")({
        enabled: true,
      })
    : (arg) => arg;

module.exports = wrapper(
  withSentryConfig(moduleExports, sentryWebpackPluginOptions)
);

Steps to Reproduce

  1. yarn next build

If withSentryConfig is removed, the build passes.

Expected Result

Build succeeds, next start runs successfully

Actual Result

NODE_ENV=production CI=1 yarn build
                    
yarn run v1.22.19                                                                                                                                                                                                                                         
$ next build                                                                                                                                                                                                                                              
info  - Loaded env from /Users/igor/Projects/fireside-next-app/.env
warn  - You have enabled experimental feature (fallbackNodePolyfills) in next.config.js.
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.

info  - Skipping validation of types
info  - Skipping linting
[@sentry/nextjs] You are using edge functions or middleware. Please note that Sentry does not yet support error monitoring for these features.
✨  Done in 56.93s.
NODE_ENV=production CI=1 yarn next start

yarn run v1.22.19                                                                                                                                                                                                                                         
$ /Users/igor/Projects/fireside-next-app/node_modules/.bin/next start                                                                                                                                                                                     
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
info  - Loaded env from /Users/igor/Projects/fireside-next-app/.env
warn  - You have enabled experimental feature (fallbackNodePolyfills) in next.config.js.
warn  - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk.

/Users/igor/Projects/fireside-next-app/build/BUILD_ID
Error: Could not find a production build in the '/Users/igor/Projects/fireside-next-app/build' directory. Try building your app with 'next build' before starting the production server. https://nextjs.org/docs/messages/production-start-no-build-id
    at NextNodeServer.getBuildId (/Users/igor/Projects/fireside-next-app/node_modules/next/dist/server/next-server.js:173:23)
    at new Server (/Users/igor/Projects/fireside-next-app/node_modules/next/dist/server/base-server.js:58:29)
    at new NextNodeServer (/Users/igor/Projects/fireside-next-app/node_modules/next/dist/server/next-server.js:67:9)
    at NextServer.createServer (/Users/igor/Projects/fireside-next-app/node_modules/next/dist/server/next.js:143:16)
    at async /Users/igor/Projects/fireside-next-app/node_modules/next/dist/server/next.js:155:31
    at async NextServer.prepare (/Users/igor/Projects/fireside-next-app/node_modules/next/dist/server/next.js:130:24)
    at async /Users/igor/Projects/fireside-next-app/node_modules/next/dist/cli/next-start.js:118:9
error Command failed with exit code 1.                                                                                                                                                                                                                    
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 32 (16 by maintainers)

Commits related to this issue

Most upvoted comments

@iscekic @baraeb92 We’ll probably release a new version on Monday that will resolve the issue for you. It’s not really a fix but rather a revert of a change that broke a bunch of configurations that weren’t dealing with the fact that withSentryConfig may return a function.

You can read up on it in this PR: #6846

lol I’m glad Sentry noticed this. I had to do the following to handle this:

module.exports = (phase, nextOptions) => {
    const extended = extend(nextConfig).withPlugins(plugins);
    const sentryExtendedNextConfig = sentry.extendNextConfig(extended(phase, nextOptions));

    // sentry.extendNextConfig can return either an config object or a function. Because we're
    // not sure which one it returns, we must check and handle each case.
    if (typeof sentryExtendedNextConfig === "function") {
        return sentryExtendedNextConfig(phase, nextOptions);
    } else {
        return sentryExtendedNextConfig;
    }
};

@iscekic @baraeb92 We’ll probably release a new version on Monday that will resolve the issue for you. It’s not really a fix but rather a revert of a change that broke a bunch of configurations that weren’t dealing with the fact that withSentryConfig may return a function.

You can read up on it in this PR: https://github.com/getsentry/sentry-javascript/pull/6846

I could pinpoint this down to having swcMinify: false and instructing webpack to emit sourcemaps with either devtool: 'source-map' or devtool: 'hidden-source-map'. withSentryConfig will set the devtool option so source maps can be uploaded.

This seems to be primarily an issue with Next.js so I will raise an upstream issue that hopefully gets resolved soon. In the meanwhile, I recommend using swc. From my personal experience, it has gotten pretty stable and the output isn’t too large either.

Edit: Here’s the issue: https://github.com/vercel/next.js/issues/45276

@lforst This is still broken for me, but I did manage to find the root cause, which is swcMinify: false. Removing that line makes the build work as expected.

While I did get it to work, I prefer not using swc because it results in a larger bundle and can be buggy across nextjs upgrades.

@kolorfilm It’s not necessary to call the return value of withSentryConfig anymore. Please take a look at the function’s type definition. Note: We did not change the type definition across updates - it always returned either a function or an object. Please make sure you handle both cases.

Okay it seems like this got upstream-fixed by https://github.com/vercel/next.js/pull/45423

@kolorfilm Glad you could get it resolved. This whole return value thing was a bit unfortunate (this PR description explains it a bit).

Right now the function actually returns a function if you pass in a function and it returns an object if you pass in an object, we should adjust the type to reflect that.