embroider-build / ember-auto-import

Zero config import from npm packages
Other
360 stars 109 forks source link

`instanceof` fails in classic mode #588

Closed gossi closed 3 weeks ago

gossi commented 1 year ago

I'm rewriting ember-command as v2 addon. The check I have there to test for links are:

if (something instanceof Link) { ... }

if (something instanceof LinkCommand) { ... }

To ensure embroider compatibility I'm using ember-try for with embroider scenarios. By default, when starting locally, it uses classic mode.

Now I've realized the checks above are failing in classic mode. As these are two different imports under the hood for Link and LinkCommand between the transpiled addon and the host app, thus the tests are failing. My theory is, due to the different locations and two different memory pointers, that's why the checks fail.

My workaround was to use a symbol, attach it to the LinkCommand and check for the existence of the symbol... it is failing, too (I think for the same reason).

When starting the test app in embroider mode: EMBROIDER_TEST_SETUP_FORCE='embroider' ember s the checks work as expected.

NullVoxPopuli commented 1 year ago

Do you have a PR where this is easily reproducible?

Potentially related:

gossi commented 1 year ago

Here is a PR: https://github.com/gossi/ember-command/pull/22

But I think, I failed on CI setup for github, so must be done locally:

  1. Checkout
  2. pnpm install
  3. pnpm --filter ember-command run build
  4. cd test-app/ && ember s

Notes:

Also, I left a comment to this issue within the code to see when I left workarounds. Search for https://github.com/embroider-build/ember-auto-import/issues/588 inside the code.

My workaround: duck-typing and __***__ properties instead of symbols 😞

mansona commented 1 year ago

@NullVoxPopuli that is exactly the issue. The module graph essentially creates two different copies of the module so when you import something from a test file it can never be === something that is imported in an app 😞

raycohen commented 1 year ago

I am trying to convert a legacy addon into a v2 addon in a project using ember-auto-import and I think I am running into this as well

simonihmig commented 3 weeks ago

If the issue here is indeed related to https://github.com/embroider-build/ember-auto-import/issues/503, then I think we can close this now, as the bug has been fixed a wile ago! Can someone confirm?

gossi commented 3 weeks ago

I took recent main branch from ember-command today with ember-auto-import@2.7.4 to run the try exam with the ember@3.28 and that worked.

I think, this means, this can be closed. Thanks a lot everybody :)