Skip to content

What is SENTRY_SERVER_INIT_PATH? Why was it added automatically to .env? #3721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Vadorequest opened this issue Jun 10, 2021 · 15 comments
Closed
Assignees

Comments

@Vadorequest
Copy link

Vadorequest commented Jun 10, 2021

Issue

This an issue with:

Platform/SDK documentation (Which one?)
https://docs.sentry.io/platforms/javascript/guides/nextjs/
Sentry Product (Which area?)
nextjs

Description

Running npx @sentry/wizard -i nextjs, I noticed the SENTRY_SERVER_INIT_PATH env variable has been added to my .env file automatically. (at least, I assume it did, because it came out of nowhere)

SENTRY_SERVER_INIT_PATH=.next/server/sentry/initServerSDK.js

This particular env var isn't documented anywhere and there is no explanation about what it does and why it's needed.

Suggested Solution

Explain what it is, so it doesn't come as a surprise when diffing local changes.


Also, I noticed it crashes during the build, too. (when running next dev)
It still continues the build, though, it's not a fatal error.

Error: Cannot find module '/Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/.next/server/sentry/initServerSDK.js'
Require stack:
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/node_modules/@sentry/nextjs/dist/utils/instrumentServer.js
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/node_modules/@sentry/nextjs/dist/index.server.js
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/next.config.js
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/node_modules/next/dist/next-server/server/config.js
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/node_modules/next/dist/server/next.js
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/node_modules/next/dist/server/lib/start-server.js
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/node_modules/next/dist/cli/next-dev.js
- /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/node_modules/next/dist/bin/next
[Sentry Webpack Plugin] Creating new release:
 'development'
[Sentry Webpack Plugin] Calling upload-sourcemaps with:
 {
  finalize: true,
  rewrite: true,
  dryRun: true,
  release: 'development',
  authToken: '',
  configFile: 'sentry.properties',
  stripPrefix: [ 'webpack://_N_E/' ],
  urlPrefix: '~/_next',
  include: [ '.next/' ],
  ignore: [
    '.next/cache',
    'server/ssr-module-cache.js',
    'static/*/_ssgManifest.js',
    'static/*/_buildManifest.js'
  ],
  debug: true
}

@getsentry-release
Copy link
Contributor

Routing to @getsentry/team-webplatform for triage. ⏲️

@AbhiPrasad
Copy link
Member

Hey, thank you for reporting! We use the SENTRY_SERVER_INIT_PATH env variable to tell webpack how to bundle the code from sentry.server.config.js. We set this in an environmental variable as webpack can often vary how/where it generates the sentry bundle. You're right that we should put a note in our docs about that, we will get on that right away!

The build should not be erroring though - that is a problem we need to fix. To help us debug this, could you share the version of @sentry/nextjs you are running, as well as a sample of your next.config.js?

@Vadorequest
Copy link
Author

I'm using "@sentry/nextjs": "6.6.0", and here is my next.config.js file

const bundleAnalyzer = require('@next/bundle-analyzer');
const nextSourceMaps = require('@zeit/next-source-maps');
const createNextPluginPreval = require('next-plugin-preval/config');
const packageJson = require('./package');
const i18nConfig = require('./src/modules/core/i18n/i18nConfig');
const { withSentryConfig } = require('@sentry/nextjs');

const withNextPluginPreval = createNextPluginPreval();
const withSourceMaps = nextSourceMaps();
const withBundleAnalyzer = bundleAnalyzer({ // Run with "yarn analyse:bundle" - See https://www.npmjs.com/package/@next/bundle-analyzer
  enabled: process.env.ANALYZE_BUNDLE === 'true',
});
const supportedLocales = i18nConfig.supportedLocales.map((supportedLocale) => {
  return supportedLocale.name;
});
const noRedirectBlacklistedPaths = ['_next', 'api']; // Paths that mustn't have rewrite applied to them, to avoid the whole app to behave inconsistently
const publicBasePaths = ['robots', 'static', 'favicon.ico']; // All items (folders, files) under /public directory should be added there, to avoid redirection when an asset isn't found
const noRedirectBasePaths = [...supportedLocales, ...publicBasePaths, ...noRedirectBlacklistedPaths]; // Will disable url rewrite for those items (should contain all supported languages and all public base paths)
const date = new Date();
const GIT_COMMIT_SHA_SHORT = typeof process.env.GIT_COMMIT_SHA === 'string' && process.env.GIT_COMMIT_SHA.substring(0, 8);
const sentryWebpackPluginOptions = {
  // Additional config options for the Sentry Webpack plugin. Keep in mind that
  // the following options are set automatically, and overriding them is not
  // recommended:
  //   release, url, org, project, authToken, configFile, stripPrefix,
  //   urlPrefix, include, ignore
  // For all available options, see:
  // https://github.com/getsentry/sentry-webpack-plugin#options.

  // debug: process.env.NODE_ENV === 'development',
  // silent: true, // Suppresses all logs
};

