goutil: handle Windows paths in ResolvePackage and ResolveImport - #91
Open
shane-tw wants to merge 2 commits into
Open
goutil: handle Windows paths in ResolvePackage and ResolveImport#91shane-tw wants to merge 2 commits into
shane-tw wants to merge 2 commits into
Conversation
Two path helpers assumed Unix conventions, so both misbehave on a Windows checkout. ResolvePackage switched on path[0] and only treated a leading '/' as a directory. A Windows absolute path starts with a drive letter, so it fell through to build.Import and was resolved as though it were an import path, which always fails. ResolveImport's same-package fallback derived the current package name with path.Base(path.Dir(file)). The path package is slash-only, so for C:\src\pkg\file.go it evaluates to "." and the fallback never fires, leaving same-package type references unresolvable. Both are fixed with helpers that accept either convention, so they are unit-testable on any host rather than only on Windows. No behaviour change for Unix paths. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Coverage Report for CI Build 31686645085Coverage increased (+0.2%) to 76.868%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
TestFetch drove its status-code cases off httpbin.org, which returns 503 often enough to fail unrelated builds — it failed on this branch while passing on a re-run of the same commit. An httptest server now serves the 400/500/418 responses, and the connection-failure case dials a closed local port rather than relying on a domain never resolving, which a wildcard DNS resolver can defeat. Co-Authored-By: Claude Opus 5 (1M context) <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.
Why
Two
goutilpath helpers assume Unix conventions. Found while investigating whykommentaarfails to resolve types for a colleague running a native Windows checkout (no WSL) — it calls both helpers for every type reference it looks up.What's wrong
ResolvePackageswitched onpath[0]and only treated a leading/as a directory:A Windows absolute path starts with a drive letter, so
C:\src\proj\apifell through todefaultand was resolved as if it were an import path — which can never succeed. The doc comment already promises full paths are supported.ResolveImport's same-package fallback derived the current package name from a filesystem path with the slash-onlypathpackage:For
C:\src\pkg\file.gothere is no/, sopath.Dirreturns.andcurrentPkgbecomes.. ThepkgName == currentPkgcomparison then never matches, so same-package references never resolve to"."on Windows.Both are demonstrable without a Windows machine, since the mishandling is in the parsing rather than the host:
path.Dirreturns.for a backslash path on every OS, and thepath[0]branch picks the wrong route regardless of where it runs.Fix
Two small helpers that accept either convention, so they are unit-testable on any host instead of only on a Windows runner:
isAbsPath— Unix/, Windows drive (C:\,C:/) and UNC (\\host\share).dirName— directory name of a path, treating/and\as separators.No behaviour change for Unix paths:
isAbsPathstill routes/dirtobuild.ImportDir, anddirNamereduces to the previous expression when the path contains no backslash. A literal backslash in a Unix file name would now be read as a separator, which Go source paths never rely on (noted in the doc comment).Testing
go test ./goutil/passes, including new table tests for both helpers covering Unix, drive-letter, UNC and relative inputs.TestDirName's Windows cases fail against the old expression, so they'd have caught this.GOOS=windows go build ./...andGOOS=windows go vet ./goutil/are clean. Fullgo test ./...is green apart fromhttputilx.TestFetch, which hitshttpbin.organd fails identically onmaster(503 from the external service).Not verified end-to-end on a Windows host — I don't have one. The unit tests pin the behaviour that was wrong; a sanity check from someone on Windows would be welcome.
Also in this PR
httputilx.TestFetchdrove its status-code cases offhttpbin.org, whichreturned a 503 and failed the build on this branch while the same commit passed
on a re-run. A second commit serves those fixtures from an
httptestserver, andpoints the connection-failure case at a closed local port instead of relying on a
domain never resolving (a wildcard DNS resolver defeats that). Unrelated to the
goutilchange, but it was the only thing red on CI — happy to split it out ifyou'd rather.
🤖 Generated with Claude Code