Skip to content

Conditional require calls are hoisted incorrectly #6943

Closed
@beckerei

Description

@beckerei

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.33.0

Framework Version

Next 13.1.5

Link to Sentry event

No response

SDK Setup

  Sentry.init({
    dsn: 'foobar',
    environment,
    enabled: !!environment && ['staging', 'production'].includes(environment),
    tracesSampleRate: 0.2,
    tracesSampler: (ctx) => !ctx.transactionContext.name.includes('healthz'),
    beforeSend(event) {
      if (!event.request?.cookies) return event;
      const { cookies } = event.request;

      Object.keys(cookies).forEach((key) => {
        if (key.toLowerCase().includes('session')) {
          cookies[key] = '[Redacted]';
        }
      });

      return event;
    },
  });

Steps to Reproduce

  1. conditionally use a require() call
  2. start prod or dev server of next in a case where the condition should be false

Expected Result

The file should not be required

Actual Result

The file will be required regardless of the surrounding if-statement


Our understanding is that this behavior was introduced within the following PR
#6685

Enabeling transformMixedEsModules
See also rollup/plugins#1177 which indicates that the rollup plugin can't fix this.

This is the recommended way to run MSW in your local env, see example https://github.com/vercel/next.js/blob/canary/examples/with-msw/pages/_app.tsx

Metadata

Metadata

Assignees

No one assigned

    Labels

    Package: nextjsIssues related to the Sentry Nextjs SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions