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

[Bug] RouteInfo.find() produces undefined items #20140

Closed
nelstrom opened this issue Jul 13, 2022 · 6 comments
Closed

[Bug] RouteInfo.find() produces undefined items #20140

nelstrom opened this issue Jul 13, 2022 · 6 comments

Comments

@nelstrom
Copy link

🐞 Describe the Bug

The documentation for the RouteInfo.find() method says:

Allows you to traverse through the linked list of RouteInfos from the topmost to leafmost. Returns the first RouteInfo in the linked list for which the callback returns true.

The callback method you provide should have the following signature (all parameters are optional):

function(item, index, array);

Up until v4.2 of Ember, this callback function worked as expected. Since v4.3 of Ember, the callback sometimes produces an undefined value for the item.

🔬 Minimal Reproduction

I've created a minimal reproduction.

The beforeModel hook in the 'accounts.account' route demonstrates the bug:

  beforeModel({ to }) {
    to.find(function (item, index, array) {
      console.log('accounts.account#beforeModel:', {
        item,
        index,
        array,
        parent: item?.parent,
      });
    });
  }

To exercise this code, check out the repository and run the tests.

😕 Actual Behavior

The RouteInfo.find() callback produces an undefined value for the item.

ember-bug-report

This screenshot was created while running the minimal reproduction against Ember 4.5.

🤔 Expected Behavior

The RouteInfo.find() callback should always provide a value for the item:

Screenshot 2022-07-13 at 12 31 57

This screenshot was created while running the minimal reproduction against Ember 4.2.

🌍 Environment

  • Ember: -4.3
  • Node.js/npm: v16.13.0/8.1.0
  • OS: Mac OS 12.1
  • Browser: Chrome v103

➕ Additional Context

We used git bisect to find the commit that introduced this problem. Here's the output from our bisect session:

Git bisect output

a47efb0356441c151c4c9a985059659975d4caf1 is the first bad commit
commit a47efb0356441c151c4c9a985059659975d4caf1
Date:   Thu Jan 27 15:29:23 2022 -0800
Future public @ember/routing types

package.json | 2 +-
.../-internals/glimmer/lib/components/link-to.ts | 12 +-
packages/@ember/-internals/routing/index.ts | 8 +-
.../-internals/routing/lib/ext/controller.ts | 10 +-
.../-internals/routing/lib/services/router.ts | 222 ++++----
.../-internals/routing/lib/services/routing.ts | 24 +-
.../-internals/routing/lib/system/route-info.ts | 4 +-
.../@ember/-internals/routing/lib/system/route.ts | 605 +++++++++++----------
.../@ember/-internals/routing/lib/system/router.ts | 141 ++---
.../-internals/routing/lib/system/router_state.ts | 22 +-
packages/@ember/-internals/routing/lib/utils.ts | 54 +-
.../@ember/controller/lib/controller_mixin.d.ts | 5 +-
packages/@ember/routing/auto-location.js | 1 -
packages/@ember/routing/auto-location.ts | 1 +
packages/@ember/routing/hash-location.js | 1 -
packages/@ember/routing/hash-location.ts | 1 +
packages/@ember/routing/location.js | 1 -
packages/@ember/routing/location.ts | 1 +
packages/@ember/routing/none-location.js | 1 -
packages/@ember/routing/none-location.ts | 1 +
packages/@ember/routing/route.js | 1 -
packages/@ember/routing/route.ts | 1 +
packages/@ember/routing/router-service.ts | 5 +
packages/@ember/routing/router.js | 1 -
packages/@ember/routing/router.ts | 1 +
.../routing/type-tests/auto-location.test.ts | 10 +
.../routing/type-tests/hash-location.test.ts | 10 +
.../routing/type-tests/history-location.test.ts | 10 +
packages/@ember/routing/type-tests/link-to.test.ts | 6 +
.../@ember/routing/type-tests/location.test.ts | 40 ++
.../routing/type-tests/none-location.test.ts | 10 +
.../@ember/routing/type-tests/route/index.test.ts | 72 +++
.../routing/type-tests/route/overrides.test.ts | 47 ++
.../routing/type-tests/route/query-params.test.ts | 34 ++
.../@ember/routing/type-tests/route/send.test.ts | 30 +
.../routing/type-tests/router-service.test.ts | 94 ++++
.../@ember/routing/type-tests/router/index.test.ts | 44 ++
.../@ember/routing/type-tests/router/map.test.ts | 26 +
yarn.lock | 8 +-
39 files changed, 1023 insertions(+), 544 deletions(-)
delete mode 100644 packages/@ember/routing/auto-location.js
create mode 100644 packages/@ember/routing/auto-location.ts
delete mode 100644 packages/@ember/routing/hash-location.js
create mode 100644 packages/@ember/routing/hash-location.ts
delete mode 100644 packages/@ember/routing/location.js
create mode 100644 packages/@ember/routing/location.ts
delete mode 100644 packages/@ember/routing/none-location.js
create mode 100644 packages/@ember/routing/none-location.ts
delete mode 100644 packages/@ember/routing/route.js
create mode 100644 packages/@ember/routing/route.ts
create mode 100644 packages/@ember/routing/router-service.ts
delete mode 100644 packages/@ember/routing/router.js
create mode 100644 packages/@ember/routing/router.ts
create mode 100644 packages/@ember/routing/type-tests/auto-location.test.ts
create mode 100644 packages/@ember/routing/type-tests/hash-location.test.ts
create mode 100644 packages/@ember/routing/type-tests/history-location.test.ts
create mode 100644 packages/@ember/routing/type-tests/link-to.test.ts
create mode 100644 packages/@ember/routing/type-tests/location.test.ts
create mode 100644 packages/@ember/routing/type-tests/none-location.test.ts
create mode 100644 packages/@ember/routing/type-tests/route/index.test.ts
create mode 100644 packages/@ember/routing/type-tests/route/overrides.test.ts
create mode 100644 packages/@ember/routing/type-tests/route/query-params.test.ts
create mode 100644 packages/@ember/routing/type-tests/route/send.test.ts
create mode 100644 packages/@ember/routing/type-tests/router-service.test.ts
create mode 100644 packages/@ember/routing/type-tests/router/index.test.ts
create mode 100644 packages/@ember/routing/type-tests/router/map.test.ts

We came across this bug while upgrading our Ember app from 4.2 to 4.3. Here's a (simplified) sample of the code that drew our attention to the issue:

import Route from '@ember/routing/route';
import { assert } from '@ember/debug';

export default class AccountsAccountRoute extends Route {

  beforeModel({ to }) {
    let routeInfo = to.find(({ name }) => name === 'accounts.account');
    let { account_id } = routeInfo?.params;
    assert("Couldn't find account_id parameter", account_id);
    // ...etc...
  }
}

As a work around, we've rewritten the call to find() as follows:

let routeInfo = to.find((item) => item?.name === 'accounts.account');
@chriskrycho
Copy link
Contributor

Looks like a duplicate of #20056?

@m3l1x
Copy link

m3l1x commented Aug 3, 2022

Looks like a duplicate of #20056?

Yes, it is, but a more detailed one. I could close mine if it's desired.

@chriskrycho
Copy link
Contributor

Yeah, if you could pull your comment over onto that thread, that would be helpful from a maintenance POV! Thank you!

@sly7-7
Copy link
Contributor

sly7-7 commented Aug 25, 2022

@chriskrycho 🤔 I think this is still an issue, maybe you wanted to close the other one ?

Thank you @nelstrom for the detail, I was just bitten by this. I can't see what changes in the router lib could have caused this, but eventually the work done by @wagenet around better types. tildeio/router.js@c83aeed#r82130596,

or maybe https://github.com/emberjs/ember.js/pull/19971/files could also have side effects ?

It seems like the underlyning weak map does not have the correct keys to retrieve the route infos. But I don't think the changes in the types could have an impact.

But other than that, for now I can't see what change in the commit could lead to this issue

@chriskrycho
Copy link
Contributor

It is still an issue, yes; but it's the same issue as the other one. 😄

@sly7-7
Copy link
Contributor

sly7-7 commented Aug 25, 2022

Yes, sorry, so that mean you close this one on purpose, right ? Even if it has more details than the other. In that case, should I put my comments in #20056 ?

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

No branches or pull requests

4 participants