Skip to content

Commit 94c68ad

Browse files
committed
cmd/go/internal/modload: finish Import implementation
modload.Import now means "figure out what this import means in the main module", including searching for a module to add to go.mod if the import is missing. (Previously it only did the search.) Fix Import to handle overlapping module file trees correctly: the fact that x/y/z is in the build list does not mean it is the module that must provide x/y/z/v2/w or even x/y/z/w. The definition of "go get x@v" is now easy to explain: if x is a module path, go get is talking about that module. Otherwise go get is talking about the module that provides or would provide x if encountered as an import in the main module. Simplify the package load process by using the new Import to eliminate the separate doMissing parallel pass, as suggested by Bryan in an earlier code review. Drop modload.SrcMod in favor of modfetch.SrcMod, and guard against SrcMod unset (filepath.Join(x, y) treats x="" as like x="."), which can happen in tests. Fixes golang/go#24851. Fixes golang/go#26048. Fixes golang/go#26250. Change-Id: I16abfa0c095492fc10ccd75f5857c1c087935867 Reviewed-on: https://go-review.googlesource.com/123095 Reviewed-by: Bryan C. Mills <[email protected]>
1 parent a42a7e8 commit 94c68ad

File tree

15 files changed

+469
-267
lines changed

15 files changed

+469
-267
lines changed

