fix(resolution): same-dir component preference must compare paths at a directory boundary#683
Open
Mubashirrrr wants to merge 1 commit into
Open
Conversation
The React and Svelte framework resolvers disambiguate same-named
components by preferring one in the referencing file's directory:
const fromDir = fromFile.substring(0, fromFile.lastIndexOf('/'));
const sameDir = components.filter((n) => n.filePath.startsWith(fromDir));
`startsWith(fromDir)` has no trailing-separator boundary, so a component
in a SIBLING directory whose name merely shares a prefix (`src/ui-kit/`
vs `src/ui/`) is treated as same-directory and can win the tie — the
reference resolves to the wrong component (a wrong graph edge).
Compare the directory portions exactly instead. A small `dirOf` helper
returns the directory (or '' for a root-level file, fixing the
`lastIndexOf('/') === -1` edge where `substring(0, -1)` yielded '' and
made `startsWith('')` match every file).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
The React and Svelte framework resolvers disambiguate same-named components by preferring the one in the referencing file's directory:
startsWith(fromDir)matches on a raw string prefix with no path-separator boundary. So a component in a sibling directory whose name merely shares a prefix is wrongly treated as "same directory":src/ui/Card.tsx→fromDir = "src/ui"Widget:src/ui/Widget.tsx(the true same-dir one) andsrc/ui-kit/Widget.tsx(a prefix sibling)"src/ui-kit/Widget.tsx".startsWith("src/ui")is true, sosrc/ui-kit/Widget.tsxlands insameDirand — depending on candidate order — can be returned instead of the realsrc/ui/Widget.tsx.The result is a wrong
referencesedge to a different component in a prefix-sibling directory. This is the same data-integrity hazard the workspace-package matcher already guards against withimportPath === name || importPath.startsWith(name + '/').There's also a secondary edge: for a root-level file like
App.tsx(no/),lastIndexOf('/')is-1andsubstring(0, -1)returns'', sostartsWith('')matches every file — silently defeating the "same directory" preference entirely.Repro (fail-before / pass-after)
Added
__tests__/framework-samedir-boundary.test.ts. It drives the publicreactResolver.resolve()/svelteResolver.resolve()with two same-named components — the true same-dir one and a prefix-sibling decoy listed first — and asserts the resolver picks the same-directory component.Before this change:
After: all 3 pass. A sanity case (a genuine same-directory component listed after an unrelated one) confirms the same-dir preference itself still works.
The fix
Replace the boundary-unsafe
startsWithwith an exact directory comparison via a smalldirOfhelper (which also returns''cleanly for root-level files):Surgical:
+34/-6across the two affected resolvers, no behavior change beyond fixing the wrong-directory match.Scope notes
src/resolution/frameworks/vue.tshas the samestartsWith(fromDir)idiom, but its loop additionally gates oncomponentName === name(filename basename equals the reference name), so the directory filter there is effectively redundant for the common case and I couldn't construct a realistic wrong-result repro without a contrived filename/component-name mismatch — left untouched rather than ship a speculative change. Happy to follow up if you'd prefer it normalized for consistency.frameworks,frameworks-integration,resolution, andreact-native-bridgesuites (184 tests) all pass, andtsc --noEmitis clean. (The MCP-daemon /status-jsonCLI-spawn suites fail identically onmainin my environment — pre-existing, unrelated.)