diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go index c4ff9656694817..3de3f1c7145a8f 100644 --- a/src/cmd/go/internal/modload/init.go +++ b/src/cmd/go/internal/modload/init.go @@ -1176,26 +1176,8 @@ func CreateModFile(loaderstate *State, ctx context.Context, modPath string) { if err != nil { base.Fatal(err) } - } else if err := module.CheckImportPath(modPath); err != nil { - if pathErr, ok := err.(*module.InvalidPathError); ok { - pathErr.Kind = "module" - // Same as build.IsLocalPath() - if pathErr.Path == "." || pathErr.Path == ".." || - strings.HasPrefix(pathErr.Path, "./") || strings.HasPrefix(pathErr.Path, "../") { - pathErr.Err = errors.New("is a local import path") - } - } - 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)) - base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg) - } - invalidMajorVersionMsg := fmt.Errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later:\n\tgo mod init %s", suggestModulePath(modPath)) - base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg) } + checkModulePath(modPath) fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath) modFile := new(modfile.File) @@ -1237,6 +1219,31 @@ func CreateModFile(loaderstate *State, ctx context.Context, modPath string) { } } +func checkModulePath(modPath string) { + if err := module.CheckImportPath(modPath); err != nil { + if pathErr, ok := err.(*module.InvalidPathError); ok { + pathErr.Kind = "module" + // Same as build.IsLocalPath() + if pathErr.Path == "." || pathErr.Path == ".." || + strings.HasPrefix(pathErr.Path, "./") || strings.HasPrefix(pathErr.Path, "../") { + pathErr.Err = errors.New("is a local import path") + } + } + base.Fatal(err) + } + if err := CheckReservedModulePath(modPath); err != nil { + base.Fatalf(`go: invalid module path %q: `, modPath) + } + 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)) + base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg) + } + invalidMajorVersionMsg := fmt.Errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later:\n\tgo mod init %s", suggestModulePath(modPath)) + base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg) + } +} + // fixVersion returns a modfile.VersionFixer implemented using the Query function. // // It resolves commit hashes and branch names to versions, diff --git a/src/cmd/go/testdata/script/mod_init_invalid_major.txt b/src/cmd/go/testdata/script/mod_init_invalid_major.txt index ae93e70d6307ff..69dfa292510379 100644 --- a/src/cmd/go/testdata/script/mod_init_invalid_major.txt +++ b/src/cmd/go/testdata/script/mod_init_invalid_major.txt @@ -80,3 +80,11 @@ stderr '(?s)^go: malformed module path "foo bar": invalid char '' ''$' ! go mod init 'foo bar baz' stderr '(?s)^go: malformed module path "foo bar baz": invalid char '' ''$' + +# go mod init should not infer an invalid /v1 suffix from GOPATH. +cd $WORK/gopath/src/example.com/user/repo/v1 +! go mod init +stderr '(?s)^go: invalid module path "example.com/user/repo/v1": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v2$' + +-- example.com/user/repo/v1/empty.go -- +package main