console.debug(`Building Next with NODE_ENV="${process.env.NODE_ENV}" NEXT_PUBLIC_APP_STAGE="${process.env.NEXT_PUBLIC_APP_STAGE}" for NEXT_PUBLIC_CUSTOMER_REF="${process.env.NEXT_PUBLIC_CUSTOMER_REF}" using GIT_COMMIT_SHA=${process.env.GIT_COMMIT_SHA} and GIT_COMMIT_REF=${process.env.GIT_COMMIT_REF}`);

// We use `filter` to make sure there are not empty element.
// Default value is an empty array.
const GIT_COMMIT_TAGS = process.env.GIT_COMMIT_TAGS ? process.env.GIT_COMMIT_TAGS.trim() : '';
console.debug(`Deployment will be tagged automatically, using GIT_COMMIT_TAGS: "${GIT_COMMIT_TAGS}"`);

// Iterate over all tags and extract the first the match "v*"
const APP_RELEASE_TAG = GIT_COMMIT_TAGS ? GIT_COMMIT_TAGS.split(' ').find((tag) => tag.startsWith('v')) : `unknown-${GIT_COMMIT_SHA_SHORT}`;
console.debug(`Release version resolved from tags: "${APP_RELEASE_TAG}" (matching first tag starting with "v")`);

console.log('next config process.env.SENTRY_DSN', process.env.SENTRY_DSN)
/**
 * This file is for advanced configuration of the Next.js framework.
 *
 * The below config applies to the whole application.
 * next.config.js gets used by the Next.js server and build phases, and it's not included in the browser build.
 *
 * Make sure adding "withSentryConfig" is the last code to run before exporting, to  ensure that your source maps include changes from all other Webpack plugins.
 *
 * XXX Not all configuration options are listed below, we only kept those of most interest.
 *  You'll need to dive into Next.js own documentation to find out about what's not included.
 *  Basically, we focused on options that seemed important for a SSG/SSR app running on serverless mode (Vercel).
 *  Also, we included some options by are not using them, this is mostly to help make you aware of those options, in case you'd need them.
 *
 * @see https://nextjs.org/docs/api-reference/next.config.js/introduction
 * @see https://docs.sentry.io/platforms/javascript/guides/nextjs/
 * @see https://github.com/getsentry/sentry-webpack-plugin#options
 */
module.exports = withNextPluginPreval(withSentryConfig(withBundleAnalyzer(withSourceMaps({
  // basepath: '', // If you want Next.js to cover only a subsection of the domain. See https://nextjs.org/docs/api-reference/next.config.js/basepath
  // target: 'serverless', // Automatically enabled on Vercel, you may need to manually opt-in if you're not using Vercel. See https://nextjs.org/docs/api-reference/next.config.js/build-target#serverless-target
  // trailingSlash: false, // By default Next.js will redirect urls with trailing slashes to their counterpart without a trailing slash. See https://nextjs.org/docs/api-reference/next.config.js/trailing-slash

  /**
   * React's Strict Mode is a development mode only feature for highlighting potential problems in an application.
   * It helps to identify unsafe lifecycles, legacy API usage, and a number of other features.
   *
   * Officially suggested by Next.js:
   * We strongly suggest you enable Strict Mode in your Next.js application to better prepare your application for the future of React.
   *
   * If you or your team are not ready to use Strict Mode in your entire application, that's OK! You can incrementally migrate on a page-by-page basis using <React.StrictMode>.
   *
   * @see https://nextjs.org/docs/api-reference/next.config.js/react-strict-mode
   */
  reactStrictMode: true,

  /**
   * Environment variables added to JS bundle.
   *
   * XXX All env variables defined in ".env*" files that aren't public (those that don't start with "NEXT_PUBLIC_") MUST manually be made available at build time below.
   *  They're necessary on Vercel for runtime execution (SSR, SSG with revalidate, everything that happens server-side will need those).
   *
   * XXX This is a duplication of the environment variables.
   *  The variables defined below are only used locally, while those in "vercel.*.json:build:env" will be used on the Vercel platform.
   *  See https://vercel.com/docs/v2/build-step/#providing-environment-variables
   *
   * @see https://nextjs.org/docs/api-reference/next.config.js/environment-variables
   */
  env: {
    // Most sensitive env variables
    GITHUB_DISPATCH_TOKEN: process.env.GITHUB_DISPATCH_TOKEN,
    AIRTABLE_API_KEY: process.env.AIRTABLE_API_KEY,
    AIRTABLE_BASE_ID: process.env.AIRTABLE_BASE_ID,
    LOCIZE_API_KEY: process.env.LOCIZE_API_KEY,
    SENTRY_DSN: process.env.SENTRY_DSN,
    NEXT_PUBLIC_SENTRY_DSN: process.env.SENTRY_DSN, // Sentry DSN must be provided to the browser for error reporting to work there

    // Vercel env variables - See https://vercel.com/docs/environment-variables#system-environment-variables
    VERCEL: process.env.VERCEL,
    VERCEL_ENV: process.env.VERCEL_ENV,
    VERCEL_URL: process.env.VERCEL_URL,
    CI: process.env.CI,

    // Dynamic env variables
    NEXT_PUBLIC_APP_DOMAIN: process.env.VERCEL_URL, // Alias
    NEXT_PUBLIC_APP_BASE_URL: process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : 'http://localhost:8888',
    NEXT_PUBLIC_APP_BUILD_TIME: date.toString(),
    NEXT_PUBLIC_APP_BUILD_TIMESTAMP: +date,
    NEXT_PUBLIC_APP_NAME: packageJson.name,
    NEXT_PUBLIC_APP_NAME_VERSION: `${packageJson.name}-${APP_RELEASE_TAG}`,
    GIT_COMMIT_SHA_SHORT,
    GIT_COMMIT_SHA: process.env.GIT_COMMIT_SHA, // Resolve commit hash from ENV first (set through CI), fallbacks to reading git (when used locally, through "/scripts/populate-git-env.sh")
    GIT_COMMIT_REF: process.env.GIT_COMMIT_REF, // Resolve commit ref (branch/tag) from ENV first (set through CI), fallbacks to reading git (when used locally, through "/scripts/populate-git-env.sh")
    GIT_COMMIT_TAGS: process.env.GIT_COMMIT_TAGS || '', // Resolve commit tags/releases from ENV first (set through CI), fallbacks to reading git (when used locally, through "/scripts/populate-git-env.sh")
  },

  /**
   * Headers allow you to set custom HTTP headers for an incoming request path.
   *
   * Headers allow you to set route specific headers like CORS headers, content-types, and any other headers that may be needed.
   * They are applied at the very top of the routes.
   *
   * @example source: '/(.*?)', // Match all paths, including "/"
   * @example source: '/:path*', // Match all paths, excluding "/"
   *
   * @return {Promise<Array<{ headers: [{value: string, key: string}], source: string }>>}
   * @see https://nextjs.org/docs/api-reference/next.config.js/headers
   * @since 9.5 - See https://nextjs.org/blog/next-9-5#headers
   */
  async headers() {
    // XXX We need to embed our website into external websites for the NRN demo, but you might want to disable this
    const DISABLE_IFRAME_EMBED_FROM_3RD_PARTIES = false;

    const headers = [
      {
        // Make all fonts immutable and cached for one year
        'source': '/static/fonts/(.*?)',
        'headers': [
          {
            'key': 'Cache-Control',
            // See https://www.keycdn.com/blog/cache-control-immutable#what-is-cache-control-immutable
            // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#browser_compatibility
            'value': `public, max-age=31536000, immutable`,
          },
        ],
      },
      {
        // Make all other static assets immutable and cached for one hour
        'source': '/static/(.*?)',
        'headers': [
          {
            'key': 'Cache-Control',
            // See https://www.keycdn.com/blog/cache-control-immutable#what-is-cache-control-immutable
            // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#browser_compatibility
            'value': `public, max-age=3600, immutable`,
          },
        ],
      },
      {
        source: '/(.*?)', // Match all paths, including "/" - See https://github.com/vercel/next.js/discussions/17991#discussioncomment-112028
        headers: [
          // This directive helps protect against some XSS attacks
          // See https://infosec.mozilla.org/guidelines/web_security#x-content-type-options
          {
            key: 'X-Content-Type-Options',
            value: `nosniff`,
          },
        ],
      },
      {
        source: '/(.*?)', // Match all paths, including "/" - See https://github.com/vercel/next.js/discussions/17991#discussioncomment-112028
        headers: [
          // This directive helps protect user's privacy and might avoid leaking sensitive data in urls to 3rd parties (e.g: when loading a 3rd party asset)
          // See https://infosec.mozilla.org/guidelines/web_security#referrer-policy
          // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
          // See https://scotthelme.co.uk/a-new-security-header-referrer-policy/
          {
            key: 'Referrer-Policy',
            // "no-referrer-when-downgrade" is the default behaviour
            // XXX You might want to restrict even more the referrer policy
            value: `no-referrer-when-downgrade`,
          },
        ],
      },
    ];

    /**
     * Because the NRN demo uses Stacker provider to show our app as an embedded iframe, we need to allow our pages to be embedded by other websites.
     *
     * In staging, we don't want to allow any website to embed our app by default, to avoid customers mistakenly use our preview URL in their production app.
     * Although, we want to allow Stacker to do it, so we can preview our website from Stacker (quick-preview).
     *
     * In production, we want to allow any website to embed our app by default, because we don't want to manage the list of websites that might embed our content.
     * Alternatively, we could also generate the CSP dynamically by pre-fetching the allowed websites from our CMS/API.
     */
    if (!DISABLE_IFRAME_EMBED_FROM_3RD_PARTIES && process.env.NEXT_PUBLIC_APP_STAGE !== 'production') {
      headers.push({
        source: '/(.*?)', // Match all paths, including "/" - See https://github.com/vercel/next.js/discussions/17991#discussioncomment-112028
        headers: [
          {
            key: 'Content-Security-Policy',
            value: `frame-ancestors *.stacker.app`,
          },
        ],
      });
    }

    // When 3rd party embeds are forbidden, only allow same origin to embed iframe by default
    // This is a stronger default, if you don't to embed your site in any external website
    if (DISABLE_IFRAME_EMBED_FROM_3RD_PARTIES) {
      headers.push({
        // This directive's "ALLOW-FROM" option is deprecated in favor of "Content-Security-Policy" "frame-ancestors"
        // So, we use a combination of both the CSP directive and the "X-Frame-Options" for browser that don't support CSP
        // See https://infosec.mozilla.org/guidelines/web_security#x-frame-options
        key: 'X-Frame-Options',
        value: `SAMEORIGIN`,
      });
      headers.push({
        source: '/(.*?)', // Match all paths, including "/" - See https://github.com/vercel/next.js/discussions/17991#discussioncomment-112028
        // See https://infosec.mozilla.org/guidelines/web_security#x-frame-options
        headers: [
          {
            key: 'Content-Security-Policy',
            value: `frame-ancestors 'self`,
          },
        ],
      });
    }

    console.info('Using headers:', JSON.stringify(headers, null, 2));

    return headers;
  },

  /**
   * Rewrites allow you to map an incoming request path to a different destination path.
   *
   * Rewrites are only available on the Node.js environment and do not affect client-side routing.
   * Rewrites are the most commonly used form of custom routing — they're used for dynamic routes (pretty URLs), user-land routing libraries (e.g. next-routes), internationalization, and other advanced use cases.
   *
   * For example, the route /user/:id rendering a specific user's profile page is a rewrite.
   * Rendering your company's about page for both /about and /fr/a-propos is also a rewrite.
   * The destination url can be internal, or external.
   *
   * @return { Promise<Array<{ destination: string, source: string, headers: Array<{ key: string, value: string }> }>> }
   * @see https://nextjs.org/docs/api-reference/next.config.js/rewrites
   * @since 9.5 - See https://nextjs.org/blog/next-9-5#rewrites
   */
  async rewrites() {
    const rewrites = [
      // I18n rewrites
      {
        // XXX Doesn't work locally (maybe because of rewrites), but works online
        source: '/',
        destination: '/api/autoRedirectToLocalisedPage',
      },
      {
        source: `/:locale((?!${noRedirectBasePaths.join('|')})[^/]+)(.*)`,
        destination: '/api/autoRedirectToLocalisedPage',
      },

      // Robots rewrites
      {
        source: `/robots.txt`,
        destination: process.env.NEXT_PUBLIC_APP_STAGE === 'production' ? `/robots/production.txt` : '/robots/!production.txt',
      },
    ];

    console.info('Using rewrites:', rewrites);

    return rewrites;
  },

  /**
   * Redirects allow you to redirect an incoming request path to a different destination path.
   *
   * Redirects are only available on the Node.js environment and do not affect client-side routing.
   * By redirects, we mean HTTP Redirects (aka URL forwarding).
   * Redirects are most commonly used when a website is reorganized — ensuring search engines and bookmarks are forwarded to their new locations.
   * The destination url can be internal, or external.
   *
   * @return { Promise<Array<{ permanent: boolean, destination: string, source: string, statusCode?: number }>> }
   * @see https://nextjs.org/docs/api-reference/next.config.js/redirects
   * @since 9.5 - See https://nextjs.org/blog/next-9-5#redirects
   */
  async redirects() {
    const redirects = [
      // I18n redirects
      {
        // Redirect root link with trailing slash to non-trailing slash, avoids 404 - See https://github.com/vercel/next.js/discussions/10651#discussioncomment-8270
        source: '/:locale/',
        destination: '/:locale',
        permanent: process.env.NEXT_PUBLIC_APP_STAGE !== 'development', // Do not use permanent redirect locally to avoid browser caching when working on it
      },
      {
        // Redirect link with trailing slash to non-trailing slash (any depth), avoids 404 - See https://github.com/vercel/next.js/discussions/10651#discussioncomment-8270
        source: '/:locale/:path*/',
        destination: '/:locale/:path*',
        permanent: process.env.NEXT_PUBLIC_APP_STAGE !== 'development', // Do not use permanent redirect locally to avoid browser caching when working on it
      },
    ];

    console.info('Using redirects:', redirects);

    return redirects;
  },

  future: {
    // See https://nextjs.org/docs/messages/webpack5
    // Necessary to manually specify to use webpack 5, because we use a custom "webpack" config (see below)
    webpack5: true,
  },

  resolve: {
    fallback: {
      // Fixes npm packages that depend on `fs` module
      fs: false,
    },
  },

  /**
   * The webpack function is executed twice, once for the server and once for the client.
   * This allows you to distinguish between client and server configuration using the isServer property.
   *
   * @param config Current webpack config. Useful to reuse parts of what's already configured while overridding other parts.
   * @param buildId The build id, used as a unique identifier between builds.
   * @param dev Indicates if the compilation will be done in development.
   * @param isServer It's true for server-side compilation, and false for client-side compilation.
   * @param defaultLoaders Default loaders used internally by Next.js:
   *  - babel Default babel-loader configuration
   * @see https://nextjs.org/docs/api-reference/next.config.js/custom-webpack-config
   */
  webpack: (config, {
    buildId,
    dev,
    isServer,
    defaultLoaders,
  }) => {
    if (isServer) {
      /**
       * This special server-only environment variable isn't string-replaced by webpack during bundling (it isn't added to the DefinePlugin definitions).
       *
       * Therefore, it's:
       * - Always '1' on the server, during development
       * - Always '1' on the server, during the Next.js build step
       * - Always undefined on the browser
       * - Always undefined in API endpoints
       * - Always undefined during static pages re-generations (ISG) and server-side pages
       *
       * It can be useful when performing processing that should only happen during the initial build, or not during the initial build.
       */
      process.env.IS_SERVER_INITIAL_BUILD = '1';
    }

    const APP_VERSION_RELEASE = APP_RELEASE_TAG || buildId;
    config.plugins.map((plugin, i) => {
      // Inject custom environment variables in "DefinePlugin" - See https://webpack.js.org/plugins/define-plugin/
      if (plugin.__proto__.constructor.name === 'DefinePlugin') {
        // Dynamically add some "public env" variables that will be replaced during the build through "DefinePlugin"
        // Those variables are considered public because they are available at build time and at run time (they'll be replaced during initial build, by their value)
        plugin.definitions['process.env.NEXT_PUBLIC_APP_BUILD_ID'] = JSON.stringify(buildId);
        plugin.definitions['process.env.NEXT_PUBLIC_APP_VERSION_RELEASE'] = JSON.stringify(APP_VERSION_RELEASE);
        plugin.definitions['process.env.SENTRY_RELEASE'] = JSON.stringify(APP_VERSION_RELEASE); // Necessary to forward it automatically to source maps
      }
    });

    if (isServer) { // Trick to only log once
      console.debug(`[webpack] Building release "${APP_VERSION_RELEASE}" using NODE_ENV="${process.env.NODE_ENV}" ${process.env.IS_SERVER_INITIAL_BUILD ? 'with IS_SERVER_INITIAL_BUILD="1"' : ''}`);
    }

    return config;
  },

  /**
   * Next.js uses a constant id generated at build time to identify which version of your application is being served.
   *
   * This can cause problems in multi-server deployments when next build is ran on every server.
   * In order to keep a static build id between builds you can provide your own build id.
   *
   * XXX We documented this function in case you might want to use it, but we aren't using it.
   *
   * @see https://nextjs.org/docs/api-reference/next.config.js/configuring-the-build-id
   */
  // generateBuildId: async () => {
  //   // You can, for example, get the latest git commit hash here
  //   return 'my-build-id'
  // },

  /**
   * Next.js exposes some options that give you some control over how the server will dispose or keep in memory built pages in development.
   *
   * XXX We documented this function in case you might want to use it, but we aren't using it.
   *
   * @see https://nextjs.org/docs/api-reference/next.config.js/configuring-onDemandEntries
   */
  // onDemandEntries: {
  //   // period (in ms) where the server will keep pages in the buffer
  //   maxInactiveAge: 25 * 1000,
  //   // number of pages that should be kept simultaneously without being disposed
  //   pagesBufferLength: 2,
  // },

  poweredByHeader: false, // See https://nextjs.org/docs/api-reference/next.config.js/disabling-x-powered-by
})), sentryWebpackPluginOptions));

