Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignments to exported variables are not visible in other modules #38

Open
dennisjlee opened this issue Nov 22, 2023 · 2 comments
Open

Comments

@dennisjlee
Copy link

Expected Behavior

If a module exports a variable that was defined with let and can be reassigned, other modules that import that variable should always see the current value of the variable.

E.g.

env.js:

let env = { FOO: 'baz' };   // Starts with an old value

function setEnv(newEnv) {
  // Allow callers to set a new value
  env = newEnv;
}

export { env, setEnv };

If module1 calls setEnv with a new value, and module2 references env, module2 should see the new value.

Actual Behavior

With the Node flag --experimental-loader=import-in-the-middle/hook.mjs, module2 actually sees the old value.

Steps to Reproduce the Problem

I made a repro here: https://github.com/dennisjlee/iitm-assign-module-var-repro

Specifications

  • Version: import-in-the-middle 1.4.2
  • Platform: Node 18.18.2 or 20.9.0
@timfish
Copy link
Contributor

timfish commented Jun 28, 2024

Because the iitm wrappers assign each export to a variable before re-exporting, primitive exports have their original value copied and the reference is lost.

This is where the code generation does that:

let $${n} = _.${n}
export { $${n} as ${n} }

Possible solutions/workarounds? I dont think there's much we can actually do to fix this.

@dennisjlee
Copy link
Author

@timfish I encountered this issue when working with a SvelteKit application. I was trying to update our NewRelic Node.js agent to a more recent version; NewRelic switched to using import-in-the-middle to instrument module loading in v11 (release notes).

When I was testing that setup with the SvelteKit app, this turned out to be a blocking bug because of this module in SvelteKit that relies on reassigning exported symbols, like my simplified repro above.

With that said, I am no longer working on that project so this is no longer important to me personally, but I expect that this issue could affect other users of import-in-the-middle via instrumentation tools like NewRelic or DataDog, depending on the libraries/frameworks they need to import.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants