sentry-javascript icon indicating copy to clipboard operation
sentry-javascript copied to clipboard

Critical Dependency Warning After Upgrading to @sentry/nextjs v8

Open saraluk opened this issue 9 months ago • 3 comments

Is there an existing issue for this?

  • [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • [X] I have reviewed the documentation https://docs.sentry.io/
  • [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

8.1.0

Framework Version

next 14.2.3

Link to Sentry event

No response

SDK Setup

Sentry.init({
  dsn: "change me",
  tracesSampleRate: 1,
  debug: false,
  integrations: [
    Sentry.feedbackIntegration({
      autoInject: false,
    }),
  ],
});

Steps to Reproduce

Upgrade @sentry/nextjs from v7.113.0 to v8.1.0 and run the project build process

Expected Result

The build process should complete without warnings related to critical dependencies.

Actual Result

After upgrading to @sentry/nextjs version 8, I encountered the following warning in the build logs:

⚠ ./node_modules/@prisma/instrumentation/node_modules/@opentelemetry/instrumentation/build/esm/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@prisma/instrumentation/node_modules/@opentelemetry/instrumentation/build/esm/platform/node/instrumentation.js
./node_modules/@prisma/instrumentation/node_modules/@opentelemetry/instrumentation/build/esm/platform/node/index.js
./node_modules/@prisma/instrumentation/node_modules/@opentelemetry/instrumentation/build/esm/platform/index.js
./node_modules/@prisma/instrumentation/node_modules/@opentelemetry/instrumentation/build/esm/index.js
./node_modules/@prisma/instrumentation/dist/chunk-64MZJIQ5.js
./node_modules/@prisma/instrumentation/dist/index.js
./node_modules/@sentry/node/cjs/integrations/tracing/prisma.js
./node_modules/@sentry/node/cjs/index.js
./node_modules/@sentry/nextjs/cjs/index.server.js
./app/global-error.tsx

saraluk avatar May 16 '24 14:05 saraluk

Hmm, let's see if this: https://github.com/getsentry/sentry-javascript/pull/12081 possibly fixes this 🤔

mydea avatar May 16 '24 15:05 mydea

I can also confirm this is going on 8.2.1 version.

gusfune avatar May 17 '24 13:05 gusfune

I can also confirm this is going on 8.2.1 version.

+1

MarwanAlsoltany avatar May 17 '24 22:05 MarwanAlsoltany

This warning can be safely ignored. In theory, the SDK should add an ignore rule for this warning. I wonder why that doesn't work for you 🤔

https://github.com/getsentry/sentry-javascript/blob/7e59832817f20becb304b82a05bb555a7f315c6e/packages/nextjs/src/config/webpack.ts#L662-L669

Would you mind sharing your next.config.js? Can you try adding the following to your ignoreWarnings in your Next.js webpack config?

[
    { module: /@opentelemetry\/instrumentation/, message: /Critical dependency/ },
    { module: /@prisma\/instrumentation/, message: /Critical dependency/ },
]

lforst avatar May 21 '24 13:05 lforst

Hi there. I'm using @sentry/nextjs v8.7.0 and I still get the warning in the console:

- warn ../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/src/platform/node/instrumentation.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/src/platform/node/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/src/platform/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/src/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@opentelemetr_easlgnajsmadvvyfptwg27pvmy/node_modules/@sentry/opentelemetry/cjs/index.js
../../node_modules/.pnpm/@[email protected]/node_modules/@sentry/node/cjs/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@opentelemetry+api@1_n5hycvwuc2wadypxzgqutrwu2y/node_modules/@sentry/nextjs/cjs/index.server.js

Is the issue closed because the fix is going to be released in a new version? 🤔

giancarlosisasi avatar Jun 04 '24 17:06 giancarlosisasi

@GiancarlosIO It's not really a fix we published but we are ignoring the warning automatically. The code for this is here: https://github.com/getsentry/sentry-javascript/blob/7de5ea40e29125b0092138e419e0a3be20c296d5/packages/nextjs/src/config/webpack.ts#L663

I struggle a bit to understand how this is happening. Would you mind sharing reproduction? Thanks!

lforst avatar Jun 05 '24 08:06 lforst

I updated to @sentry/nextjs 8.9.2 and still getting this warning log

cyrus-za avatar Jun 15 '24 10:06 cyrus-za

@cyrus-za would you mind sharing more information? The import trace would be helpful.

lforst avatar Jun 17 '24 07:06 lforst

@cyrus-za would you mind sharing more information? The import trace would be helpful.

I setup my nextjs project with the instrumentation hook defined here

Using next 14.2.3, nx 19.2.2, pnpm 9.1.0 and node 20.14.0

I got a nx lib for my wrapper around sentry. Not sure if that makes a difference.

Everything worked just fine before the switch to v8

Critical dependency: the request of a dependency is an expression

Import trace for requested module:
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/node/instrumentation.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/node/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/platform/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@opentelemetry/instrumentation/build/esm/index.js
../../node_modules/.pnpm/@[email protected]/node_modules/@prisma/instrumentation/dist/chunk-B5GLSQ4E.js
../../node_modules/.pnpm/@[email protected]/node_modules/@prisma/instrumentation/dist/index.js
../../node_modules/.pnpm/@[email protected]/node_modules/@sentry/node/cjs/integrations/tracing/prisma.js
../../node_modules/.pnpm/@[email protected]/node_modules/@sentry/node/cjs/index.js
../../node_modules/.pnpm/@[email protected]_@[email protected]_@[email protected]_@opentelemetry+api@1_ujmyanxtchbaumo3rdotqhac5q/node_modules/@sentry/nextjs/cjs/index.server.js
../utils/sentry/src/lib/sentry.ts
../utils/sentry/src/index.ts
./src/app/layout.tsx

cyrus-za avatar Jun 17 '24 09:06 cyrus-za

It might be nx that is messing with our ignoring of the warning.

@GiancarlosIO are you also using nx?

Please note: It is safe to ignore this warning. All it does, is say that webpack is unable to statically analyze an import. This may have some implications with regards to bundling server code but in 99.999% of the cases this should be fine.

lforst avatar Jun 17 '24 09:06 lforst

In case it helps, here's my nextjs config

/* eslint-disable  n/no-process-env */
import { composePlugins, withNx } from '@nx/next'
import { withSentryConfig } from '@sentry/nextjs'
import webpack from 'webpack'
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'

/** @type {import('@nx/next/plugins/with-nx').WithNxOptions} */

const config = {
  experimental: {
    instrumentationHook: true,
  },
  typescript: {
    tsconfigPath: 'tsconfig.app.json',
    // `build` depends on `type-check` so we don't need to run `type-check` separately
    ignoreBuildErrors: true,
  },
  nx: {
    // Set this to true if you would like to use SVGR
    // See: https://github.com/gregberge/svgr
    svgr: false,
  },
  webpack(cfg, { isServer, nextRuntime }) {
    // eslint-disable-next-line no-param-reassign
    cfg.resolve.alias.canvas = false

    if (isServer && nextRuntime === 'nodejs') {
      cfg.plugins.push(new webpack.IgnorePlugin({ resourceRegExp: /@aws-sdk\/signature-v4-crt/ }))
    }

    return cfg
  },
  reactStrictMode: true,
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'lh3.googleusercontent.com',
        port: '',
        pathname: '/**/*',
      },
      {
        protocol: 'https',
        hostname: 's.gravatar.com',
        port: '',
        pathname: '/**/*',
      },
    ],
  },
  transpilePackages: ['jotai-devtools', 'react-pdf'],
}

