Skip to content

Conditional require calls are hoisted incorrectly #6943

Closed

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

Activity

  1. lforst commented on Jan 26, 2023

    @lforst
    Contributor

    Hi @beckerei, thanks for the detailed writeup and research. Unfortunately, I am not sure if we can fix this fast and reliably, if at all. If you have suggestions I am all ears.

    Would doing a dynamic import work for you? From what I can see here the underlying call is using dynamic imports anyways so it should work if I am not mistaken.

  2. joshuajaco commented on Jan 26, 2023

    @joshuajaco
    Contributor

    Yes, using a dynamic import in that case does solve the issue. However this is not a sustainable solution. Next supports conditional require calls, therefore so should sentry, especially since it already supported it prior to version 7.31.0.

    I think the only reliable solution is to keep the original import/export syntax and let the next build handle transformation. I'm afraid this will be hard to accomplish with rollup, if possible at all.

  3. lforst commented on Jan 26, 2023

    @lforst
    Contributor

    @joshuajaco I have a feeling the whole ecosystem is moving away from CJS/require as a whole so I think we should let this simmer for a bit and see if more people run into this problem before rushing to a fix.

    Can I ask why you deem using dynamic imports a non-sustainable solution?

  4. joshuajaco commented on Jan 26, 2023

    @joshuajaco
    Contributor

    In this case I agree, a dynamic import is actually the better solution. Beside the fact that you might need to import something synchronously, my biggest concern is actually that nextjs supports require out of the box, but this breaks when installing sentry. This is totally unexpected and confusing behaviour that is difficult to debug. Not to mention that this might not even be something you notice until it is too late. In our case we suddenly started showing mock data on production.

    I think a warning message as mentioned in rollup/plugins#1177 (comment) would already help prevent this issue from remaining unnoticed.

  5. beckerei commented on Jan 31, 2023

    @beckerei
    Author

    @lforst thank you for taking care :)

  6. lforst commented on Jan 31, 2023

    @lforst
    Contributor

    @beckerei No worries. Let's hope this actually fixes your issue. I'll try not to forget to ping everybody once this is released.

  7. lforst commented on Feb 1, 2023

    @lforst
    Contributor

    Hi, we just released an update that should fix this problem. https://github.com/getsentry/sentry-javascript/releases/tag/7.35.0

    Let me know if upgrading the SDK fixes this issue for you!

  8. joshuajaco commented on Feb 2, 2023

    @joshuajaco
    Contributor

    Works as expected now 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions