Skip to content

Commit a627fcd

Browse files
committed
[dev.cmdgo] cmd/go: replace Target with MainModules, allowing for multiple targets
This change replaces the Target variable that represents the main module and the pathPrefix and inGorootSrc which provide other information about the main module with a single MainModules value that represents multiple main modules and holds their path prefixes, module roots, and whether they are in GOROOT/src. In cases where the code checks Target or its previously associated variables, the code now checks or iterates over MainModules. In some cases, the code still assumes a single main module by calling MainModules.MustGetSingleMainModule. Some of those cases are correct: for instance, there is always only one main module for mod=vendor. Other cases are accompanied with TODOs and will have to be fixed in future CLs to properly support multiple main modules. This CL (and other cls on top of it) are planned to be checked into a branch to allow for those evaluating the workspaces proposal to try it hands on. For golang#45713 Change-Id: I3b699e1d5cad8c76d62dc567b8460de8c73a87ea Reviewed-on: https://go-review.googlesource.com/c/go/+/334932 Trust: Michael Matloob <[email protected]> Run-TryBot: Michael Matloob <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent ab36149 commit a627fcd

21 files changed

Lines changed: 563 additions & 314 deletions

File tree

src/cmd/go/internal/get/get.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ func downloadPaths(patterns []string) []string {
225225
base.ExitIfErrors()
226226

227227
var pkgs []string
228-
for _, m := range search.ImportPathsQuiet(patterns) {
228+
noModRoots := []string{}
229+
for _, m := range search.ImportPathsQuiet(patterns, noModRoots) {
229230
if len(m.Pkgs) == 0 && strings.Contains(m.Pattern(), "...") {
230231
pkgs = append(pkgs, m.Pattern())
231232
} else {
@@ -315,7 +316,8 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
315316
if wildcardOkay && strings.Contains(arg, "...") {
316317
match := search.NewMatch(arg)
317318
if match.IsLocal() {
318-
match.MatchDirs()
319+
noModRoots := []string{} // We're in gopath mode, so there are no modroots.
320+
match.MatchDirs(noModRoots)
319321
args = match.Dirs
320322
} else {
321323
match.MatchPackages()

src/cmd/go/internal/load/pkg.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,9 +1450,9 @@ func disallowInternal(ctx context.Context, srcDir string, importer *Package, imp
14501450
// The importer is a list of command-line files.
14511451
// Pretend that the import path is the import path of the
14521452
// directory containing them.
1453-
// If the directory is outside the main module, this will resolve to ".",
1453+
// If the directory is outside the main modules, this will resolve to ".",
14541454
// which is not a prefix of any valid module.
1455-
importerPath = modload.DirImportPath(ctx, importer.Dir)
1455+
importerPath, _ = modload.MainModules.DirImportPath(ctx, importer.Dir)
14561456
}
14571457
parentOfInternal := p.ImportPath[:i]
14581458
if str.HasPathPrefix(importerPath, parentOfInternal) {
@@ -2447,7 +2447,8 @@ func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string)
24472447
}
24482448
matches, _ = modload.LoadPackages(ctx, modOpts, patterns...)
24492449
} else {
2450-
matches = search.ImportPaths(patterns)
2450+
noModRoots := []string{}
2451+
matches = search.ImportPaths(patterns, noModRoots)
24512452
}
24522453

24532454
var (

src/cmd/go/internal/modcmd/download.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,18 @@ func runDownload(ctx context.Context, cmd *base.Command, args []string) {
9191
args = []string{"all"}
9292
}
9393
if modload.HasModRoot() {
94-
modload.LoadModFile(ctx) // to fill Target
95-
targetAtUpgrade := modload.Target.Path + "@upgrade"
96-
targetAtPatch := modload.Target.Path + "@patch"
94+
modload.LoadModFile(ctx) // to fill MainModules
95+
96+
if len(modload.MainModules.Versions()) != 1 {
97+
panic(modload.TODOWorkspaces("TODO: multiple main modules not supported in Download"))
98+
}
99+
mainModule := modload.MainModules.Versions()[0]
100+
101+
targetAtUpgrade := mainModule.Path + "@upgrade"
102+
targetAtPatch := mainModule.Path + "@patch"
97103
for _, arg := range args {
98104
switch arg {
99-
case modload.Target.Path, targetAtUpgrade, targetAtPatch:
105+
case mainModule.Path, targetAtUpgrade, targetAtPatch:
100106
os.Stderr.WriteString("go mod download: skipping argument " + arg + " that resolves to the main module\n")
101107
}
102108
}

src/cmd/go/internal/modcmd/vendor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func runVendor(ctx context.Context, cmd *base.Command, args []string) {
8282
modpkgs := make(map[module.Version][]string)
8383
for _, pkg := range pkgs {
8484
m := modload.PackageModule(pkg)
85-
if m.Path == "" || m == modload.Target {
85+
if m.Path == "" || m.Version == "" && modload.MainModules.Contains(m.Path) {
8686
continue
8787
}
8888
modpkgs[m] = append(modpkgs[m], pkg)

src/cmd/go/internal/modget/get.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,11 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
389389

390390
haveExternalExe := false
391391
for _, pkg := range pkgs {
392-
if pkg.Name == "main" && pkg.Module != nil && pkg.Module.Path != modload.Target.Path {
393-
haveExternalExe = true
394-
break
392+
if pkg.Name == "main" && pkg.Module != nil {
393+
if !modload.MainModules.Contains(pkg.Module.Path) {
394+
haveExternalExe = true
395+
break
396+
}
395397
}
396398
}
397399
if haveExternalExe {
@@ -675,7 +677,9 @@ func (r *resolver) queryNone(ctx context.Context, q *query) {
675677

676678
if !q.isWildcard() {
677679
q.pathOnce(q.pattern, func() pathSet {
678-
if modload.HasModRoot() && q.pattern == modload.Target.Path {
680+
hasModRoot := modload.HasModRoot()
681+
if hasModRoot && modload.MainModules.Contains(q.pattern) {
682+
v := module.Version{Path: q.pattern}
679683
// The user has explicitly requested to downgrade their own module to
680684
// version "none". This is not an entirely unreasonable request: it
681685
// could plausibly mean “downgrade away everything that depends on any
@@ -686,7 +690,7 @@ func (r *resolver) queryNone(ctx context.Context, q *query) {
686690
// However, neither of those behaviors would be consistent with the
687691
// plain meaning of the query. To try to reduce confusion, reject the
688692
// query explicitly.
689-
return errSet(&modload.QueryMatchesMainModuleError{Pattern: q.pattern, Query: q.version})
693+
return errSet(&modload.QueryMatchesMainModuleError{MainModule: v, Pattern: q.pattern, Query: q.version})
690694
}
691695

692696
return pathSet{mod: module.Version{Path: q.pattern, Version: "none"}}
@@ -698,8 +702,8 @@ func (r *resolver) queryNone(ctx context.Context, q *query) {
698702
continue
699703
}
700704
q.pathOnce(curM.Path, func() pathSet {
701-
if modload.HasModRoot() && curM == modload.Target {
702-
return errSet(&modload.QueryMatchesMainModuleError{Pattern: q.pattern, Query: q.version})
705+
if modload.HasModRoot() && curM.Version == "" && modload.MainModules.Contains(curM.Path) {
706+
return errSet(&modload.QueryMatchesMainModuleError{MainModule: curM, Pattern: q.pattern, Query: q.version})
703707
}
704708
return pathSet{mod: module.Version{Path: curM.Path, Version: "none"}}
705709
})
@@ -718,12 +722,12 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
718722

719723
// Absolute paths like C:\foo and relative paths like ../foo... are
720724
// restricted to matching packages in the main module.
721-
pkgPattern := modload.DirImportPath(ctx, q.pattern)
725+
pkgPattern, mainModule := modload.MainModules.DirImportPath(ctx, q.pattern)
722726
if pkgPattern == "." {
723727
return errSet(fmt.Errorf("%s%s is not within module rooted at %s", q.pattern, absDetail, modload.ModRoot()))
724728
}
725729

726-
match := modload.MatchInModule(ctx, pkgPattern, modload.Target, imports.AnyTags())
730+
match := modload.MatchInModule(ctx, pkgPattern, mainModule, imports.AnyTags())
727731
if len(match.Errs) > 0 {
728732
return pathSet{err: match.Errs[0]}
729733
}
@@ -739,7 +743,7 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
739743
return pathSet{}
740744
}
741745

742-
return pathSet{pkgMods: []module.Version{modload.Target}}
746+
return pathSet{pkgMods: []module.Version{mainModule}}
743747
})
744748
}
745749
}
@@ -789,11 +793,12 @@ func (r *resolver) queryWildcard(ctx context.Context, q *query) {
789793
return pathSet{}
790794
}
791795

792-
if curM.Path == modload.Target.Path && !versionOkForMainModule(q.version) {
796+
if modload.MainModules.Contains(curM.Path) && !versionOkForMainModule(q.version) {
793797
if q.matchesPath(curM.Path) {
794798
return errSet(&modload.QueryMatchesMainModuleError{
795-
Pattern: q.pattern,
796-
Query: q.version,
799+
MainModule: curM,
800+
Pattern: q.pattern,
801+
Query: q.version,
797802
})
798803
}
799804

@@ -1159,8 +1164,8 @@ func (r *resolver) loadPackages(ctx context.Context, patterns []string, findPack
11591164
}
11601165

11611166
opts.AllowPackage = func(ctx context.Context, path string, m module.Version) error {
1162-
if m.Path == "" || m == modload.Target {
1163-
// Packages in the standard library and main module are already at their
1167+
if m.Path == "" || m.Version == "" && modload.MainModules.Contains(m.Path) {
1168+
// Packages in the standard library and main modules are already at their
11641169
// latest (and only) available versions.
11651170
return nil
11661171
}
@@ -1370,11 +1375,11 @@ func (r *resolver) disambiguate(cs pathSet) (filtered pathSet, isPackage bool, m
13701375
continue
13711376
}
13721377

1373-
if m.Path == modload.Target.Path {
1374-
if m.Version == modload.Target.Version {
1378+
if modload.MainModules.Contains(m.Path) {
1379+
if m.Version == "" {
13751380
return pathSet{}, true, m, true
13761381
}
1377-
// The main module can only be set to its own version.
1382+
// A main module can only be set to its own version.
13781383
continue
13791384
}
13801385

@@ -1744,10 +1749,11 @@ func (r *resolver) resolve(q *query, m module.Version) {
17441749
panic("internal error: resolving a module.Version with an empty path")
17451750
}
17461751

1747-
if m.Path == modload.Target.Path && m.Version != modload.Target.Version {
1752+
if modload.MainModules.Contains(m.Path) && m.Version != "" {
17481753
reportError(q, &modload.QueryMatchesMainModuleError{
1749-
Pattern: q.pattern,
1750-
Query: q.version,
1754+
MainModule: module.Version{Path: m.Path},
1755+
Pattern: q.pattern,
1756+
Query: q.version,
17511757
})
17521758
return
17531759
}
@@ -1775,7 +1781,7 @@ func (r *resolver) updateBuildList(ctx context.Context, additions []module.Versi
17751781

17761782
resolved := make([]module.Version, 0, len(r.resolvedVersion))
17771783
for mPath, rv := range r.resolvedVersion {
1778-
if mPath != modload.Target.Path {
1784+
if !modload.MainModules.Contains(mPath) {
17791785
resolved = append(resolved, module.Version{Path: mPath, Version: rv.version})
17801786
}
17811787
}

src/cmd/go/internal/modget/query.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,9 @@ func (q *query) validate() error {
192192
// TODO(bcmills): "all@none" seems like a totally reasonable way to
193193
// request that we remove all module requirements, leaving only the main
194194
// module and standard library. Perhaps we should implement that someday.
195-
return &modload.QueryMatchesMainModuleError{
196-
Pattern: q.pattern,
197-
Query: q.version,
195+
return &modload.QueryUpgradesAllError{
196+
MainModules: modload.MainModules.Versions(),
197+
Query: q.version,
198198
}
199199
}
200200
}

src/cmd/go/internal/modload/build.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -212,20 +212,21 @@ func addDeprecation(ctx context.Context, m *modinfo.ModulePublic) {
212212
// in rs (which may be nil to indicate that m was not loaded from a requirement
213213
// graph).
214214
func moduleInfo(ctx context.Context, rs *Requirements, m module.Version, mode ListMode) *modinfo.ModulePublic {
215-
if m == Target {
215+
if m.Version == "" && MainModules.Contains(m.Path) {
216216
info := &modinfo.ModulePublic{
217217
Path: m.Path,
218218
Version: m.Version,
219219
Main: true,
220220
}
221-
if v, ok := rawGoVersion.Load(Target); ok {
221+
_ = TODOWorkspaces("handle rawGoVersion here")
222+
if v, ok := rawGoVersion.Load(m); ok {
222223
info.GoVersion = v.(string)
223224
} else {
224225
panic("internal error: GoVersion not set for main module")
225226
}
226-
if HasModRoot() {
227-
info.Dir = ModRoot()
228-
info.GoMod = ModFilePath()
227+
if modRoot := MainModules.ModRoot(m); modRoot != "" {
228+
info.Dir = modRoot
229+
info.GoMod = modFilePath(modRoot)
229230
}
230231
return info
231232
}
@@ -397,7 +398,8 @@ func mustFindModule(ld *loader, target, path string) module.Version {
397398
}
398399

399400
if path == "command-line-arguments" {
400-
return Target
401+
_ = TODOWorkspaces("support multiple main modules; search by modroot")
402+
return MainModules.mustGetSingleMainModule()
401403
}
402404

403405
base.Fatalf("build %v: cannot find module for path %v", target, path)
@@ -406,13 +408,14 @@ func mustFindModule(ld *loader, target, path string) module.Version {
406408

407409
// findModule searches for the module that contains the package at path.
408410
// If the package was loaded, its containing module and true are returned.
409-
// Otherwise, module.Version{} and false are returend.
411+
// Otherwise, module.Version{} and false are returned.
410412
func findModule(ld *loader, path string) (module.Version, bool) {
411413
if pkg, ok := ld.pkgCache.Get(path).(*loadPkg); ok {
412414
return pkg.mod, pkg.mod != module.Version{}
413415
}
414416
if path == "command-line-arguments" {
415-
return Target, true
417+
_ = TODOWorkspaces("support multiple main modules; search by modroot")
418+
return MainModules.mustGetSingleMainModule(), true
416419
}
417420
return module.Version{}, false
418421
}

0 commit comments

Comments
 (0)