-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/go/vcs: FromDir documentation and root return value #7723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Easy doc fix. Should do for 1.3 Labels changed: added release-go1.3, repo-tools. Owner changed to @robpike. Status changed to Accepted. |
I have a couple of follow-up questions, to make sure I get this right. The `importPath` comment is erroneous as you state, but I'm wondering if you could provide some context about the other two points you make. Perhaps an example of a directory layout for a given repository and a mismatch between what you expect from FromDir and what it returns instead. "- the returned root is a filesystem path, but documenting it as an "import path" suggests it'll always be slash-separated. Doc bug or code bug?" Can you clarify this, as well? What was the import path separated by if not by slashes? |
On Windows, the root parameter contains backslashes. But an import path is conventionally slash-separated, not a filepath as implemented here. (It's a slice of dir.) My middle point above was due to my confusion about what to provide for srcRoot. For a dir of $GOPATH/src/foo/bar, I had been providing $GOPATH and getting back src/foo. Providing $GOPATH/src yields just foo, as documented. A sentence documenting the expected form of srcRoot would have saved me a little time. |
I also ran into the invalid
It should be
You're right,
I can confirm, and I think it's a bug. Here's the rationale. The documentation says:
If I understand correctly, an import path is defined as a However, as @kr said, on Windows, there's a bug in that the returned Consider this snippet being executed on Windows: pkg, err := build.Import("example.com/repo/subpackage", "", build.FindOnly)
if err != nil {
panic(err)
}
_, root, err := FromDir(pkg.Dir, pkg.SrcRoot)
if err != nil {
panic(err)
}
fmt.Println(root) On Windows, that snippet will print The fix is simple: diff --git a/go/vcs/vcs.go b/go/vcs/vcs.go
index a702496..b615508 100644
--- a/go/vcs/vcs.go
+++ b/go/vcs/vcs.go
@@ -348,7 +348,7 @@ func FromDir(dir, srcRoot string) (vcs *Cmd, root string, err error) {
for len(dir) > len(srcRoot) {
for _, vcs := range vcsList {
if fi, err := os.Stat(filepath.Join(dir, "."+vcs.Cmd)); err == nil && fi.IsDir() {
- return vcs, dir[len(srcRoot)+1:], nil
+ return vcs, filepath.ToSlash(dir[len(srcRoot)+1:]), nil
}
}
I can make a CL that fixes this issue. |
I've created https://golang.org/cl/18461 that resolves this issue. |
CL https://golang.org/cl/18461 mentions this issue. |
CL https://golang.org/cl/21345 mentions this issue. |
Apply golang/tools@5804fef. In the context of cmd/go build tool, import path is a '/'-separated path. This can be inferred from `go help importpath` and `go help packages`. vcsFromDir documentation says on return, root is the import path corresponding to the root of the repository. On Windows and other OSes where os.PathSeparator is not '/', that wasn't true since root would contain characters other than '/', and therefore it wasn't a valid import path corresponding to the root of the repository. Fix that by using filepath.ToSlash. Add test coverage for vcsFromDir, it was previously not tested. It's taken from golang.org/x/tools/go/vcs tests, and modified to improve style. Additionally, remove an unneccessary statement from the documentation "(thus root is a prefix of importPath)". There is no variable importPath that is being referred to (it's possible p.ImportPath was being referred to). Without it, the description of root value matches the documentation of repoRoot.root struct field: // root is the import path corresponding to the root of the // repository root string Rename and change signature of vcsForDir(p *Package) to vcsFromDir(dir, srcRoot string). This is more in sync with the x/tools version. It's also simpler, since vcsFromDir only needs those two values from Package, and nothing more. Change "for" to "from" in name because it's more consistent and clear. Update usage of vcsFromDir to match the new signature, and respect that returned root is a '/'-separated path rather than a os.PathSeparator separated path. Fixes #15040. Updates #7723. Helps #11490. Change-Id: Idf51b9239f57248739daaa200aa1c6e633cb5f7f Reviewed-on: https://go-review.googlesource.com/21345 Reviewed-by: Alex Brainman <[email protected]> Run-TryBot: Alex Brainman <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
Import path is a '/'-separated path. FromDir documentation says on return, root is the import path corresponding to the root of the repository. On Windows and other OSes where os.PathSeparator is not '/', that wasn't true since root would contain characters other than '/', and therefore it wasn't a valid import path corresponding to the root of the repository. Fix that by using filepath.ToSlash. Add test coverage for root value returned from FromDir, it was previously not tested. Additionally, remove a dubious statement from the documentation "(thus root is a prefix of importPath)". There is no variable importPath that is being referred to. It's also redundant and confusing. Without it, the description of root value matches the documentation of RepoRoot.Root struct field: // Root is the import path corresponding to the root of the // repository. Root string Fixes golang/go#7723. Change-Id: If9f5f55b5751e01a7f88b79d9b039402af3e9312 Reviewed-on: https://go-review.googlesource.com/18461 Reviewed-by: Chris Manghane <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]>
The text was updated successfully, but these errors were encountered: