Skip to content

Commit db2f680

Browse files
Bryan C. Millsrsc
Bryan C. Mills
authored andcommitted
cmd/go: allow internal imports based on module paths
Updates #23970. Change-Id: I2e69ad15b9d1097bfeef9947f03cfa6834a6a049 Reviewed-on: https://go-review.googlesource.com/125676 Run-TryBot: Bryan C. Mills <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 38dc795 commit db2f680

File tree

5 files changed

+135
-9
lines changed

5 files changed

+135
-9
lines changed

src/cmd/go/internal/load/pkg.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -961,16 +961,29 @@ func disallowInternal(srcDir string, p *Package, stk *ImportStack) *Package {
961961
if i > 0 {
962962
i-- // rewind over slash in ".../internal"
963963
}
964-
parent := p.Dir[:i+len(p.Dir)-len(p.ImportPath)]
965-
if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
966-
return p
967-
}
964+
if p.Module == nil {
965+
parent := p.Dir[:i+len(p.Dir)-len(p.ImportPath)]
968966

969-
// Look for symlinks before reporting error.
970-
srcDir = expandPath(srcDir)
971-
parent = expandPath(parent)
972-
if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
973-
return p
967+
if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
968+
return p
969+
}
970+
971+
// Look for symlinks before reporting error.
972+
srcDir = expandPath(srcDir)
973+
parent = expandPath(parent)
974+
if str.HasFilePathPrefix(filepath.Clean(srcDir), filepath.Clean(parent)) {
975+
return p
976+
}
977+
} else {
978+
// p is in a module, so make it available based on the import path instead
979+
// of the file path (https://golang.org/issue/23970).
980+
parent := p.ImportPath[:i]
981+
// TODO(bcmills): In case of replacements, use the module path declared by
982+
// the replacement module, not the path seen by the user.
983+
importerPath := (*stk)[len(*stk)-2]
984+
if strings.HasPrefix(importerPath, parent) {
985+
return p
986+
}
974987
}
975988

976989
// Internal is present, and srcDir is outside parent's tree. Not allowed.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
written by hand — attempts to use a prohibited internal package
2+
(https://golang.org/s/go14internal)
3+
4+
-- .mod --
5+
module golang.org/notx/useinternal
6+
-- .info --
7+
{"Version":"v0.1.0","Name":"","Short":"","Time":"2018-07-25T17:24:00Z"}
8+
-- go.mod --
9+
module golang.org/notx/useinternal
10+
-- useinternal.go --
11+
package useinternal
12+
13+
import _ "golang.org/x/internal/subtle"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
written by hand — loosely derived from golang.org/x/crypto/internal/subtle,
2+
but splitting the internal package across a module boundary
3+
4+
-- .mod --
5+
module golang.org/x/internal
6+
-- .info --
7+
{"Version":"v0.1.0","Name":"","Short":"","Time":"2018-07-25T17:24:00Z"}
8+
-- go.mod --
9+
module golang.org/x/internal
10+
-- subtle/aliasing.go --
11+
// Copyright 2018 The Go Authors. All rights reserved.
12+
// Use of this source code is governed by a BSD-style
13+
// license that can be found in the LICENSE file.
14+
15+
// +build !appengine
16+
17+
// This is a tiny version of golang.org/x/crypto/internal/subtle.
18+
19+
package subtle
20+
21+
import "unsafe"
22+
23+
func AnyOverlap(x, y []byte) bool {
24+
return len(x) > 0 && len(y) > 0 &&
25+
uintptr(unsafe.Pointer(&x[0])) <= uintptr(unsafe.Pointer(&y[len(y)-1])) &&
26+
uintptr(unsafe.Pointer(&y[0])) <= uintptr(unsafe.Pointer(&x[len(x)-1]))
27+
}
28+
-- subtle/aliasing_appengine.go --
29+
// Copyright 2018 The Go Authors. All rights reserved.
30+
// Use of this source code is governed by a BSD-style
31+
// license that can be found in the LICENSE file.
32+
33+
// +build appengine
34+
35+
package subtle
36+
37+
import "reflect"
38+
39+
func AnyOverlap(x, y []byte) bool {
40+
return len(x) > 0 && len(y) > 0 &&
41+
reflect.ValueOf(&x[0]).Pointer() <= reflect.ValueOf(&y[len(y)-1]).Pointer() &&
42+
reflect.ValueOf(&y[0]).Pointer() <= reflect.ValueOf(&x[len(x)-1]).Pointer()
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
written by hand — uses an internal package from another module
2+
(https://golang.org/s/go14internal)
3+
4+
-- .mod --
5+
module golang.org/x/useinternal
6+
-- .info --
7+
{"Version":"v0.1.0","Name":"","Short":"","Time":"2018-07-25T17:24:00Z"}
8+
-- go.mod --
9+
module golang.org/x/useinternal
10+
-- useinternal.go --
11+
package useinternal
12+
13+
import _ "golang.org/x/internal/subtle"
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
env GO111MODULE=on
2+
3+
# golang.org/x/internal should be importable from other golang.org/x modules.
4+
go mod -init -module golang.org/x/anything
5+
go build .
6+
7+
# ...but that should not leak into other modules.
8+
! go build ./baddep
9+
stderr 'use of internal package'
10+
11+
# Internal packages in the standard library should not leak into modules.
12+
! go build ./fromstd
13+
stderr 'use of internal package'
14+
15+
16+
# Dependencies should be able to use their own internal modules...
17+
rm go.mod
18+
go mod -init -module golang.org/notx
19+
go build ./throughdep
20+
21+
# ... but other modules should not, even if they have transitive dependencies.
22+
! go build .
23+
stderr 'use of internal package'
24+
25+
# And transitive dependencies still should not leak.
26+
! go build ./baddep
27+
stderr 'use of internal package'
28+
29+
30+
-- useinternal.go --
31+
package useinternal
32+
import _ "golang.org/x/internal/subtle"
33+
34+
-- throughdep/useinternal.go --
35+
package throughdep
36+
import _ "golang.org/x/useinternal"
37+
38+
-- baddep/useinternal.go --
39+
package baddep
40+
import _ "golang.org/notx/useinternal"
41+
42+
-- fromstd/useinternal.go --
43+
package fromstd
44+
import _ "internal/testenv"

0 commit comments

Comments
 (0)