Skip to content

Commit cdbd4d4

Browse files
author
Bryan C. Mills
committed
cmd/go: enable module mode without a main module when GO111MODULE=on
This is as minimal a change as I could comfortably make to enable 'go get' outside of a module for 1.12. In general, commands invoked in module mode while outside of a module operate as though they are in a module with an initially-empty go.mod file. ('go env GOMOD' reports os.DevNull.) Commands that operate on the current directory (such as 'go list' and 'go get -u' without arguments) fail: without a module definition, we don't know the package path. Likewise, commands whose sole purpose is to write files within the main module (such as 'go mod edit' and 'go mod vendor') fail, since we don't know where to write their output. Since the go.sum file for the main module is authoritative, we do not check go.sum files when operating outside of a module. I plan to revisit that when the tree opens for 1.13. We may also want to revisit the behavior of 'go list': it would be useful to be able to query individual packages (and dependencies of those packages) within versioned modules, but today we only allow versioned paths in conjunction with the '-m' flag. Fixes #24250 RELNOTE=yes Change-Id: I028c323ddea27693a92ad0aa4a6a55d5e3f43f2c Reviewed-on: https://go-review.googlesource.com/c/148517 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent a2b4ac6 commit cdbd4d4

22 files changed

+511
-106
lines changed

src/cmd/go/internal/clean/clean.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,7 @@ func init() {
105105
}
106106

107107
func runClean(cmd *base.Command, args []string) {
108-
if len(args) == 0 && modload.Failed() {
109-
// Don't try to clean current directory,
110-
// which will cause modload to base.Fatalf.
111-
} else {
108+
if len(args) > 0 || !modload.Enabled() || modload.HasModRoot() {
112109
for _, pkg := range load.PackagesAndErrors(args) {
113110
clean(pkg)
114111
}

src/cmd/go/internal/envcmd/env.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ func findEnv(env []cfg.EnvVar, name string) string {
115115
// ExtraEnvVars returns environment variables that should not leak into child processes.
116116
func ExtraEnvVars() []cfg.EnvVar {
117117
gomod := ""
118-
if modload.Init(); modload.ModRoot != "" {
119-
gomod = filepath.Join(modload.ModRoot, "go.mod")
118+
if modload.HasModRoot() {
119+
gomod = filepath.Join(modload.ModRoot(), "go.mod")
120+
} else if modload.Enabled() {
121+
gomod = os.DevNull
120122
}
121123
return []cfg.EnvVar{
122124
{Name: "GOMOD", Value: gomod},

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,9 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) {
15151515
}
15161516

15171517
if cfg.ModulesEnabled {
1518-
p.Module = ModPackageModuleInfo(p.ImportPath)
1518+
if !p.Internal.CmdlineFiles {
1519+
p.Module = ModPackageModuleInfo(p.ImportPath)
1520+
}
15191521
if p.Name == "main" {
15201522
p.Internal.BuildInfo = ModPackageBuildInfo(p.ImportPath, p.Deps)
15211523
}

src/cmd/go/internal/modcmd/edit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ func runEdit(cmd *base.Command, args []string) {
157157
if len(args) == 1 {
158158
gomod = args[0]
159159
} else {
160-
modload.MustInit()
161-
gomod = filepath.Join(modload.ModRoot, "go.mod")
160+
gomod = filepath.Join(modload.ModRoot(), "go.mod")
162161
}
163162

164163
if *editModule != "" {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func runVendor(cmd *base.Command, args []string) {
4343
}
4444
pkgs := modload.LoadVendor()
4545

46-
vdir := filepath.Join(modload.ModRoot, "vendor")
46+
vdir := filepath.Join(modload.ModRoot(), "vendor")
4747
if err := os.RemoveAll(vdir); err != nil {
4848
base.Fatalf("go mod vendor: %v", err)
4949
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ func runGet(cmd *base.Command, args []string) {
281281
base.Errorf("go get %s: %v", arg, err)
282282
continue
283283
}
284-
if !str.HasFilePathPrefix(abs, modload.ModRoot) {
285-
base.Errorf("go get %s: directory %s is outside module root %s", arg, abs, modload.ModRoot)
284+
if !str.HasFilePathPrefix(abs, modload.ModRoot()) {
285+
base.Errorf("go get %s: directory %s is outside module root %s", arg, abs, modload.ModRoot())
286286
continue
287287
}
288288
// TODO: Check if abs is inside a nested module.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
9898
Path: m.Path,
9999
Version: m.Version,
100100
Main: true,
101-
Dir: ModRoot,
102-
GoMod: filepath.Join(ModRoot, "go.mod"),
101+
Dir: ModRoot(),
102+
GoMod: filepath.Join(ModRoot(), "go.mod"),
103103
}
104104
if modFile.Go != nil {
105105
info.GoVersion = modFile.Go.Version
@@ -117,7 +117,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
117117
}
118118

119119
if cfg.BuildMod == "vendor" {
120-
info.Dir = filepath.Join(ModRoot, "vendor", m.Path)
120+
info.Dir = filepath.Join(ModRoot(), "vendor", m.Path)
121121
return info
122122
}
123123

@@ -171,7 +171,7 @@ func moduleInfo(m module.Version, fromBuildList bool) *modinfo.ModulePublic {
171171
if filepath.IsAbs(r.Path) {
172172
info.Replace.Dir = r.Path
173173
} else {
174-
info.Replace.Dir = filepath.Join(ModRoot, r.Path)
174+
info.Replace.Dir = filepath.Join(ModRoot(), r.Path)
175175
}
176176
}
177177
complete(info.Replace)

src/cmd/go/internal/modload/import.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ func Import(path string) (m module.Version, dir string, err error) {
6767
// -mod=vendor is special.
6868
// Everything must be in the main module or the main module's vendor directory.
6969
if cfg.BuildMod == "vendor" {
70-
mainDir, mainOK := dirInModule(path, Target.Path, ModRoot, true)
71-
vendorDir, vendorOK := dirInModule(path, "", filepath.Join(ModRoot, "vendor"), false)
70+
mainDir, mainOK := dirInModule(path, Target.Path, ModRoot(), true)
71+
vendorDir, vendorOK := dirInModule(path, "", filepath.Join(ModRoot(), "vendor"), false)
7272
if mainOK && vendorOK {
7373
return module.Version{}, "", fmt.Errorf("ambiguous import: found %s in multiple directories:\n\t%s\n\t%s", path, mainDir, vendorDir)
7474
}

0 commit comments

Comments
 (0)