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:
parent
55b11c713e
commit
77adbdfd2c
16
internal/lsp/cache/check.go
vendored
16
internal/lsp/cache/check.go
vendored
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user