embroider-build / ember-auto-import

Zero config import from npm packages
Other
361 stars 110 forks source link

[2.7.3] side-effect imports are dropped #628

Closed runspired closed 2 weeks ago

runspired commented 2 weeks ago

I have a v2-addon with an entry-point designed to be a side-effect import. The module has export {} because some tools will DCE it otherwise, but overall its just a pure side-effect.

In the test app of a classic ember-cli app build using latest ember-auto-import and ember-cli I am importing the module in a test file to ensure it correctly performs its side-effect. It is not, because as it turns out the import of this side-effect module is dropped during build.

If I change the code to import * as foo from 'side-effect-module'; console.log(foo); the module import will not be dropped.

NullVoxPopuli commented 2 weeks ago

I could not replicate locally -- side-effecting imports work as expected in a fresh app.

(@runspired and I are pairing on this though)

NullVoxPopuli commented 2 weeks ago

current hypothesis is that something has gone very wrong with pnpm + dep management since the lockfile reveals that it is ignoring overrides 🙈

(auto-import and embroider/shared-internals (etc) were not up to date)

NullVoxPopuli commented 2 weeks ago

Gonna close for now. We can re open if there is a smell of auto-import (2.7.3+) issue later

runspired commented 2 weeks ago

this branch has the issue: https://github.com/emberjs/data/pull/9479 I've worked around for now by having the test assign the fake export from the side-effect to "this"

runspired commented 2 weeks ago

also I'm not convinced this is a pnpm issue where we ended up with the wrong auto-import, yes the wrong version is present some places in the project but we didn't narrow down to figuring out if the wrong one was present in the specific test app

ef4 commented 2 weeks ago

I'm running the reproduction and I see the same behavior. But I can see that ember-auto-import did include the module. It's the import statement in the test module that has been deleted.

If you comment out this line, the bug goes away and the side-effectful import remains.

So I think the problem is somewhere in your custom babel plugins.

runspired commented 2 weeks ago

@ef4 those plugins have already run before this ever occurs, and by commenting them out, all you've done is prevented auto-import and macros from running correctly at all.

runspired commented 2 weeks ago

I can't reopen this but its definitely not resolved and definitely not the fault of the babel plugins

ef4 commented 2 weeks ago

You could have given me the slightest benefit of the doubt and debugged those babel plugins to confirm, but OK, I did it for you. Here is where they are deleting side-effectful imports:

https://github.com/emberjs/data/blob/bb493919857cd86004c303fccbcdfe47852f8eef/packages/build-config/cjs-src/transforms/babel-plugin-transform-logging.js#L58-L60

ef4 commented 2 weeks ago

Most likely that code was intended to be guarded by the if statement above it, but it is not. So it deletes all side-effectful imports.