Repository navigation
fix(index): resolve sibling refs of files outside BasePath - #738
pcbeingused333 wants to merge 2 commits into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
daveshanley
left a comment
There was a problem hiding this comment.
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:
- The resolver's miss paths get 22-36% slower.
- A root spec outside
BasePathnow resolves differently, and only whenSpecFilePathis absolute.
| // 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() { |
There was a problem hiding this comment.
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.
|
|
||
| // 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() { |
There was a problem hiding this comment.
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 todir/common.yaml. - This branch, relative
SpecFilePath(spec.yaml,BasePath: schemas, run fromdir): still resolves todir/schemas/common.yaml, because of thefilepath.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>
|
Both addressed in the new commit.
This comment was drafted with Claude Code. |
Fixes #735
Root cause:
resolveRelativeFilePathonly accepted the path relative to the current file when it was inside a configured local file system root. For a file outsideBasePath,pathExistsInFSrejected the correct sibling, and resolution moved on toBasePath+ ref. If a file with that name existed there (here, the root spec), the ref silently resolved to the wrong document.Fix: before trying
BasePathand 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 (noDirFS). The root document's refs keep preferringBasePath. 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_RelativeRefOutsideBasePathreproduces the layout from the issue.TestIssue735_RootDocumentPrefersBasePathpins the root spec case with absolute and relativeSpecFilePath. Unit tests cover the OS file system, aDirFS, 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.