Skip to content

Commit be9e244

Browse files
Bryan C. Millsgopherbot
Bryan C. Mills
authored andcommitted
cmd/go/internal/modload: remove ImportMap and PackageDir
These two functions together duplicated much of the functionality of modload.Lookup. Use that instead in modcmd.vendorPkg, and reduce the modload surface area. Updates #42504 Updates #40775 For #26904 Change-Id: Ib8aaac495d090178dd56971aef9e5aa44ffa818b Reviewed-on: https://go-review.googlesource.com/c/go/+/332571 Reviewed-by: Michael Matloob <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Jay Conrod <[email protected]> Auto-Submit: Bryan Mills <[email protected]>
1 parent de561dc commit be9e244

File tree

2 files changed

+14
-31
lines changed

2 files changed

+14
-31
lines changed

src/cmd/go/internal/modcmd/vendor.go

+14-9
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,25 @@ func moduleLine(m, r module.Version) string {
222222
}
223223

224224
func vendorPkg(vdir, pkg string) {
225-
// TODO(#42504): Instead of calling modload.ImportMap then build.ImportDir,
226-
// just call load.PackagesAndErrors. To do that, we need to add a good way
227-
// to ignore build constraints.
228-
realPath := modload.ImportMap(pkg)
229-
if realPath != pkg && modload.ImportMap(realPath) != "" {
225+
src, realPath, _ := modload.Lookup("", false, pkg)
226+
if src == "" {
227+
base.Errorf("internal error: no pkg for %s\n", pkg)
228+
return
229+
}
230+
if realPath != pkg {
231+
// TODO(#26904): Revisit whether this behavior still makes sense.
232+
// This should actually be impossible today, because the import map is the
233+
// identity function for packages outside of the standard library.
234+
//
235+
// Part of the purpose of the vendor directory is to allow the packages in
236+
// the module to continue to build in GOPATH mode, and GOPATH-mode users
237+
// won't know about replacement aliasing. How important is it to maintain
238+
// compatibility?
230239
fmt.Fprintf(os.Stderr, "warning: %s imported as both %s and %s; making two copies.\n", realPath, realPath, pkg)
231240
}
232241

233242
copiedFiles := make(map[string]bool)
234243
dst := filepath.Join(vdir, pkg)
235-
src := modload.PackageDir(realPath)
236-
if src == "" {
237-
fmt.Fprintf(os.Stderr, "internal error: no pkg for %s -> %s\n", pkg, realPath)
238-
}
239244
copyDir(dst, src, matchPotentialSourceFile, copiedFiles)
240245
if m := modload.PackageModule(realPath); m.Path != "" {
241246
copyMetadata(m.Path, realPath, dst, src, copiedFiles)

src/cmd/go/internal/modload/load.go

-22
Original file line numberDiff line numberDiff line change
@@ -767,28 +767,6 @@ func (mms *MainModuleSet) DirImportPath(ctx context.Context, dir string) (path s
767767
return ".", module.Version{}
768768
}
769769

770-
// ImportMap returns the actual package import path
771-
// for an import path found in source code.
772-
// If the given import path does not appear in the source code
773-
// for the packages that have been loaded, ImportMap returns the empty string.
774-
func ImportMap(path string) string {
775-
pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
776-
if !ok {
777-
return ""
778-
}
779-
return pkg.path
780-
}
781-
782-
// PackageDir returns the directory containing the source code
783-
// for the package named by the import path.
784-
func PackageDir(path string) string {
785-
pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)
786-
if !ok {
787-
return ""
788-
}
789-
return pkg.dir
790-
}
791-
792770
// PackageModule returns the module providing the package named by the import path.
793771
func PackageModule(path string) module.Version {
794772
pkg, ok := loaded.pkgCache.Get(path).(*loadPkg)

0 commit comments

Comments
 (0)