const sentryConfig = {
  // For all available options, see:
  // https://docs.sentry.io/platforms/javascript/guides/nextjs/manual-setup/

  // Upload a larger set of source maps for prettier stack traces (increases build time)
  widenClientFileUpload: true,

  // Transpiles SDK to be compatible with IE11 (increases bundle size)
  transpileClientSDK: false,

  // Hides source maps from generated client bundles
  hideSourceMaps: true,

  automaticVercelMonitors: true,

  // Suppresses source map uploading logs during build
  silent: true,
}

const plugins = [
  // Add more Next.js plugins to this list if needed.
  withNx,
]

/** @type {import('@nx/next/plugins/with-nx').WithNxOptions} */

const configWithSentry = process.env.VERCEL ? withSentryConfig(config, sentryConfig) : config

export default composePlugins(...plugins)(configWithSentry)

cyrus-za avatar Jun 17 '24 11:06 cyrus-za

I've managed to get this working by effectively duplicating the Sentry code for this myself in my Next config:

/** @type {import('next').NextConfig} */
const nextConfig = withSentryConfig(
  ...
  webpack(
    /** @type {import('webpack').WebpackOptionsNormalized} */ config,
    /** @type {import('next/dist/server/config-shared').WebpackConfigContext} */ context
  ) {
    config.ignoreWarnings = [
      ...(config.ignoreWarnings ?? []),
      (warning, { requestShortener }) => {
        const isOtelModule =
          !!warning.module &&
          (/@opentelemetry\/instrumentation/.test(warning.module.readableIdentifier(requestShortener)) ||
            /@prisma\/instrumentation/.test(warning.module.readableIdentifier(requestShortener)))

        const isCriticalDependencyMessage = /Critical dependency/.test(warning.message)

        return isOtelModule && isCriticalDependencyMessage
      },
    ]
  },
  ...
)

