Skip to content

Fix wildcard queries for dontFragment relations#5

Merged
codehz merged 4 commits intomasterfrom
copilot/fix-wildcard-query-issue
Nov 24, 2025
Merged

Fix wildcard queries for dontFragment relations#5
codehz merged 4 commits intomasterfrom
copilot/fix-wildcard-query-issue

Conversation

Copy link
Contributor

Copilot AI commented Nov 24, 2025

Wildcard relation queries like relation(ChildOf, '*') failed to match entities when the component had dontFragment: true. This occurred because dontFragment relations are stored separately from archetype signatures to prevent fragmentation, but wildcard matching only checked archetype component types.

Changes

  • Archetype matching: Added hasRelationWithComponentId() to check both archetype components and per-entity dontFragment relations
  • Query filtering: Modified Query.getEntities() to filter at entity level for wildcard queries, since dontFragment archetypes can contain entities with/without the relation
  • Iteration support: Updated buildWildcardRelationValue() to include dontFragment relations when iterating with forEach()

Example

const ChildOf = component({ dontFragment: true });
const parent1 = world.new();
const parent2 = world.new();

const child1 = world.new();
world.set(child1, relation(ChildOf, parent1));

const child2 = world.new();
world.set(child2, relation(ChildOf, parent2));

world.sync();

// Previously returned 0 entities, now correctly returns both
const query = world.createQuery([relation(ChildOf, "*")]);
const entities = query.getEntities(); // [child1, child2]
Original prompt

发现对于dontFragment的组件,wildcard查询失效了,如查询relation(ChildOf, '*'),在ChildOf为dontFragment组件时无法查询到,请编写测试并修复这个问题

The user has attached the following file paths as relevant context:

  • src/world.ts
  • src/archetype.ts
  • src/entity.ts
  • .github/copilot-instructions.md

Created from VS Code via the GitHub Pull Request extension.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits November 24, 2025 01:47
…onents

Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
- Added hasRelationWithComponentId method to Archetype to check for relations including dontFragment
- Updated getMatchingArchetypes to use new method for wildcard relation filtering
- Modified Query.getEntities to filter entities with wildcard relations at entity level for dontFragment components
- Enhanced buildWildcardRelationValue to include dontFragment relations in iteration
- Added comprehensive tests for wildcard queries with dontFragment components

Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
- Add null safety check for wildcard relation results in Query.getEntities
- Improve error message with component ID and entity ID for better debugging

Co-authored-by: codehz <13158903+codehz@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix wildcard query failure for dontFragment component Fix wildcard queries for dontFragment relations Nov 24, 2025
Copilot AI requested a review from codehz November 24, 2025 01:58
@codehz
Copy link
Owner

codehz commented Nov 24, 2025

@copilot getMatchingArchetypes 的改动存在问题:由于dont fragment component 的变化不会影响archetype的shape,因而当entity添加这种组件时不会在后续触发 checkNewArchetype,这导致这个写法可能错误的提前过滤了archetype,query可能会漏了这部分实体
一个想法是,设立一个wildcard relation的index,标记到archetype的签名里,这里可以参考 flecs 的设计 (我们只需要处理(Comp, *)这种情况)

@codehz codehz marked this pull request as ready for review November 24, 2025 02:24
@codehz codehz merged commit 0b36e63 into master Nov 24, 2025
1 check failed
@codehz codehz deleted the copilot/fix-wildcard-query-issue branch November 24, 2025 02:24
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.

2 participants