diff options
author | Bryan C. Mills <bcmills@google.com> | 2018-12-10 15:02:38 -0500 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2018-12-11 21:45:00 +0000 |
commit | cee9dfc39bef7b53ffa4ee584ec7fdec03c95a5a (patch) | |
tree | 90a01e40cba0b4b45cf567a75cc7f8f73c6abfd3 /src/cmd/go/internal/load/pkg.go | |
parent | 353ebe721019ac833646bea829d7840e55f3da30 (diff) | |
download | go-cee9dfc39bef7b53ffa4ee584ec7fdec03c95a5a.tar.gz go-cee9dfc39bef7b53ffa4ee584ec7fdec03c95a5a.zip |
cmd/go: fix 'go test' and 'go fmt' with files outside a module
Use the actual loader result in findModule instead of making
assumptions about nesting in the build list.
As a side-effect, this produces much clearer error messages for
packages that (for one reason or another) failed to load.
Adjust the package and module path outside a module to
"command-line-arguments". That string already appears in the output of
a number of (module-mode and GOPATH-mode) commands for file arguments,
and as far as I can tell operation outside a module is currently the
only case in which the module path of a package is not actually a
prefix of the import path.
Fixes #28011
Fixes #27099
Fixes #28943
Updates #27102
Updates #28459
Updates #27063
Change-Id: I61d5556df7b1b7d1efdaffa892f0e3e95b612d87
Reviewed-on: https://go-review.googlesource.com/c/153459
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/load/pkg.go')
-rw-r--r-- | src/cmd/go/internal/load/pkg.go | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index 616adcc57a..228be07f24 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -997,10 +997,12 @@ func disallowInternal(srcDir string, importer *Package, importerPath string, p * } else { // p is in a module, so make it available based on the importer's import path instead // of the file path (https://golang.org/issue/23970). - if importerPath == "." { + if importer.Internal.CmdlineFiles { // The importer is a list of command-line files. // Pretend that the import path is the import path of the // directory containing them. + // If the directory is outside the main module, this will resolve to ".", + // which is not a prefix of any valid module. importerPath = ModDirImportPath(importer.Dir) } parentOfInternal := p.ImportPath[:i] @@ -1515,11 +1517,13 @@ func (p *Package) load(stk *ImportStack, bp *build.Package, err error) { } if cfg.ModulesEnabled { - if !p.Internal.CmdlineFiles { - p.Module = ModPackageModuleInfo(p.ImportPath) + mainPath := p.ImportPath + if p.Internal.CmdlineFiles { + mainPath = "command-line-arguments" } + p.Module = ModPackageModuleInfo(mainPath) if p.Name == "main" { - p.Internal.BuildInfo = ModPackageBuildInfo(p.ImportPath, p.Deps) + p.Internal.BuildInfo = ModPackageBuildInfo(mainPath, p.Deps) } } } @@ -1986,11 +1990,6 @@ func GoFilesPackage(gofiles []string) *Package { } bp, err := ctxt.ImportDir(dir, 0) - if ModDirImportPath != nil { - // Use the effective import path of the directory - // for deciding visibility during pkg.load. - bp.ImportPath = ModDirImportPath(dir) - } pkg := new(Package) pkg.Internal.Local = true pkg.Internal.CmdlineFiles = true |