nodejs / import-in-the-middle

Like `require-in-the-middle`, but for ESM import
https://www.npmjs.com/package/import-in-the-middle
Apache License 2.0
60 stars 23 forks source link

Import in the middle breaks `drizzle-orm` exports #141

Open Lms24 opened 1 month ago

Lms24 commented 1 month ago

Expected Behavior

Using import-in-the-middle works with imports from drizzle-orm as with other NPM packages

Actual Behavior

Registering the import-in-the-middle hook causes certain top-level exports from the drizzle-orm package to be missing.

This was originally reported as a bug in our Sentry JS SDK repo and I tracked it down to registering the IITM hook as the only thing needed to reproduce the error.

Steps to Reproduce the Problem

I created a minimal reproduction example in this repo: https://github.com/Lms24/iitm-bug-drizzle-or

  1. clone repo
  2. npm install
  3. run node index.js:
    • observe 112 exports from drizzle-orm console log
    • script terminates successfully
  4. run node --import instrument.mjs index.js
    • observe 85 exports form drizzle-orm console log (=> 27 exports missing)
    • script crashes when calling one of the missing functions (e.g. drizzle.or())

Specifications

Lms24 commented 1 month ago

This is likely a duplicate of https://github.com/nodejs/import-in-the-middle/issues/139.

I'm gonna leave this open from my end until we can confirm that #140 fixes this issue as well. If it does, sorry for the duplicate! If you wanna close this before, please feel free!

timfish commented 1 month ago

This is not a duplicate of #139 because my current PR doesn't fix this.

timfish commented 1 month ago

Oh wow, so this issue is caused by the fact that we are finding a duplicate export named or. Duplicate named exports are usually not exported up through to the root of the module but they are in this case because they come from the same source file.

A simpler reproduction: a.mjs

export const val = 1;

b.mjs

export * from './a.mjs';

c.mjs

export * from './a.mjs';
export * from './b.mjs';

When run in Node, c.mjs has a single export named val. When running through import-in-the-middle there is no export.

We are not currently checking if duplicate named exports are from the same source file.