Skip to content

fix(index): resolve sibling refs of files outside BasePath - #738

Open
pcbeingused333 wants to merge 2 commits into
pb33f:mainfrom
pcbeingused333:fix/relative-ref-outside-basepath
Open

pcbeingused333 wants to merge 2 commits into
pb33f:mainfrom
pcbeingused333:fix/relative-ref-outside-basepath

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #735

Root cause: resolveRelativeFilePath only accepted the path relative to the current file when it was inside a configured local file system root. For a file outside BasePath, pathExistsInFS rejected the correct sibling, and resolution moved on to BasePath + ref. If a file with that name existed there (here, the root spec), the ref silently resolved to the wrong document.

Fix: before trying BasePath and the other roots, the sibling of the current file is used if it exists on disk. This applies when the current file's directory is absolute, is not the root document's directory, and the sibling lies outside every root read straight from the OS (no DirFS). The root document's refs keep preferring BasePath. It does not widen what can be read: that same path is already returned, and opened, whenever no file of that name exists under a root. Virtual file systems behave as before.

Tests: TestIssue735_RelativeRefOutsideBasePath reproduces the layout from the issue. TestIssue735_RootDocumentPrefersBasePath pins the root spec case with absolute and relative SpecFilePath. Unit tests cover the OS file system, a DirFS, a relative base directory and the root document's directory. go test ./... passes.

This change was prepared with the help of an AI coding assistant.

A relative ref in a file outside every local file system root was rejected
by pathExistsInFS even though the file exists next to it, so resolution fell
back to BasePath and could pick a different document with the same name.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (ef96408) to head (6a71b83).

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #738   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          316       316           
  Lines        40239     40257   +18     
=========================================
+ Hits         40239     40257   +18     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@daveshanley daveshanley left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, the diagnosis is right and the fix works. The issue layout fails on main and passes here, including with relative paths as the issue's repro runs it, and both bundlers produce the expected output. The claim that this does not widen what can be read also holds.

Two things need sorting before it can go in, details inline:

  1. The resolver's miss paths get 22-36% slower.
  2. A root spec outside BasePath now resolves differently, and only when SpecFilePath is absolute.

Comment thread index/path_resolution.go
// The current file may live outside every configured root. A file that exists next to it still
// wins over a file with the same relative name under BasePath or another root.
if filepath.IsAbs(defRoot) && index.hasOSFileSystem() {
if info, err := os.Stat(fallback); err == nil && !info.IsDir() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This os.Stat runs on every call that gets past the first loop, and it shows up in a benchmark of resolveRelativeFilePath (main vs this branch, darwin/arm64, 8 runs, benchstat):

Case main PR Change
Sibling exists in a root 1.95µs, 6 allocs 1.97µs, 6 allocs none
Sibling missing in root, found under BasePath 4.18µs, 13 allocs 5.09µs, 16 allocs +22%
File outside roots, found under BasePath 2.92µs, 11 allocs 3.97µs, 14 allocs +36%
Missing everywhere 5.48µs, 21 allocs 6.79µs, 24 allocs +24%

In rows 2 and 4 fallback is inside an OS-backed root, so pathExistsInFS has already stat'd the same path in the loop above and failed. The second stat is a pure duplicate.

Please skip the stat when fallback is already inside an OS-backed root. I tried that locally and rows 2 and 4 went back to baseline. Row 3 keeps its one extra stat, which the fix needs.

Comment thread index/path_resolution.go Outdated

// The current file may live outside every configured root. A file that exists next to it still
// wins over a file with the same relative name under BasePath or another root.
if filepath.IsAbs(defRoot) && index.hasOSFileSystem() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also changes how the root spec's own refs resolve, which goes further than the issue asks.

Layout: root spec at dir/spec.yaml with $ref: './common.yaml#/...', BasePath set to dir/schemas, and a common.yaml in both dir/ and dir/schemas/.

  • main: resolves to dir/schemas/common.yaml.
  • This branch, absolute SpecFilePath: resolves to dir/common.yaml.
  • This branch, relative SpecFilePath (spec.yaml, BasePath: schemas, run from dir): still resolves to dir/schemas/common.yaml, because of the filepath.IsAbs(defRoot) guard.

So the same layout gives a different answer depending on how the caller spells the path, and anyone using BasePath as an override for the root document gets a silent flip.

I would like the new rule limited to files reached through a reference, which is what the issue describes, with the root document keeping its current BasePath preference. Whichever way it lands, please add a test that pins the root spec case for both absolute and relative SpecFilePath.

The sibling check now applies only outside the root document's directory,
and only stats paths that no OS file system root has already checked.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Both addressed in the new commit.

  1. The extra stat is now skipped when the path is already inside an OS-backed root. In a local benchmark of your four cases, allocations for rows 1, 2 and 4 are the same as main (6, 13, 21). Row 3 keeps the extra stat (11 allocs on main, 15 here; one more than before because of the filepath.Rel check).
  2. The new rule no longer applies to the root document, so it keeps the BasePath preference with both absolute and relative SpecFilePath, and there is a test for each. The resolver only receives the directory here, so the exception is by directory: a referenced file sitting next to the root spec also behaves as on main. If you want that distinguished by file, it means passing the source file through the callers; I can do that.

This comment was drafted with Claude Code.

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.

A relative ref in a file outside BasePath resolves against BasePath and can hit the wrong file

2 participants