Skip to content

Commit aa476ba

Browse files
author
Bryan C. Mills
committed
cmd/go/internal/modload: refactor pathInModuleCache
I found the control flow of this function a bit tricky to reason about due to nesting and interaction between conditions and iteration. This change factors out a helper function that can return early instead of mixing conditionals and 'continue' statements. Also remove the (unused) ModuleUsedDirectly function. For #36460 Change-Id: I60a2a5a1b32989e5a17a14e1a8c858b280cda8f2 Reviewed-on: https://go-review.googlesource.com/c/go/+/251998 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
1 parent 564b350 commit aa476ba

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

src/cmd/go/internal/modload/load.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ var (
374374
// pathInModuleCache returns the import path of the directory dir,
375375
// if dir is in the module cache copy of a module in our build list.
376376
func pathInModuleCache(dir string) string {
377-
for _, m := range buildList[1:] {
377+
tryMod := func(m module.Version) (string, bool) {
378378
var root string
379379
var err error
380380
if repl := Replacement(m); repl.Path != "" && repl.Version == "" {
@@ -388,13 +388,26 @@ func pathInModuleCache(dir string) string {
388388
root, err = modfetch.DownloadDir(m)
389389
}
390390
if err != nil {
391-
continue
391+
return "", false
392392
}
393-
if sub := search.InDir(dir, root); sub != "" {
394-
sub = filepath.ToSlash(sub)
395-
if !strings.Contains(sub, "/vendor/") && !strings.HasPrefix(sub, "vendor/") && !strings.Contains(sub, "@") {
396-
return path.Join(m.Path, filepath.ToSlash(sub))
397-
}
393+
394+
sub := search.InDir(dir, root)
395+
if sub == "" {
396+
return "", false
397+
}
398+
sub = filepath.ToSlash(sub)
399+
if strings.Contains(sub, "/vendor/") || strings.HasPrefix(sub, "vendor/") || strings.Contains(sub, "@") {
400+
return "", false
401+
}
402+
403+
return path.Join(m.Path, filepath.ToSlash(sub)), true
404+
}
405+
406+
for _, m := range buildList[1:] {
407+
if importPath, ok := tryMod(m); ok {
408+
// checkMultiplePaths ensures that a module can be used for at most one
409+
// requirement, so this must be it.
410+
return importPath
398411
}
399412
}
400413
return ""
@@ -568,12 +581,6 @@ func PackageImports(path string) (imports, testImports []string) {
568581
return imports, testImports
569582
}
570583

571-
// ModuleUsedDirectly reports whether the main module directly imports
572-
// some package in the module with the given path.
573-
func ModuleUsedDirectly(path string) bool {
574-
return loaded.direct[path]
575-
}
576-
577584
// Lookup returns the source directory, import path, and any loading error for
578585
// the package at path as imported from the package in parentDir.
579586
// Lookup requires that one of the Load functions in this package has already

0 commit comments

Comments
 (0)