From 1e586a538ebbd26e54f3e5420dbc23914a69e16f Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Mon, 30 Dec 2019 13:30:09 -0500 Subject: [PATCH] internal/imports: clean up dead code Now that we're storing module information per-directory, we don't need to pre-compute the need for a replace statement. And we never have the package name in the place it used to be set. Change-Id: I3b0845dc49f52f8c449840410dbb786fe903d29d Reviewed-on: https://go-review.googlesource.com/c/tools/+/212861 Run-TryBot: Heschi Kreinick TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/imports/mod.go | 14 +++----------- internal/imports/mod_cache.go | 4 ---- internal/imports/mod_cache_test.go | 2 -- 3 files changed, 3 insertions(+), 17 deletions(-) diff --git a/internal/imports/mod.go b/internal/imports/mod.go index ba23215885..a7bcdfe000 100644 --- a/internal/imports/mod.go +++ b/internal/imports/mod.go @@ -488,14 +488,15 @@ func (r *ModuleResolver) canonicalize(info directoryPackageInfo) (*pkg, error) { dirInMod := info.dir[len(mod.Dir)+len("/"):] importPath = path.Join(mod.Path, filepath.ToSlash(dirInMod)) } - } else if info.needsReplace { + } else if !strings.HasPrefix(importPath, info.moduleName) { + // The module's name doesn't match the package's import path. It + // probably needs a replace directive we don't have. return nil, fmt.Errorf("package in %q is not valid without a replace statement", info.dir) } res := &pkg{ importPathShort: importPath, dir: info.dir, - packageName: info.packageName, // may not be populated if the caller didn't ask for it relevance: relevance, } // We may have discovered a package that has a different version @@ -559,7 +560,6 @@ func (r *ModuleResolver) scanDirForPackage(root gopathwalk.Root, dir string) dir dir: dir, rootType: root.Type, nonCanonicalImportPath: importPath, - needsReplace: false, moduleDir: modDir, moduleName: modName, } @@ -567,14 +567,6 @@ func (r *ModuleResolver) scanDirForPackage(root gopathwalk.Root, dir string) dir // stdlib packages are always in scope, despite the confusing go.mod return result } - // Check that this package is not obviously impossible to import. - if !strings.HasPrefix(importPath, modName) { - // The module's declared path does not match - // its expected path. It probably needs a - // replace directive we don't have. - result.needsReplace = true - } - return result } diff --git a/internal/imports/mod_cache.go b/internal/imports/mod_cache.go index 8b04cd3258..e44926ee5d 100644 --- a/internal/imports/mod_cache.go +++ b/internal/imports/mod_cache.go @@ -49,10 +49,6 @@ type directoryPackageInfo struct { // nonCanonicalImportPath is the package's expected import path. It may // not actually be importable at that path. nonCanonicalImportPath string - // needsReplace is true if the nonCanonicalImportPath does not match the - // module's declared path, making it impossible to import without a - // replace directive. - needsReplace bool // Module-related information. moduleDir string // The directory that is the module root of this dir. diff --git a/internal/imports/mod_cache_test.go b/internal/imports/mod_cache_test.go index 5bf96c1cf6..e7d3c73960 100644 --- a/internal/imports/mod_cache_test.go +++ b/internal/imports/mod_cache_test.go @@ -68,7 +68,6 @@ func TestModCacheInfo(t *testing.T) { status: directoryScanned, dir: "mypackage", nonCanonicalImportPath: "example.com/mypackage", - needsReplace: false, }, }, { @@ -83,7 +82,6 @@ func TestModCacheInfo(t *testing.T) { info: directoryPackageInfo{ dir: "mypackage/other", nonCanonicalImportPath: "example.com/mypackage/other", - needsReplace: false, }, }, }