1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:14:46 -07:00

internal/lsp: report use of disallowed internal packages

An error should be reported if an "internal" package is imported
into code that is outside of the tree rooted at the parent
of the "internal" directory.

Fixes #35937

Change-Id: If5ff3dd79b462087381d575dddb20b78c10f0a83
GitHub-Last-Rev: f5d19960046da7f9701325afc36b5bd0b9663ab6
GitHub-Pull-Request: golang/tools#207
Reviewed-on: https://go-review.googlesource.com/c/tools/+/218977
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Brian Falk 2020-02-19 19:59:26 +00:00 committed by Rebecca Stambler
parent 55b11c713e
commit 77adbdfd2c

View File

@ -54,6 +54,19 @@ func (ph *packageHandle) packageKey() packageKey {
}
}
func (ph *packageHandle) isValidImportFor(parentPkgPath string) bool {
importPath := string(ph.m.pkgPath)
pkgRootIndex := strings.Index(importPath, "/internal/")
if pkgRootIndex != -1 && parentPkgPath != "command-line-arguments" {
if !strings.HasPrefix(parentPkgPath, importPath[0:pkgRootIndex]) {
return false
}
}
return true
}
// packageData contains the data produced by type-checking a package.
type packageData struct {
memoize.NoCopy
@ -368,6 +381,9 @@ func typeCheck(ctx context.Context, fset *token.FileSet, m *metadata, mode sourc
if dep == nil {
return nil, errors.Errorf("no package for import %s", pkgPath)
}
if !dep.isValidImportFor(pkg.PkgPath()) {
return nil, errors.Errorf("invalid use of internal package %s", pkgPath)
}
depPkg, err := dep.check(ctx)
if err != nil {
return nil, err