Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions src/cmd/go/internal/modload/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions src/cmd/go/testdata/script/mod_init_invalid_major.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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