GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `.` in `exports` property of CJS file breaks dynamic imports · Issue #12622 · getsentry/sentry-javascript

. in exports property of CJS file breaks dynamic imports #12622

New issue
Copy link
New issue
Copy link
Closed
Closed
. in exports property of CJS file breaks dynamic imports#12622
Copy link
Labels
BugPackage-Meta: OpenTelemetry

Description

@MattIPv4
MattIPv4
opened on Jun 24, 2024
Issue body actions

Is there an existing issue for this?

  • I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues
  • I have reviewed the documentation https://docs.sentry.io/
  • 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/node

SDK Version

8.11.0

Framework Version

No response

Link to Sentry event

No response

SDK Setup

Sentry.init();

Steps to Reproduce

package.json:

{
  "type": "module",
  "dependencies": {
    "@sentry/node": "^8.11.0"
  }
}

index.js:

import * as Sentry from '@sentry/node';

const main = async () => {
  Sentry.init();

  const other = await import(new URL('./other.cjs', import.meta.url));
  console.log(other);
};

main();

other.cjs:

exports['one.two'] = () => console.log('b');

Expected Result

It runs, outputting:

[Module: null prototype] {
  default: { 'one.two': [Function (anonymous)] },
  'one.two': [Function (anonymous)]
}

(this can be reproduced by commenting out the Sentry.init(); line)

Actual Result

It fails on a sytax error:

file://[snip]/other.cjs?iitm=true:21
      let $one.two = _.one.two
              ^

SyntaxError: Unexpected token '.'
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:168:18)
    at callTranslator (node:internal/modules/esm/loader:279:14)
    at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:285:30)
    at async link (node:internal/modules/esm/module_job:78:21)

Node.js v20.12.2

