Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

7nik
Copy link
Contributor

@7nik 7nik commented Sep 6, 2025

Closes #15339

It looks like duplicate keys cause crashing only in one place.
In some cases it still can successfully render the list but will crash soon anyway, so I don't see reasons to add other checks.

Before submitting the PR, please make sure you do the following

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • Prefix your PR title with feat:, fix:, chore:, or docs:.
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.
  • If this PR changes code within packages/svelte/src, add a changeset (npx changeset).

Tests and linting

  • Run the tests with pnpm test and lint the project with pnpm lint

Copy link

changeset-bot bot commented Sep 6, 2025

🦋 Changeset detected

Latest commit: 89a3f0f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot
Copy link

Copy link
Contributor

github-actions bot commented Sep 6, 2025

Playground

pnpm add https://pkg.pr.new/svelte@16724

@Rich-Harris
Copy link
Member

I feel a bit weird about adding a less-cryptic error for when matched is empty, but still not erroring at all if there are duplicate keys that don't result in an error. Is there some other proxy, besides matched being empty, for those other cases?

I also realised as a result of this PR that validate_each_keys doesn't always work, because the render_effect could run after the block update — for the test case in this PR, it fails with a is undefined. We could change that by turning the render_effect in validate_each_keys to a block, but maybe it would be better to move the check into reconcile inside an if (DEV) block?

@7nik
Copy link
Contributor Author

7nik commented Sep 11, 2025

Checking other cases requires computations. We cannot even do array.length === state.items.size - to_destroy.length (key duplication often results into a shortened list) because state.items may still contain transiting items from previous runs. Probably the simplest computation is a set of seen keys in the reconcile - validate keys during reconciling. Another place is inside create_item. If we want to add any computation to prod.

@dummdidumm
Copy link
Member

I vote for merging this. I just ran into this and it's really annoying to debug. Yes it's not going to catch all cases but I'd rather fail with a runtime error that gives me a hint at what goes wrong compared to a cryptic "undefined" runtime error.

@7nik
Copy link
Contributor Author

7nik commented Sep 12, 2025

I tried to move validate_each_keys inside reconcile but some tests failed, and I didn't get time to dig into why.

@7nik
Copy link
Contributor Author

7nik commented Sep 13, 2025

I moved validate_each_keys inside #each's block because, if thrown in reconcile, the error isn't caught by the boundary in async mode. Guess it is because reconcile is called in batch callbacks.

@7nik
Copy link
Contributor Author

7nik commented Sep 13, 2025

Btw, in prod the error isn't caught by boundary. I need help here.

@7nik
Copy link
Contributor Author

7nik commented Sep 14, 2025

Added the error emitting for the non-crashing case.
It doesn't emit the error at hydration in prod, but I guess it's fine.

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

Successfully merging this pull request may close these issues.

Cannot read properties of undefined (reading 'prev')
4 participants