Skip to content

Commit 97bdac0

Browse files
author
Jay Conrod
committed
cmd: upgrade golang.org/x/mod to relax import path check
This incorporates CL 297089, which allows leading dots in import path elements but not module path elements. Also added a test. Fixes #43985 Updates #34992 Change-Id: I2d5faabd8f7b23a7943d3f3ccb6707ab5dc2ce3c Reviewed-on: https://go-review.googlesource.com/c/go/+/297530 Trust: Jay Conrod <[email protected]> Run-TryBot: Jay Conrod <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]>
1 parent f6a74c6 commit 97bdac0

File tree

6 files changed

+82
-20
lines changed

6 files changed

+82
-20
lines changed

src/cmd/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2
77
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff
88
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
9-
golang.org/x/mod v0.4.2-0.20210225160341-66bf157bf5bc
9+
golang.org/x/mod v0.4.2-0.20210301144719-c8bb1bd8a2aa
1010
golang.org/x/sys v0.0.0-20210218145245-beda7e5e158e // indirect
1111
golang.org/x/tools v0.1.1-0.20210220032852-2363391a5b2f
1212
)

src/cmd/go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
1414
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
1515
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
1616
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
17-
golang.org/x/mod v0.4.2-0.20210225160341-66bf157bf5bc h1:xQukuh0OD2SNSUK1CCBFATgHYx5ye75S/bAWEU/PT0E=
18-
golang.org/x/mod v0.4.2-0.20210225160341-66bf157bf5bc/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
17+
golang.org/x/mod v0.4.2-0.20210301144719-c8bb1bd8a2aa h1:Ci2bbuyE4ah9djFByg+fdNQcqc8DVSdcXbrWy6MBoEs=
18+
golang.org/x/mod v0.4.2-0.20210301144719-c8bb1bd8a2aa/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
1919
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
2020
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
2121
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=

src/cmd/go/internal/get/get.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func downloadPackage(p *load.Package) error {
431431
}
432432
importPrefix = importPrefix[:slash]
433433
}
434-
if err := module.CheckImportPath(importPrefix); err != nil {
434+
if err := checkImportPath(importPrefix); err != nil {
435435
return fmt.Errorf("%s: invalid import path: %v", p.ImportPath, err)
436436
}
437437
security := web.SecureOnly
@@ -591,3 +591,31 @@ func selectTag(goVersion string, tags []string) (match string) {
591591
}
592592
return ""
593593
}
594+
595+
// checkImportPath is like module.CheckImportPath, but it forbids leading dots
596+
// in path elements. This can lead to 'go get' creating .git and other VCS
597+
// directories in places we might run VCS tools later.
598+
func checkImportPath(path string) error {
599+
if err := module.CheckImportPath(path); err != nil {
600+
return err
601+
}
602+
checkElem := func(elem string) error {
603+
if elem[0] == '.' {
604+
return fmt.Errorf("malformed import path %q: leading dot in path element", path)
605+
}
606+
return nil
607+
}
608+
elemStart := 0
609+
for i, r := range path {
610+
if r == '/' {
611+
if err := checkElem(path[elemStart:]); err != nil {
612+
return err
613+
}
614+
elemStart = i + 1
615+
}
616+
}
617+
if err := checkElem(path[elemStart:]); err != nil {
618+
return err
619+
}
620+
return nil
621+
}

src/cmd/go/testdata/script/mod_invalid_path.txt

+24
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@ cd $WORK/gopath/src/badname
2323
! go list .
2424
stderr 'invalid module path'
2525

26+
# Test that an import path containing an element with a leading dot is valid,
27+
# but such a module path is not.
28+
# Verifies #43985.
29+
cd $WORK/gopath/src/dotname
30+
go list ./.dot
31+
stdout '^example.com/dotname/.dot$'
32+
go list ./use
33+
stdout '^example.com/dotname/use$'
34+
! go list -m example.com/dotname/.dot@latest
35+
stderr '^go list -m: example.com/dotname/.dot@latest: malformed module path "example.com/dotname/.dot": leading dot in path element$'
36+
go get -d example.com/dotname/.dot
37+
go get -d example.com/dotname/use
38+
go mod tidy
39+
2640
-- mod/go.mod --
2741

2842
-- mod/foo.go --
@@ -38,3 +52,13 @@ module .\.
3852
-- badname/foo.go --
3953
package badname
4054

55+
-- dotname/go.mod --
56+
module example.com/dotname
57+
58+
go 1.16
59+
-- dotname/.dot/dot.go --
60+
package dot
61+
-- dotname/use/use.go --
62+
package use
63+
64+
import _ "example.com/dotname/.dot"

src/cmd/vendor/golang.org/x/mod/module/module.go

+25-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/vendor/modules.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ golang.org/x/arch/x86/x86asm
2828
golang.org/x/crypto/ed25519
2929
golang.org/x/crypto/ed25519/internal/edwards25519
3030
golang.org/x/crypto/ssh/terminal
31-
# golang.org/x/mod v0.4.2-0.20210225160341-66bf157bf5bc
31+
# golang.org/x/mod v0.4.2-0.20210301144719-c8bb1bd8a2aa
3232
## explicit
3333
golang.org/x/mod/internal/lazyregexp
3434
golang.org/x/mod/modfile

0 commit comments

Comments
 (0)