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
72 stars 27 forks source link

Introduce failing test to recreate openai shim issue #113

Open ericallam opened 5 months ago

ericallam commented 5 months ago

Using iitm 1.8.1 in an ESM project that imports the openai package leads to the following exception:

TypeError: getDefaultAgent is not a function

getDefaultAgent is part of the openai shims system, which automatically sets certain platform/runtime specific values using module side-effects. I've recreated a simplified version of this issue in a test file (I may have put the test file in the wrong place, please advise and I can move it if needed). You can see the test pass with the following:

node --require ./test/version-check.js  test/hook/side-effects.mjs

And fail with the loader added:

node --require ./test/version-check.js --experimental-loader ./test/generic-loader.mjs test/hook/side-effects.mjs

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ undefined
- 'node'
    at file:///Users/eric/code/triggerdotdev/opensource/import-in-the-middle/test/hook/side-effects.mjs:4:1
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: undefined,
  expected: 'node',
  operator: 'strictEqual'
}

I'm not exactly sure what the issue is, but it does look like possibly the second import of registry.mjs in the shims file maybe just uses a module cache, instead of re-importing the module:

import * as shims from "./registry.mjs";
import * as auto from "./runtime.mjs";

if (!shims.kind) {
  shims.setShims(auto.getRuntime());
}

export * from "./registry.mjs";
ericallam commented 5 months ago

Seems like other people are running into this issue in the wild, as seen in this issue here: https://github.com/openai/openai-node/issues/903

timfish commented 5 months ago

I'm looking into this! Thanks for the test case.

timfish commented 5 months ago

@ericallam do you happen to have the openai code that causes TypeError: getDefaultAgent is not a function?

ericallam commented 5 months ago

@timfish full reproduction with OpenAI SDK here: https://github.com/ericallam/iitm-openai

This is the bit of code I copied from them in the test case: https://github.com/openai/openai-node/blob/master/src/_shims/index.mjs

guidev commented 4 months ago

Any update? this makes Datadog unusable in any node project that uses the openai sdk...

timfish commented 4 months ago

The issue with openai highlights a fundamental flaw in import-in-the-middle (#38) that probably can't be solved with the current Node loaders API.

I have no idea how DataDog initialises import-in-the-middle but this PR adds the ability to programatically skip wrapping of incompatible modules to work around this issue for now.