}
if *editModule != "" {
- if err := module.CheckImportPath(*editModule); err != nil {
+ err := module.CheckImportPath(*editModule)
+ if err == nil {
+ err = modload.CheckReservedModulePath(*editModule)
+ }
+ if err != nil {
base.Fatalf("go: invalid -module: %v", err)
}
}
base.ShortPath(filepath.Dir(gomod)), goVers, verb, gover.FromGoWork(wf))
}
+// CheckReservedModulePath checks whether the module path is a reserved module path
+// that can't be used for a user's module.
+func CheckReservedModulePath(path string) error {
+ if gover.IsToolchain(path) {
+ return errors.New("module path is reserved")
+ }
+
+ return nil
+}
+
// CreateModFile initializes a new module by creating a go.mod file.
//
// If modPath is empty, CreateModFile will attempt to infer the path from the
}
}
base.Fatal(err)
+ } else if err := CheckReservedModulePath(modPath); err != nil {
+ base.Fatalf(`go: invalid module path %q: `, modPath)
} else if _, _, ok := module.SplitPathVersion(modPath); !ok {
if strings.HasPrefix(modPath, "gopkg.in/") {
invalidMajorVersionMsg := fmt.Errorf("module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN:\n\tgo mod init %s", suggestGopkgIn(modPath))
if f.Module == nil {
// No module declaration. Must add module path.
return nil, nil, fmt.Errorf("error reading %s: missing module declaration. To specify the module path:\n\tgo mod edit -module=example.com/mod", base.ShortPath(gomod))
+ } else if err := CheckReservedModulePath(f.Module.Mod.Path); err != nil {
+ return nil, nil, fmt.Errorf("error reading %s: invalid module path: %q", base.ShortPath(gomod), f.Module.Mod.Path)
}
return data, f, err
--- /dev/null
+# Don't allow the creation of modules with special "go" or "toolchain" paths.
+! go mod init go
+! stderr 'panic'
+stderr 'invalid module path'
+
+! go mod init toolchain
+! stderr 'panic'
+stderr 'invalid module path'
+
+# A module that contains the path element "go" is okay.
+go mod init example.com/go
+stderr 'creating new go.mod'
+
+# go mod edit won't allow a reserved module path either
+! go mod edit -module=go
+stderr 'invalid -module'
+
+# The go command should check for work modules for bad
+# names to return a proper error and avoid a panic.
+cp badmod.txt go.mod
+! go list
+! stderr panic
+stderr 'invalid module path'
+
+-- badmod.txt --
+module go