The code is open source at https://github.com/UnlyEd/next-right-now/pull/370/files (PR)

@Vadorequest
Copy link
Author

Also, the path .next/server/sentry/initServerSDK.js seems correct, from what I can tell.

image

@Vadorequest
Copy link
Author

image

The absolute path /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/.next/server/sentry/initServerSDK.js does exist, I'm not sure why Webpack cannot resolve it.

 head /Users/vadorequest/dev/NRN/v2-mst-aptd-at-lcz-sty/.next/server/sentry/initServerSDK.js
/*
 * ATTENTION: An "eval-source-map" devtool has been used.
 * This devtool is neither made for production nor for readable output files.
 * It uses "eval()" calls to create a separate source file with attached SourceMaps in the browser devtools.
 * If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
 * or disable the default devtool with "devtool: false".
 * If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
 */
(function() {
var exports = {};

@lobsterkatie
Copy link
Member

@Vadorequest - two questions:

  1. When you run yarn dev and it gives you that error during build, do you get the error again when you hit an API route wrapped in withSentry? If not, can you please try adding debug: true to your server-side Sentry.init() and see if instead of a second error, you see a bunch of Sentry Logger [Log]: Integration installed: messages?

  2. Are any of the potential moving parts here (any part of your installed copy of next, or of @sentry/nextjs, or the next.config.js file, or anything else you can think of which might be relevant) actually symbolic links to somewhere else, or file:-type dependencies?

(I ask because I was running into this same problem - webpack not being able to find files which were, in fact, there, including this one - when testing my local copy of the SDK. If I linked my local copy, or included it as a file: dependency, I got this error. I ended up needing to actually copy the built files into my test app's node_modules folder in order for all of the paths to resolve.)

@Vadorequest
Copy link
Author

@lobsterkatie

  1. Here is the logs when hitting an API endpoint with debug enabled
event - build page: /api/status
wait  - compiling...
[Sentry Webpack Plugin] Creating new release:
 'development'
[Sentry Webpack Plugin] Calling upload-sourcemaps with:
 {
  finalize: true,
  rewrite: true,
  dryRun: true,
  release: 'development',
  authToken: 'XXXXXXXXX',
  configFile: 'sentry.properties',
  stripPrefix: [ 'webpack://_N_E/' ],
  urlPrefix: '~/_next',
  include: [ '.next/' ],
  ignore: [
    '.next/cache',
    'server/ssr-module-cache.js',
    'static/*/_ssgManifest.js',
    'static/*/_buildManifest.js'
  ],
  debug: true
}
[Sentry Webpack Plugin] Finalizing release:
 'development'
[Sentry Webpack Plugin] Creating new release:
 'development'
[Sentry Webpack Plugin] Calling upload-sourcemaps with:
 {
  finalize: true,
  rewrite: true,
  dryRun: true,
  release: 'development',
  authToken: 'XXXXXX',
  configFile: 'sentry.properties',
  stripPrefix: [ 'webpack://_N_E/' ],
  urlPrefix: '~/_next',
  include: [ '.next/' ],
  ignore: [
    '.next/cache',
    'server/ssr-module-cache.js',
    'static/*/_ssgManifest.js',
    'static/*/_buildManifest.js'
  ],
  debug: true
}
[Sentry Webpack Plugin] Finalizing release:
 'development'
event - compiled successfully

  1. I use absolute modules aliases, but besides that I don't thing there is anything particular. The files are at the expected location on disk. I didn't do anything in particular AFAIK.

@lobsterkatie
Copy link
Member

lobsterkatie commented Jun 14, 2021

Thanks. It looks like you added debug: true to your sentry webpack plugin options. Did you also add it to your server-side Sentry.init() call (in sentry.server.config.js)? Here's what I'd expect to see if hitting the API endpoint doesn't give you the error you mentioned (notice the Sentry Logger [Log] statements):

Logs
> yarn dev
yarn run v1.22.10
$ next dev -p 3001
ready - started server on 0.0.0.0:3001, url: http://localhost:3001
info  - Loaded env from /Users/Katie/Documents/Sentry/test-apps/next-js-test/.env.local
info  - Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5
[Sentry Webpack Plugin] DRY Run Mode
[Sentry Webpack Plugin] DRY Run Mode
[Sentry Webpack Plugin] Creating new release:
 'development'
[Sentry Webpack Plugin] Calling upload-sourcemaps with:
 {
  finalize: true,
  rewrite: true,
  dryRun: true,
  release: 'development',
  configFile: 'sentry.properties',
  stripPrefix: [ 'webpack://_N_E/' ],
  urlPrefix: '~/_next',
  include: [ '.next/' ],
  ignore: [
    '.next/cache',
    'server/ssr-module-cache.js',
    'static/*/_ssgManifest.js',
    'static/*/_buildManifest.js'
  ],
  org: 'xxxxxx',
  url: 'https://sentry.io/',
  project: 'xxxxx'
}
[Sentry Webpack Plugin] Finalizing release:
 'development'
[Sentry Webpack Plugin] Creating new release:
 'development'
[Sentry Webpack Plugin] Calling upload-sourcemaps with:
 {
  finalize: true,
  rewrite: true,
  dryRun: true,
  release: 'development',
  configFile: 'sentry.properties',
  stripPrefix: [ 'webpack://_N_E/' ],
  urlPrefix: '~/_next',
  include: [ '.next/' ],
  ignore: [
    '.next/cache',
    'server/ssr-module-cache.js',
    'static/*/_ssgManifest.js',
    'static/*/_buildManifest.js'
  ],
  org: 'xxxxxx',
  url: 'https://sentry.io/',
  project: 'xxxxx'
}
[Sentry Webpack Plugin] Finalizing release:
 'development'
event - compiled successfully
Sentry Logger [Log]: Integration installed: InboundFilters
Sentry Logger [Log]: Integration installed: FunctionToString
Sentry Logger [Log]: Integration installed: OnUncaughtException
Sentry Logger [Log]: Integration installed: OnUnhandledRejection
Sentry Logger [Log]: Integration installed: LinkedErrors
Sentry Logger [Log]: Integration installed: Http
Sentry Logger [Log]: Integration installed: RewriteFrames
Sentry Logger [Log]: [Tracing] starting http.server transaction - GET /api/hello
event - build page: /api/hello
wait  - compiling...
[Sentry Webpack Plugin] Creating new release:
 'development'
[Sentry Webpack Plugin] Calling upload-sourcemaps with:
 {
  finalize: true,
  rewrite: true,
  dryRun: true,
  release: 'development',
  configFile: 'sentry.properties',
  stripPrefix: [ 'webpack://_N_E/' ],
  urlPrefix: '~/_next',
  include: [ '.next/' ],
  ignore: [
    '.next/cache',
    'server/ssr-module-cache.js',
    'static/*/_ssgManifest.js',
    'static/*/_buildManifest.js'
  ],
  org: 'xxxxxx',
  url: 'https://sentry.io/',
  project: 'xxxxx'
}
[Sentry Webpack Plugin] Finalizing release:
 'development'
Sentry Logger [Log]: [Tracing] Adding sentry-trace header 2a925036f63442c8b21c61953d0fc45a-833e371c74b83399-1 to outgoing request to https://telemetry.nextjs.org/api/v1/record: 
[Sentry Webpack Plugin] Creating new release:
 'development'
[Sentry Webpack Plugin] Calling upload-sourcemaps with:
 {
  finalize: true,
  rewrite: true,
  dryRun: true,
  release: 'development',
  configFile: 'sentry.properties',
  stripPrefix: [ 'webpack://_N_E/' ],
  urlPrefix: '~/_next',
  include: [ '.next/' ],
  ignore: [
    '.next/cache',
    'server/ssr-module-cache.js',
    'static/*/_ssgManifest.js',
    'static/*/_buildManifest.js'
  ],
  org: 'xxxxxx',
  url: 'https://sentry.io/',
  project: 'xxxxx'
}
[Sentry Webpack Plugin] Finalizing release:
 'development'
Sentry Logger [Log]: [Tracing] Adding sentry-trace header 2a925036f63442c8b21c61953d0fc45a-a36b8279032b8a96-1 to outgoing request to https://telemetry.nextjs.org/api/v1/record: 
event - compiled successfully
Sentry Logger [Log]: [Tracing] starting http.server transaction - GET /api/hello
Sentry Logger [Log]: [Tracing] Finishing http.server transaction: GET /api/hello.

In any case, I figured out what's happening, I believe. The very first time you run yarn dev, there's no .next folder, and during build the SDK looks and can't find the relevant file. By the time the build is over, though, both the folder and the relevant file exist, so when you actually hit a route, all is well. We should definitely handle this more gracefully. I've added it to our backlog.

In the meantime, when you add debug: true to your server init, does the SDK indeed print all of the log statements which show that it's starting up?

@Vadorequest
Copy link
Author

In the meantime, when you add debug: true to your server init, does the SDK indeed print all of the log statements which show that it's starting up?

Yes it does.

image


Okay, so it's basically a false-positive error and everything works fine - great. Thanks for investing this!

@lobsterkatie
Copy link
Member

Great. As I say, we should handle this case better, but glad to hear that it's not blocking you.

(Leaving this open for the moment since there's a fix backlogged.)

@huggge
Copy link

huggge commented Jun 29, 2021

Maybe obvious to most of you but im deploying to Vercel.

Running locally, errors get caught and sent to Sentry but in preview deployment to Vercel no errors are sent.
Do i need to add this ENV manually in Vercel?

SENTRY_SERVER_INIT_PATH=.next/server/sentry/initServerSDK.js

I believe this is a bit unclear in the documentation https://docs.sentry.io/platforms/javascript/guides/nextjs/

@natterstefan
Copy link

Hi @huggge,

I'm having a hard time deploying my app with @sentry/nextjs as well. Have you figured out if you need to set SENTRY_SERVER_INIT_PATH on Vercel in the meantime? And if so, what's the value you added?

Thanks for your help.

@huggge
Copy link

huggge commented Jun 30, 2021

@natterstefan I added
SENTRY_SERVER_INIT_PATH=.next/server/sentry/initServerSDK.js aswell as
SENTRY_URL
SENTRY_ORG
SENTRY_PROJECT
SENTRY_AUTH_TOKEN

But only got it working after downgrading to @sentry/nextjs 6.7.1

However im not sure if the SENTRY_SERVER_INIT_PATH is needed.

Are you using next-compose-plugins?

If so I found out that there is an issue with adding the withSentryConfig like this [withSentryConfig, SentryWebpackPluginOptions

Got it working though by adding the plugin this way:

module.exports = withPlugins(
 [
   [nextTranslate],
   [
     optimizedImages,
     {
       /* config for next-optimized-images */
       optimizeImagesInDev: true,
       mozjpeg: { quality: 95 },
     },
   ],
 ],
 withSentryConfig(nextConfig, SentryWebpackPluginOptions),
)

@natterstefan
Copy link

natterstefan commented Jun 30, 2021

Hi @huggge,

thanks for the reply. This is the current setup (demo repo) that I use in our app: https://github.com/natterstefan/next-with-sentry/blob/6b88176afee7d8d1395015047224555959175bc3/next.config.js#L36-L37. It works with "@sentry/nextjs": "^6.8.0" and "next-compose-plugins": "^2.2.1".

For now, I added SENTRY_SERVER_INIT_PATH as well.

@lobsterkatie
Copy link
Member

FYI, we've changed our approach, and as of getsentry/sentry-javascript#3786, we're no longer using the SENTRY_SERVER_INIT_PATH env variable. It should be released sometime next week.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants