Skip to content

Commit 181d09f

Browse files
committed
Update package's list of imports after we filter out sources.
This ensures that callers only see imports that actually remain in the sources after we excluded irrelevant parts of the upstream package sources.
1 parent 6bdf1b4 commit 181d09f

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

build/build.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -564,23 +564,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
564564
}
565565

566566
for _, importedPkgPath := range pkg.Imports {
567-
// Ignore all imports that aren't mentioned in import specs of pkg.
568-
// For example, this ignores imports such as runtime/internal/sys and runtime/internal/atomic.
569-
ignored := true
570-
for _, pos := range pkg.ImportPos[importedPkgPath] {
571-
importFile := filepath.Base(pos.Filename)
572-
for _, file := range pkg.GoFiles {
573-
if importFile == file {
574-
ignored = false
575-
break
576-
}
577-
}
578-
if !ignored {
579-
break
580-
}
581-
}
582-
583-
if importedPkgPath == "unsafe" || ignored {
567+
if importedPkgPath == "unsafe" {
584568
continue
585569
}
586570
importedPkg, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)

build/context.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package build
33
import (
44
"fmt"
55
"go/build"
6+
"go/token"
67
"net/http"
78
"os"
89
"os/exec"
@@ -240,9 +241,14 @@ func (sc simpleCtx) applyPostloadTweaks(pkg *build.Package) *build.Package {
240241
case "syscall/js":
241242
// Reuse upstream tests to ensure conformance, but completely replace
242243
// implementation.
243-
pkg.XTestGoFiles = append(pkg.TestGoFiles, "js_test.go")
244+
pkg.GoFiles = []string{}
245+
pkg.XTestGoFiles = append(pkg.XTestGoFiles, "js_test.go")
244246
}
245247

248+
pkg.Imports, pkg.ImportPos = updateImports(pkg.GoFiles, pkg.ImportPos)
249+
pkg.TestImports, pkg.TestImportPos = updateImports(pkg.TestGoFiles, pkg.TestImportPos)
250+
pkg.XTestImports, pkg.XTestImportPos = updateImports(pkg.XTestGoFiles, pkg.XTestImportPos)
251+
246252
return pkg
247253
}
248254

@@ -389,3 +395,31 @@ func IsPkgNotFound(err error) bool {
389395
(strings.Contains(err.Error(), "cannot find package") || // Modules off.
390396
strings.Contains(err.Error(), "is not in GOROOT")) // Modules on.
391397
}
398+
399+
// updateImports package's list of import paths to only those present in sources
400+
// after post-load tweaks.
401+
func updateImports(sources []string, importPos map[string][]token.Position) (newImports []string, newImportPos map[string][]token.Position) {
402+
if importPos == nil {
403+
// Short-circuit for tests when no imports are loaded.
404+
return nil, nil
405+
}
406+
sourceSet := map[string]bool{}
407+
for _, source := range sources {
408+
sourceSet[source] = true
409+
}
410+
411+
newImportPos = map[string][]token.Position{}
412+
for importPath, positions := range importPos {
413+
for _, pos := range positions {
414+
if sourceSet[filepath.Base(pos.Filename)] {
415+
newImportPos[importPath] = append(newImportPos[importPath], pos)
416+
}
417+
}
418+
}
419+
420+
for importPath := range newImportPos {
421+
newImports = append(newImports, importPath)
422+
}
423+
sort.Strings(newImports)
424+
return newImports, newImportPos
425+
}

0 commit comments

Comments
 (0)