aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLE Manh Cuong <cuong.manhle.vn@gmail.com>2019-06-17 11:07:39 +0700
committerBryan C. Mills <bcmills@google.com>2019-06-25 16:45:02 +0000
commit599aa6dd6d2d3d35a596302d8c109d0c15b6e8be (patch)
tree176b80e0b1b910493a70b65c1f942b745c35fa9b
parent3f83c83bd681861802ac07ec285cef28fd5766cb (diff)
downloadgo-599aa6dd6d2d3d35a596302d8c109d0c15b6e8be.tar.gz
go-599aa6dd6d2d3d35a596302d8c109d0c15b6e8be.zip
cmd/go: validate path in mod init path
When mod init with given module path, validate that module path is a valid import path. Note that module.CheckImportPath is used, because module.CheckPath verifies that module path is something that "go get" can fetch, which is strictly stronger condition than "a valid module path". Updates #28389 Fixes #32644 Change-Id: Ia60f218dd7d79186f87be723c28a96d6cb63017e Reviewed-on: https://go-review.googlesource.com/c/go/+/182560 Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
-rw-r--r--src/cmd/go/internal/modload/init.go3
-rw-r--r--src/cmd/go/testdata/script/mod_init_path.txt20
2 files changed, 23 insertions, 0 deletions
diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index 75ea131273..807ce8d5dc 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -518,6 +518,9 @@ func findAltConfig(dir string) (root, name string) {
func findModulePath(dir string) (string, error) {
if CmdModModule != "" {
// Running go mod init x/y/z; return x/y/z.
+ if err := module.CheckImportPath(CmdModModule); err != nil {
+ return "", err
+ }
return CmdModModule, nil
}
diff --git a/src/cmd/go/testdata/script/mod_init_path.txt b/src/cmd/go/testdata/script/mod_init_path.txt
new file mode 100644
index 0000000000..637c29f4bc
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_init_path.txt
@@ -0,0 +1,20 @@
+env GO111MODULE=on
+
+! go mod init .
+stderr 'malformed import path'
+
+cd x
+go mod init example.com/x
+
+cd ../y
+go mod init m
+
+-- x/main.go --
+package main
+
+func main() {}
+
+-- y/main.go --
+package main
+
+func main() {}