vendor/cmd/go/internal/load/pkg.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,9 @@ func LoadImport(path, srcDir string, parent *Package, stk *ImportStack, importPo
513513
} else if modErr != nil {
514514
bp = new(build.Package)
515515
err = fmt.Errorf("unknown import path %q: %v", importPath, modErr)
516+
} else if cfg.ModulesEnabled && path != "unsafe" {
517+
bp = new(build.Package)
518+
err = fmt.Errorf("unknown import path %q: internal error: module loader did not resolve import", importPath)
516519
} else {
517520
buildMode := build.ImportComment
518521
if mode&ResolveImport == 0 || path != origPath {

vendor/cmd/go/internal/modcmd/verify.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"cmd/go/internal/base"
1515
"cmd/go/internal/dirhash"
16+
"cmd/go/internal/modfetch"
1617
"cmd/go/internal/modload"
1718
"cmd/go/internal/module"
1819
)
@@ -29,9 +30,9 @@ func runVerify() {
2930

3031
func verifyMod(mod module.Version) bool {
3132
ok := true
32-
zip := filepath.Join(modload.SrcMod, "cache/download", mod.Path, "/@v/", mod.Version+".zip")
33+
zip := filepath.Join(modfetch.SrcMod, "cache/download", mod.Path, "/@v/", mod.Version+".zip")
3334
_, zipErr := os.Stat(zip)
34-
dir := filepath.Join(modload.SrcMod, mod.Path+"@"+mod.Version)
35+
dir := filepath.Join(modfetch.SrcMod, mod.Path+"@"+mod.Version)
3536
_, dirErr := os.Stat(dir)
3637
data, err := ioutil.ReadFile(zip + "hash")
3738
if err != nil {

vendor/cmd/go/internal/modfetch/cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,11 @@ func readDiskStat(path, rev string) (file string, info *RevInfo, err error) {
236236
// just to find out about a commit we already know about
237237
// (and have cached under its pseudo-version).
238238
func readDiskStatByHash(path, rev string) (file string, info *RevInfo, err error) {
239+
if SrcMod == "" {
240+
// Do not download to current directory.
241+
return "", nil, errNotCached
242+
}
243+
239244
if !codehost.AllHex(rev) || len(rev) < 12 {
240245
return "", nil, errNotCached
241246
}

vendor/cmd/go/internal/modfetch/fetch.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ var downloadCache par.Cache
2828
// local download cache and returns the name of the directory
2929
// corresponding to the root of the module's file tree.
3030
func Download(mod module.Version) (dir string, err error) {
31+
if SrcMod == "" {
32+
// Do not download to current directory.
33+
return "", fmt.Errorf("missing modfetch.SrcMod")
34+
}
35+
3136
// The par.Cache here avoids duplicate work but also
3237
// avoids conflicts from simultaneous calls by multiple goroutines
3338
// for the same version.
@@ -190,6 +195,11 @@ func readGoSum(file string, data []byte) {
190195

191196
// checkSum checks the given module's checksum.
192197
func checkSum(mod module.Version) {
198+
if SrcMod == "" {
199+
// Do not use current directory.
200+
return
201+
}
202+
193203
// Do the file I/O before acquiring the go.sum lock.
194204
data, err := ioutil.ReadFile(filepath.Join(SrcMod, "cache/download", mod.Path, "@v", mod.Version+".ziphash"))
195205
if err != nil {
@@ -245,6 +255,11 @@ func checkOneSum(mod module.Version, h string) {
245255
// Sum returns the checksum for the downloaded copy of the given module,
246256
// if present in the download cache.
247257
func Sum(mod module.Version) string {
258+
if SrcMod == "" {
259+
// Do not use current directory.
260+
return ""
261+
}
262+
248263
data, err := ioutil.ReadFile(filepath.Join(SrcMod, "cache/download", mod.Path, "@v", mod.Version+".ziphash"))
249264
if err != nil {
250265
return ""

vendor/cmd/go/internal/modget/get.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -511,24 +511,22 @@ func getQuery(path, vers string, forceModulePath bool) (module.Version, error) {
511511
return module.Version{}, err
512512
}
513513

514-
// Otherwise fall back to resolving package path as of today
515-
// and applying the version to the resulting module.
516-
// We could do more subtle things when older versions are
517-
// specified, but this seems good enough and more predictable.
518-
// If this behavior is wrong, the user can always specify the
519-
// desired module path instead of a package path,
520-
// and then the code above will handle it.
521-
repo, info, err := modload.Import(path, modload.Allowed)
514+
// Otherwise, interpret the package path as an import
515+
// and determine what module that import would address
516+
// if found in the current source code.
517+
// Then apply the version to that module.
518+
m, _, err := modload.Import(path)
522519
if err != nil {
523520
return module.Version{}, err
524521
}
525-
if vers != "latest" {
526-
// modload.Import returned "latest" version. Look up requested version.
527-
if info, err = modload.Query(repo.ModulePath(), vers, modload.Allowed); err != nil {
528-
return module.Version{}, err
529-
}
522+
if m.Path == "" {
523+
return module.Version{}, fmt.Errorf("package %q is not in a module", path)
530524
}
531-
return module.Version{Path: repo.ModulePath(), Version: info.Version}, nil
525+
info, err = modload.Query(m.Path, vers, modload.Allowed)
526+
if err != nil {
527+
return module.Version{}, err
528+
}
529+
return module.Version{Path: m.Path, Version: info.Version}, nil
532530
}
533531

534532
// isModulePath reports whether path names an actual module,

vendor/cmd/go/internal/modload/build.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
117117
}
118118

119119
if semver.IsValid(m.Version) {
120-
dir := filepath.Join(SrcMod, m.Path+"@"+m.Version)
120+
dir := filepath.Join(modfetch.SrcMod, m.Path+"@"+m.Version)
121121
if stat, err := os.Stat(dir); err == nil && stat.IsDir() {
122122
m.Dir = dir
123123
}
@@ -194,11 +194,12 @@ func PackageBuildInfo(path string, deps []string) string {
194194
}
195195

196196
func findModule(target, path string) module.Version {
197+
// TODO: This should use loaded.
197198
if path == "." {
198199
return buildList[0]
199200
}
200201
for _, mod := range buildList {
201-
if importPathInModule(path, mod.Path) {
202+
if maybeInModule(path, mod.Path) {
202203
return mod
203204
}
204205
}

0 commit comments

Comments
 (0)