ember-auto-import icon indicating copy to clipboard operation
ember-auto-import copied to clipboard

`instanceof` fails in classic mode

Open gossi opened this issue 1 year ago • 4 comments

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.

gossi avatar Jul 13 '23 19:07 gossi

Do you have a PR where this is easily reproducible?

Potentially related:

  • https://github.com/embroider-build/ember-auto-import/issues/503

NullVoxPopuli avatar Jul 14 '23 23:07 NullVoxPopuli

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:

  • The last test that is failing is: Rendering | Component | <CommandElement>: invoke command
  • When starting with EMBROIDER_TEST_SETUP_FORCE='embroider' ember s that test will pass.
  • The code in question: https://github.com/gossi/ember-command/blob/80b753cef18b8514f4d4ea89da0583f4724dec93/ember-command/src/-private/instance.ts#L137

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 😞

gossi avatar Jul 15 '23 09:07 gossi

@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 😞

mansona avatar Jul 15 '23 12:07 mansona

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

raycohen avatar Aug 05 '23 13:08 raycohen

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?

simonihmig avatar Sep 09 '24 07:09 simonihmig

I took recent main branch from ember-command today with [email protected] to run the try exam with the [email protected] and that worked.

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

gossi avatar Sep 10 '24 13:09 gossi