@@ -8,6 +8,7 @@ package build
8
8
import (
9
9
"bytes"
10
10
"crypto/sha256"
11
+ "errors"
11
12
"fmt"
12
13
"go/ast"
13
14
"go/build"
@@ -240,6 +241,22 @@ func ImportDir(dir string, mode build.ImportMode, installSuffix string, buildTag
240
241
return pkg , nil
241
242
}
242
243
244
+ // newNativesContext returns a new nativesContext, with special considerations
245
+ // for the syscall package.
246
+ func newNativesContext (importPath string ) * simpleCtx {
247
+ nativesContext := embeddedCtx (& withPrefix {fs : natives .FS , prefix : DefaultGOROOT }, "" , nil )
248
+
249
+ if importPath == "syscall" {
250
+ // Special handling for the syscall package, which uses OS native
251
+ // GOOS/GOARCH pair. This will no longer be necessary after
252
+ // https://github.com/gopherjs/gopherjs/issues/693.
253
+ nativesContext .bctx .GOARCH = build .Default .GOARCH
254
+ nativesContext .bctx .BuildTags = append (nativesContext .bctx .BuildTags , "js" )
255
+ }
256
+
257
+ return nativesContext
258
+ }
259
+
243
260
// parseAndAugment parses and returns all .go files of given pkg.
244
261
// Standard Go library packages are augmented with files in compiler/natives folder.
245
262
// If isTest is true and pkg.ImportPath has no _test suffix, package is built for running internal tests.
@@ -262,15 +279,7 @@ func parseAndAugment(xctx XContext, pkg *PackageData, isTest bool, fileSet *toke
262
279
importPath = importPath [:len (importPath )- 5 ]
263
280
}
264
281
265
- nativesContext := embeddedCtx (& withPrefix {fs : natives .FS , prefix : DefaultGOROOT }, "" , nil )
266
-
267
- if importPath == "syscall" {
268
- // Special handling for the syscall package, which uses OS native
269
- // GOOS/GOARCH pair. This will no longer be necessary after
270
- // https://github.com/gopherjs/gopherjs/issues/693.
271
- nativesContext .bctx .GOARCH = build .Default .GOARCH
272
- nativesContext .bctx .BuildTags = append (nativesContext .bctx .BuildTags , "js" )
273
- }
282
+ nativesContext := newNativesContext (importPath )
274
283
275
284
if nativesPkg , err := nativesContext .Import (importPath , "" , 0 ); err == nil {
276
285
names := nativesPkg .GoFiles
@@ -699,6 +708,13 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
699
708
hashFile := func () error {
700
709
fp := filepath .Join (pkg .Dir , name )
701
710
file , err := buildutil .OpenFile (pkg .bctx , name )
711
+ if errors .Is (err , os .ErrNotExist ) {
712
+ nativesCtx := newNativesContext (pkg .ImportPath )
713
+ if nativesPkg , e := nativesCtx .Import (pkg .ImportPath , "" , 0 ); e == nil {
714
+ fullPath := path .Join (nativesPkg .Dir , name )
715
+ file , err = nativesCtx .bctx .OpenFile (fullPath )
716
+ }
717
+ }
702
718
if err != nil {
703
719
return fmt .Errorf ("failed to open %v: %v" , fp , err )
704
720
}
0 commit comments