Activity

  1. MattIPv4
    added
    Type: Bug
    on Jun 24, 2024
  2. getsantry
    added
    Waiting for: Product Owner
    on Jun 24, 2024
  3. getsantry
    added this to GitHub Issues with 👀 3on Jun 24, 2024
  4. getsantry
    moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 24, 2024
  5. github-actions
    added
    Package: node
    on Jun 24, 2024
  6. MattIPv4 commented on Jun 24, 2024

    @MattIPv4
    MattIPv4
    on Jun 24, 2024
    Author
    More actions

    For some additional context, this works fine in @sentry/node@7. This is blocking our upgrade to @sentry/node@8. In our actual code, the usage of . in exports comes from files generated by grpc-tools, which are then run through @grpc/grpc-js.

  7. AbhiPrasad commented on Jun 24, 2024

    @AbhiPrasad
    AbhiPrasad
    on Jun 24, 2024
    Contributor
    More actions

    Sorry for the trouble, this is def some issues with the new automatic instrumentation in 8.x.

    Could you apply patch-package with the linked patch here #12485 (comment) and see if the error goes away?

  8. getsantry
    removed
    Waiting for: Product Owner
    on Jun 24, 2024
  9. getsantry
    moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 24, 2024
  10. MattIPv4 commented on Jun 24, 2024

    @MattIPv4
    MattIPv4
    on Jun 24, 2024
    Author
    More actions

    I had to install [email protected] as the patch failed on 1.8.1, so my package.json now looks like this (from the repro above):

    {
      "type": "module",
      "scripts": {
        "postinstall": "patch-package"
      },
      "dependencies": {
        "@sentry/node": "^8.11.0",
        "import-in-the-middle": "^1.8.0",
        "patch-package": "^8.0.0"
      }
    }

    Running npm i shows that patch-package is applying the patch, however node index.js still results in the same syntax error as above.

    Reacted by Abhijeet Prasad
  11. getsantry
    added
    Waiting for: Product Owner
    on Jun 24, 2024
  12. getsantry
    moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Jun 24, 2024
  13. AbhiPrasad
    assigned
    timfish
    on Jun 24, 2024
  14. AbhiPrasad
    removed
    Waiting for: Product Owner
    on Jun 24, 2024
  15. getsantry
    moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Jun 24, 2024
  16. timfish commented on Jun 24, 2024

    @timfish
    timfish
    on Jun 24, 2024
    Collaborator
    More actions

    This is currently tracked upstream by nodejs/import-in-the-middle#94

    I've been thinking of some ways to work around this and should get a PR submitted this week!

    Reacted by Matt Cowley
  17. timfish commented on Jun 25, 2024

    @timfish
    timfish
    on Jun 25, 2024
    Collaborator
    More actions

    nodejs/import-in-the-middle#115

  18. AbhiPrasad
    added
    Package-Meta: OpenTelemetry
    on Jul 8, 2024
  19. timfish commented on Sep 16, 2024

    @timfish
    timfish
    on Sep 16, 2024
    Collaborator
    More actions

    v8.29.0 of the Node SDK added a new onlyIncludeInstrumentedModules option to registerEsmLoaderHooks.

    import * as Sentry from '@sentry/node';
    
    Sentry.init({
      dsn: '__PUBLIC_DSN__',
      registerEsmLoaderHooks: { onlyIncludeInstrumentedModules: true },
    });

    When set to true, import-in-the-middle will only wrap ESM modules that are specifically instrumented by OpenTelemetry plugins. This is useful to avoid issues where import-in-the-middle is not compatible with some dependencies.

    This feature will only work if you Sentry.init() the SDK before the instrumented modules are loaded. This can be achieved via the Node --register and --import CLI flags or by loading your app code via async await import() after calling Sentry.init().

    Please let me know if this helps work around the import-in-the-middle incompatibility issues!

  20. MattIPv4 commented on Sep 16, 2024

    @MattIPv4
    MattIPv4
    on Sep 16, 2024
    Author
    More actions

    👀 This seems to work just fine, but I am curious what the ramifications are of setting this. What exactly stops working by no longer instrumenting these files? I assume something does, otherwise this'd be a default?

  21. getsantry
    added
    Waiting for: Product Owner
    on Sep 16, 2024
  22. getsantry
    moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Sep 16, 2024
  23. timfish commented on Sep 16, 2024

    @timfish
    timfish
    on Sep 16, 2024
    Collaborator
    More actions

    By default import-in-the-middle wraps every ES module (ie. every ESM file that loaded) with another module which proxies the exports which allows them to be modified after they've already been loaded. This is important for ESM because all imports are evaluated and loaded before any code executes.

    import-in-the-middle means that Sentry can instrument the node:net module below even though it's loaded before the Sentry code runs.

    import * as Sentry from '@sentry/node';
    import * as net from 'node:net';
    
    Sentry.init({});

    There are a couple of fundamental issues with import-in-the-middle that mean it can break modules it wraps because it changes the behaviour of the exports. One example of this exported primitives. The ESM spec says that exported primitives should be treated like references which means they become internally mutable inside the module. There is no way for import-in-the-middle to wrap an exported primitive without copying it which breaks this behaviour.

    If you enable the onlyIncludeInstrumentedModules option, as the OpenTelemetry instrumentations are initialised, we notify import-in-the-middle of the specific modules we would like to be wrapped so we can instrument them. This doesn't fix the issues with import-in-the-middle, it just means we only need it to work with the libraries that we intend to instrument.

    The only downside with this option is that Sentry needs to be initialised before any module we want to instrument is loaded. This can be achieved by loading Sentry via --register or --import which is the recommended approach in the docs. You can also async load your app code after Sentry.init():

    import * as Sentry from '@sentry/node';
    
    Sentry.init({});
    
    await import('./my-app');

    I assume something does, otherwise this'd be a default?

    If we still need to use import-in-the-middle to instrument ESM libraries when the next major version of the SDK is released, it will almost certainly become the default. It's a breaking change because the Node SDK does work in a lot of cases without this option enabled. I'm still looking at ways we can improve this and enable it automatically.

    Reacted by Matt Cowley
  24. getsantry
    removed
    Waiting for: Product Owner
    on Sep 16, 2024
  25. getsantry
    moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Sep 16, 2024
  26. timfish
    removed their assignment
    on Jan 26, 2025
  27. lforst commented on Mar 3, 2025

    @lforst
    lforst
    on Mar 3, 2025
    Contributor
    More actions

    I think we can close this now that we set onlyIncludeInstrumentedModules by default.

    Reacted by Tim Fish and rdalal-do
  28. lforst
    closed this as completedon Mar 3, 2025
  29. ghost added
    Bug
    on Aug 12, 2025
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

    BugPackage-Meta: OpenTelemetry

    Projects

    • GitHub Issues with 👀 3
      Status
      No status
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.