I'm not sure if the reason the Sentry code only catches one of the two errors is because it's only looking at the server context?

matmannion avatar Jun 20 '24 10:06 matmannion

@lforst Why is this issue closed?

cyrus-za avatar Jun 26 '24 09:06 cyrus-za

Since we fixed the original issue and mostly due to lack of minimal reproduction. Also, I am pretty confident that it is a downstream that does unorthodox stuff. However, I want to try @matmannion's approach.

lforst avatar Jun 26 '24 12:06 lforst

Since we fixed the original issue and mostly due to lack of minimal reproduction. Also, I am pretty confident that it is a downstream that does unorthodox stuff. However, I want to try @matmannion's approach.

I haven't tried it but I think Next has some variance in config parsing if you use a next.config.mjs file instead of .js, may help with reproduction

matmannion avatar Jun 26 '24 12:06 matmannion

Even the attempt at ignoring the warning is not fixing it for us, NextJS 14.2.4, @sentry/nextjs 8.26.0.

hpohl-pley avatar Aug 14 '24 13:08 hpohl-pley

I am also encoutering this issue

berci-i avatar Sep 11 '24 09:09 berci-i

Same here

My bad, I had setup my sentry config in next.config.js, but had forgotten to add it to my plugins list (I use composePlugins from nx) 😇

mikaeledgren avatar Sep 12 '24 06:09 mikaeledgren

I am also having this issue: next: 14.2.10 @sentry/[email protected]

ming535 avatar Sep 14 '24 10:09 ming535

I only seem to be facing this issue when importing @sentry/nextjs in client components.

taylor-lindores-reeves avatar Sep 15 '24 09:09 taylor-lindores-reeves

If possible please share reproduction. We cannot act on "+1" comments.

lforst avatar Sep 16 '24 14:09 lforst

@lforst I've reproduced the error while debugging a separate issue with dd-trace here. To remove the stack trace, @sentry/nextjs would need to be added as an external package by adding this configuration.

It seems like this wasn't added to the default list due to the impact it has on boot times: https://github.com/vercel/next.js/pull/61194

mattsacks avatar Sep 17 '24 21:09 mattsacks

@mattsacks we explicitly do not want to be added to the external list. It comes with a plethora of issues.

lforst avatar Sep 18 '24 09:09 lforst

We're seeing this issue after upgrading to the latest Next.js canary release too.

mrmckeb avatar Sep 19 '24 23:09 mrmckeb

@mattsacks we explicitly do not want to be added to the external list. It comes with a plethora of issues.

Edit: reviewed #61194 which answers my question. Boot / cold start increase on require statements by 350ms. Hopefully Sentry can do something about this but will be removing @sentry/nextjs from externals list for this reason.

@lforst please could you share some insight as to why adding this package to the externals list would cause issues? Is this specifically regarding otel instrumentation, or a much wider range of issues?

~~I've added @sentey/nextjs to my externals list, warning has disappeared and I'm not facing any issues thus far. It would be nice to understand a bit about what's happening under the hood.~~

taylor-lindores-reeves avatar Sep 20 '24 05:09 taylor-lindores-reeves

@taylor-lindores-reeves much wider range. The way Next.js works is that it would even prevent us from having client components (ie components that use use client) in our package. Also, otel.

lforst avatar Sep 20 '24 07:09 lforst

I'm seeing the same thing whenever import * as Sentry from '@sentry/nextjs'; is used in a "use client" component in NextJS without the withSentryConfig being applied to the next.config.js, eg for local development when there's no NEXT_PUBLIC_SENTRY_DSN set we don't apply the withSentryConfig wrapper.

Sicria avatar Sep 24 '24 11:09 Sicria

Hilarious, an obvious bug in error tracing module left open for months.

And by open I mean closed without fixing of course.

max-degterev avatar Sep 24 '24 22:09 max-degterev

This began happening from Sentry v8.28.0 upgrading to v8.29.0 for us, NextJS v14.2.13

chris-erickson avatar Sep 25 '24 18:09 chris-erickson

I can confirm that the issue started happening with @sentry/nextjs 8.32.0 and next 14.2.13.

hayata-suenaga avatar Sep 30 '24 04:09 hayata-suenaga