@@ -3,6 +3,7 @@ package build
3
3
import (
4
4
"fmt"
5
5
"go/build"
6
+ "go/token"
6
7
"net/http"
7
8
"os"
8
9
"os/exec"
@@ -240,9 +241,14 @@ func (sc simpleCtx) applyPostloadTweaks(pkg *build.Package) *build.Package {
240
241
case "syscall/js" :
241
242
// Reuse upstream tests to ensure conformance, but completely replace
242
243
// implementation.
243
- pkg .XTestGoFiles = append (pkg .TestGoFiles , "js_test.go" )
244
+ pkg .GoFiles = []string {}
245
+ pkg .XTestGoFiles = append (pkg .XTestGoFiles , "js_test.go" )
244
246
}
245
247
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
+
246
252
return pkg
247
253
}
248
254
@@ -389,3 +395,31 @@ func IsPkgNotFound(err error) bool {
389
395
(strings .Contains (err .Error (), "cannot find package" ) || // Modules off.
390
396
strings .Contains (err .Error (), "is not in GOROOT" )) // Modules on.
391
397
}
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