Skip to content

fix(resolve): a paths wildcard whose prefix and suffix overlap an import no longer panics - #358

Open
sunerpy wants to merge 1 commit into
mainfrom
fix/path-alias-overlap
Open

sunerpy wants to merge 1 commit into
mainfrom
fix/path-alias-overlap

Conversation

@sunerpy

@sunerpy sunerpy commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Summary

A tsconfig/jsconfig paths key with text on both sides of its *, such as "@x/*/index", made codegraph init panic on an import like @x/index:

thread '<unnamed>' panicked at crates/codegraph-resolve/src/path_aliases.rs:453:24:
byte range starts at 3 but ends at 2

apply_aliases checked starts_with(prefix) and ends_with(suffix) separately and then sliced import_path[prefix.len()..len - suffix.len()]. @x/index passes both checks only by counting its one / twice, so the slice range is reversed. Init exits 101 and leaves the index in phase=building, unreadable. In an indexed project, the sync that picks up such an import panics, and so does every later sync.

The fix applies TypeScript's isPatternMatch length rule: a wildcard pattern matches only an import at least as long as its prefix and suffix together. A shorter import skips the pattern, so a less specific pattern may still match it. An empty capture at exactly prefix + suffix (@x//index) still matches, as tsc allows.

This ports upstream colbymchenry/codegraph 77ee3484 (#2969). Upstream's JS slice returned an empty string there and linked the import to widgets/index; this build panicked instead.

Graph impact

No extraction version move. A repository with this shape cannot finish an index on v0.56.0, and every other repository's graph is unchanged: scripts/regen-goldens.sh --check reports all 32 corpora identical.

Tests

  • crates/codegraph-resolve/src/path_aliases.rs: four unit tests mirroring upstream's applyAliases cases. They cover: the overlap does not match; the search falls through past an overlapping pattern (once with upstream's two @x/ patterns in tsc's declaration order, once through the loader with a shorter prefix); the exact-length empty capture still matches. The first three panic on main at path_aliases.rs:453:24. The exact-length case passes on both, as upstream reports for its own suite.
  • crates/codegraph-cli/tests/sync_convergence: typescript_tsconfig_overlapping_wildcard_import_appears and typescript_tsconfig_overlapping_wildcard_import_disappears, matrix cases beside the other tsconfig paths cases. On main, the sync (appears) and the init (disappears) exit 101. With the fix, each sync equals a fresh index, and the import links to neither src/widgets/index.ts nor its trap.

Validation

Command Result
cargo test -p codegraph-resolve --locked path_alias 36 passed
cargo test -p codegraph-resolve --locked all passed (lib: 2072)
cargo test -p codegraph-rs --locked --test sync_convergence overlapping_wildcard 2 passed
cargo test -p codegraph-rs --locked --test sync_convergence -- tsconfig jsconfig alias paths_ 62 passed
cargo clippy -p codegraph-resolve -p codegraph-rs --all-targets --locked -- -D warnings clean
cargo fmt --all --check clean
python3 scripts/docs-check.py OK
scripts/regen-goldens.sh --check with this branch's release codegraph and bench 32 identical
Release-binary probe on the #2969 project init exits 0 and status is current; adding the import, then removing it, syncs cleanly, and each synced index equals a fresh init

All cargo commands ran with env -u RUSTUP_TOOLCHAIN (the pinned 1.98.0 toolchain).

Not in this PR: paths key order

load_project_aliases reads compilerOptions.paths into a serde_json::Map built without preserve_order, so the keys come out sorted. Among patterns whose prefixes have the same length, the stable specificity sort keeps that order, while tsc breaks such ties by declaration order. With {"@x/*/index": ["widgets/*/index"], "@x/*": ["lib/*"]}, tsc and upstream send @x/button/index to widgets/button/index. This build sends it to lib/button/index, which does not exist, so the import stays unresolved and press links by name only. Matching tsc's order changes the graph, so it is tracked separately with the other graph-changing work and is not part of this crash fix.

The pre-push make pre-ci passed on 0e16434. This is owner decision OD-C10: the crash was found by the v1.6.4 upstream audit and reproduced on the official v0.56.0 binary, and it is fixed in the v0.56.1 patch.

…ort no longer panics

A tsconfig/jsconfig `paths` key with text on both sides of its `*`, like
`@x/*/index`, splits into the prefix `@x/` and the suffix `/index`.
apply_aliases checked starts_with and ends_with separately, then sliced
import_path[prefix.len()..len - suffix.len()]. An import of `@x/index`
starts with the one and ends with the other only by counting its one `/`
twice, so the slice ended before it began and panicked ("byte range
starts at 3 but ends at 2"). `codegraph init` exited 101 and left the
index in phase=building, unreadable. In an indexed project, the sync that
picked up such an import panicked the same way, and so did every sync
after it.

A wildcard pattern now matches only an import at least as long as its
prefix and suffix together, TypeScript's own isPatternMatch rule. A
shorter import skips the pattern, and a less specific pattern may still
match it. An empty capture at exactly the prefix plus the suffix
(`@x//index`) still matches, as tsc allows. This ports upstream 77ee3484
(#2969); upstream's string slice did not throw there, and instead linked
the import to the file the empty capture named.

No extraction version move: a repository with this shape cannot finish
an index today, and every other graph is unchanged. All 32 golden
corpora check identical.

Evidence: the upstream audit reproduced the panic on the official
v0.56.0 binary, for both init and sync. The new applyAliases unit tests
mirror upstream's cases: the overlap, the fall-through to a less specific
pattern (in tsc's declaration order, and through the loader with a
shorter prefix), and the exact-length capture. The first three panic on
the unchanged code; the exact-length case passes on both. The new sync
convergence cases typescript_tsconfig_overlapping_wildcard_import_appears
and _disappears fail on the unchanged code (sync and init exit 101); with
the fix, each sync equals a fresh index.
@codecov

codecov Bot commented Nov 27, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #358      +/-   ##
==========================================
- Coverage   96.40%   96.40%   -0.01%     
==========================================
  Files         368      368              
  Lines      254628   254704      +76     
==========================================
+ Hits       245468   245541      +73     
- Misses       9160     9163       +3     
Files with missing lines Coverage Δ
crates/codegraph-resolve/src/path_aliases.rs 98.89% <100.00%> (+0.11%) ⬆️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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.

1 participant