]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: validate path in mod init path
authorLE Manh Cuong <cuong.manhle.vn@gmail.com>
Mon, 17 Jun 2019 04:07:39 +0000 (11:07 +0700)
committerBryan C. Mills <bcmills@google.com>
Tue, 25 Jun 2019 16:45:02 +0000 (16:45 +0000)
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>
src/cmd/go/internal/modload/init.go
src/cmd/go/testdata/script/mod_init_path.txt [new file with mode: 0644]

index 75ea1312735fb40fcc2affc92bdeedf50485d75c..807ce8d5dc5fe290d6ee66f2a17d0e03ca0a3488 100644 (file)
@@ -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 (file)
index 0000000..637c29f
--- /dev/null
@@ -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() {}