diff options
author | Bryan C. Mills <bcmills@google.com> | 2023-01-20 16:25:33 -0500 |
---|---|---|
committer | Gopher Robot <gobot@golang.org> | 2023-01-31 20:33:02 +0000 |
commit | 5f00ce86334aa7e80ab825882db1a080f1b56404 (patch) | |
tree | aea0f001cf2164b02a27cda9449d17d356c13a2a /src/cmd/go/internal/modload/load.go | |
parent | de2efc2aa1448cfcf28fec66ce0ff3f979fc2f12 (diff) | |
download | go-5f00ce86334aa7e80ab825882db1a080f1b56404.tar.gz go-5f00ce86334aa7e80ab825882db1a080f1b56404.zip |
cmd/go: use Join functions instead of adding path separators to strings
Adding a file path separator is incorrect for a file path that may be
the root directory on a Unix platform (such as in a container or
chroot).
Adding a path separator is incorrect for a package path prefix that
may be the empty string (as in the "std" module in GOROOT/src).
And in both cases, a Join function is arguably clearer and simpler
anyway.
Fixes #51506 (maybe).
Change-Id: Id816930811ad5e4d1fbd206cddf219ecd4ad39a2
Reviewed-on: https://go-review.googlesource.com/c/go/+/463178
Reviewed-by: Russ Cox <rsc@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/modload/load.go')
-rw-r--r-- | src/cmd/go/internal/modload/load.go | 20 |
1 files changed, 10 insertions, 10 deletions
diff --git a/src/cmd/go/internal/modload/load.go b/src/cmd/go/internal/modload/load.go index e4f6a95320..f450ced299 100644 --- a/src/cmd/go/internal/modload/load.go +++ b/src/cmd/go/internal/modload/load.go @@ -546,9 +546,9 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str pkgNotFoundLongestPrefix := "" for _, mainModule := range MainModules.Versions() { modRoot := MainModules.ModRoot(mainModule) - if modRoot != "" && strings.HasPrefix(absDir, modRoot+string(filepath.Separator)) && !strings.Contains(absDir[len(modRoot):], "@") { - suffix := filepath.ToSlash(absDir[len(modRoot):]) - if pkg, found := strings.CutPrefix(suffix, "/vendor/"); found { + if modRoot != "" && str.HasFilePathPrefix(absDir, modRoot) && !strings.Contains(absDir[len(modRoot):], "@") { + suffix := filepath.ToSlash(str.TrimFilePathPrefix(absDir, modRoot)) + if pkg, found := strings.CutPrefix(suffix, "vendor/"); found { if cfg.BuildMod != "vendor" { return "", fmt.Errorf("without -mod=vendor, directory %s has no package path", absDir) } @@ -562,7 +562,7 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str mainModulePrefix := MainModules.PathPrefix(mainModule) if mainModulePrefix == "" { - pkg := strings.TrimPrefix(suffix, "/") + pkg := suffix if pkg == "builtin" { // "builtin" is a pseudo-package with a real source file. // It's not included in "std", so it shouldn't resolve from "." @@ -572,7 +572,7 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str return pkg, nil } - pkg := mainModulePrefix + suffix + pkg := pathpkg.Join(mainModulePrefix, suffix) if _, ok, err := dirInModule(pkg, mainModulePrefix, modRoot, true); err != nil { return "", err } else if !ok { @@ -749,17 +749,17 @@ func (mms *MainModuleSet) DirImportPath(ctx context.Context, dir string) (path s if dir == modRoot { return mms.PathPrefix(v), v } - if strings.HasPrefix(dir, modRoot+string(filepath.Separator)) { + if str.HasFilePathPrefix(dir, modRoot) { pathPrefix := MainModules.PathPrefix(v) if pathPrefix > longestPrefix { longestPrefix = pathPrefix longestPrefixVersion = v - suffix := filepath.ToSlash(dir[len(modRoot):]) - if strings.HasPrefix(suffix, "/vendor/") { - longestPrefixPath = strings.TrimPrefix(suffix, "/vendor/") + suffix := filepath.ToSlash(str.TrimFilePathPrefix(dir, modRoot)) + if strings.HasPrefix(suffix, "vendor/") { + longestPrefixPath = strings.TrimPrefix(suffix, "vendor/") continue } - longestPrefixPath = mms.PathPrefix(v) + suffix + longestPrefixPath = pathpkg.Join(mms.PathPrefix(v), suffix) } } } |