machty / ember-concurrency

ember-concurrency is an Ember Addon that enables you to write concise, worry-free, cancelable, restartable, asynchronous tasks.
http://ember-concurrency.com
MIT License
691 stars 155 forks source link

Usage in a V2 addon #543

Closed IgnaceMaes closed 4 months ago

IgnaceMaes commented 10 months ago

I hit the following issue when using ember-concurrency in a brand new V2 addon:

Error: Assertion Failed: It appears you're attempting to use the new task(async () => { ... }) syntax, but the async arrow task function you've provided is not being properly compiled by Babel.

Possible causes / remedies:

1. You must pass the async function expression directly to the task() function (it is not currently supported to pass in a variable containing the async arrow fn, or any other kind of indirection)
2. The new task syntax is only supported by native classes. Ensure that this is one.
3. If this code is in an addon, please ensure the addon specifies ember-concurrency "2.3.0" or higher in "dependencies" (not "devDependencies")
4. Ensure that there is only one version of ember-concurrency v2.3.0+ being used in your project (including nested dependencies) and consider using npm/yarn/pnpm resolutions to enforce a single version is used
    at _assert (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/debug/index.js:128:1)
    at task (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/addon-tree-output/ember-concurrency/-private/task-public-api.js:57:1)
    at new redactedService (webpack://__ember_auto_import__/../ember-redacted/dist/services/redacted.js?:14:862)
    at Function.create (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/object/core.js:376:1)
    at InternalFactoryManager.create (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/container/index.js:376:1)
    at Proxy.create (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/container/index.js:205:1)
    at instantiateFactory (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/container/index.js:277:1)
    at lookup (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/container/index.js:224:1)
    at Container.lookup (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/container/index.js:122:1)
    at ApplicationInstance.lookup (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/runtime/lib/mixins/container_proxy.js:19:1)
    at ApplicationRoute.getInjection (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/metal/index.js:2033:1)
    at /var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/metal/index.js:1277:1
    at untrack (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@glimmer/validator.js:645:1)
    at ComputedProperty.get (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/metal/index.js:1276:1)
    at ApplicationRoute.getter [as redacted] (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/@ember/-internals/metal/index.js:854:1)
    at ApplicationRoute.beforeModel (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/test-app/routes/application.js:22:1)
    at UnresolvedRouteInfoByParam.runBeforeModelHook (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/router_js.js:682:1)
    at /var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/router_js.js:606:1
    at invokeCallback (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/rsvp.js:435:1)
    at publish (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/rsvp.js:421:1)
    at invokeWithOnError (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/backburner.js.js:282:1)
    at Queue.flush (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/backburner.js.js:188:1)
    at DeferredActionQueues.flush (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/backburner.js.js:344:1)
    at Backburner._end (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/backburner.js.js:773:1)
    at Backburner._boundAutorunEnd (/var/folders/9h/5lcgcj596r94wnt0shgmjdjc0000gp/T/broccoli-855363sN2uagIsO3F/out-212-broccoli_merge_trees/assets/backburner.js.js:509:1)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

After a bit of fiddling around I found that manually registering the Babel plugin resolves this. This can be done by adding the following line in the your-v2-addon/babel.config.json file:

{
  "presets": [["@babel/preset-typescript"]],
  "plugins": [
    // .. other plugins
    // Add:
    "./node_modules/ember-concurrency/lib/babel-plugin-transform-ember-concurrency-async-tasks"
  ]
}

Is this intended behaviour? And if so, should it be documented?

barryofguilder commented 10 months ago

I ran into this issue last week too as well as when I try to call perform on a task in my V2 addon, I get this error:

Screenshot 2023-08-25 at 4 20 30 PM

ui-button.js:43 Uncaught TypeError: this.buttonTask.perform is not a function

Edit: When I manually registered the Babel plugin like what you said, it also fixes this issue too.

VincentMolinie commented 9 months ago

I had the exact same issue but inside my app (not an addon). While investigating, I realised the culprit was the coverage 🤔 If you are trying to launch you test with coverage like so COVERAGE=true ember build --environment=test this will fail.

To fix it your solution works, we just have to specify the ember-concurrency plugin BEFORE the one of ember coverage

    // ember-cli-build.js
    babel: {
      plugins: [
        require.resolve('ember-auto-import/babel-plugin'),
        require.resolve(
          'ember-concurrency/lib/babel-plugin-transform-ember-concurrency-async-tasks',
        ),
        ...EmberCodeCoverage.buildBabelPlugin(),
      ],
      sourceMaps: 'inline',
    },
barryofguilder commented 9 months ago

@VincentMolinie that's interesting. I'm not using ember coverage in my addon, so I still think there's something else going on.

mbp101 commented 9 months ago

I'm having a similar issue with using perform in a brand new Ember 5.3 app. I get a perform is not defined error.

I tried manually registering the Babel plugin but no success so far.

mkszepp commented 5 months ago

I have also got this error while migrating the addon to v2 (fixed like described in comment)... is there planned to convert this addon to v2? Its the last of my dependency (excluded cli's) which is not a v2 addon

machty commented 5 months ago

@mkszepp Definitely plans to convert this addon to v2 but I am personally too busy to do it right now and I'd have to do a lot of catching up to understand everything that needs to happen. Would love to have a champion to help out with this, and I think the community could provide plenty of help in the #e-concurrency Discord channel.

machty commented 5 months ago

I'm having a similar issue with using perform in a brand new Ember 5.3 app. I get a perform is not defined error.

I tried manually registering the Babel plugin but no success so far.

Can you provide more information about this? Is it possible you're perform in a .gts/.gjs file and you don't have an import for it?

mkszepp commented 4 months ago

The addon is now migrated to an addon V2 (see https://github.com/machty/ember-concurrency/releases/tag/4.0.0). Thank you @machty and everyone which has helped to bring this to an V2 addon. 🎉

The original reported error should resolved with v4 release. I think the problem with perform is not directly related to this issue.

aklkv commented 3 months ago

It seems that even with v4 released, v2 addons still need to have following

node_modules/ember-concurrency/async-arrow-task-transform

in babel.config.json would it make sense to add this information to migration guides?

mkszepp commented 3 months ago

@aklkv it is already documented here http://ember-concurrency.com/docs/v4-upgrade and here https://github.com/machty/ember-concurrency?tab=readme-ov-file#installation

As reminder: v2 addons are static (like non ember addons) and not dynamic like v1 addons, for this reason this is generally necessary

aklkv commented 3 months ago

@mkszepp it documents everything but v2 addon which might be confusing as it was for me, some might assume that it work would work without any extra configurations in v2 environment

mkszepp commented 3 months ago

Addons V2 are static, magically parts like in V1 addons are not possible.

V2 addons are working like every other addon which you get from npm (excluded ember addons). There is always necessary a little configuration. You will see this manual steps in future in more and more modernized addons.

Here the RFC link to V2 addon format, maybe this helps you better to understand this way https://rfcs.emberjs.com/id/0507-embroider-v2-package-format/

aklkv commented 3 months ago

I am not sure if I am not communicating my question succinctly here PR illustrates what I mean