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, }, }, }