@@ -283,46 +283,6 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
283
283
// Tests are always run in the package directory.
284
284
cmd .Dir = result .MainDir
285
285
286
- // wasmtime is the default emulator used for `-target=wasip1`. wasmtime
287
- // is a WebAssembly runtime CLI with WASI enabled by default. However,
288
- // only stdio are allowed by default. For example, while STDOUT routes
289
- // to the host, other files don't. It also does not inherit environment
290
- // variables from the host. Some tests read testdata files, often from
291
- // outside the package directory. Other tests require temporary
292
- // writeable directories. We allow this by adding wasmtime flags below.
293
- if config .EmulatorName () == "wasmtime" {
294
- // At this point, The current working directory is at the package
295
- // directory. Ex. $GOROOT/src/compress/flate for compress/flate.
296
- // buildAndRun has already added arguments for wasmtime, that allow
297
- // read-access to files such as "testdata/huffman-zero.in".
298
- //
299
- // Ex. main(.wasm) --dir=. -- -test.v
300
-
301
- // Below adds additional wasmtime flags in case a test reads files
302
- // outside its directory, like "../testdata/e.txt". This allows any
303
- // relative directory up to the module root, even if the test never
304
- // reads any files.
305
- //
306
- // Ex. run --dir=.. --dir=../.. --dir=../../..
307
- var dirs []string
308
- switch config .Target .GOOS {
309
- case "wasip1" :
310
- dirs = dirsToModuleRootRel (result .MainDir , result .ModuleRoot )
311
- default :
312
- dirs = dirsToModuleRootAbs (result .MainDir , result .ModuleRoot )
313
- }
314
-
315
- args := []string {"run" }
316
- for _ , d := range dirs {
317
- args = append (args , "--dir=" + d )
318
- }
319
-
320
- args = append (args , "--env=PWD=" + cmd .Dir )
321
-
322
- args = append (args , cmd .Args [1 :]... )
323
- cmd .Args = args
324
- }
325
-
326
286
// Run the test.
327
287
start := time .Now ()
328
288
err = cmd .Run ()
@@ -848,12 +808,11 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
848
808
for _ , v := range environmentVars {
849
809
emuArgs = append (emuArgs , "--env" , v )
850
810
}
851
- if len (cmdArgs ) != 0 {
852
- // Use of '--' argument no longer necessary as of Wasmtime v14:
853
- // https://github.com/bytecodealliance/wasmtime/pull/6946
854
- // args = append(args, "--")
855
- args = append (args , cmdArgs ... )
856
- }
811
+
812
+ // Use of '--' argument no longer necessary as of Wasmtime v14:
813
+ // https://github.com/bytecodealliance/wasmtime/pull/6946
814
+ // args = append(args, "--")
815
+ args = append (args , cmdArgs ... )
857
816
858
817
// Set this for nicer backtraces during tests, but don't override the user.
859
818
if _ , ok := os .LookupEnv ("WASMTIME_BACKTRACE_DETAILS" ); ! ok {
@@ -903,21 +862,36 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
903
862
904
863
name = emulator [0 ]
905
864
865
+ // wasmtime is a WebAssembly runtime CLI with WASI enabled by default.
866
+ // By default, only stdio is allowed. For example, while STDOUT routes
867
+ // to the host, other files don't. It also does not inherit environment
868
+ // variables from the host. Some tests read testdata files, often from
869
+ // outside the package directory. Other tests require temporary
870
+ // writeable directories. We allow this by adding wasmtime flags below.
906
871
if name == "wasmtime" {
907
- // Wasmtime needs some special flags to pass environment variables
908
- // and allow reading from the current directory.
909
- switch config .Options .Target {
910
- case "wasip1" :
911
- emuArgs = append (emuArgs , "--dir=." )
912
- case "wasip2" :
913
- dir := result .MainDir
914
- if isSingleFile {
915
- cwd , _ := os .Getwd ()
916
- dir = cwd
872
+ // Below adds additional wasmtime flags in case a test reads files
873
+ // outside its directory, like "../testdata/e.txt". This allows any
874
+ // relative directory up to the module root, even if the test never
875
+ // reads any files.
876
+ if config .TestConfig .CompileTestBinary {
877
+ // Add relative dirs (../, ../..) up to module root (for wasip1)
878
+ dirs := dirsToModuleRootRel (result .MainDir , result .ModuleRoot )
879
+
880
+ // Add absolute dirs up to module root (for wasip2)
881
+ dirs = append (dirs , dirsToModuleRootAbs (result .MainDir , result .ModuleRoot )... )
882
+
883
+ for _ , d := range dirs {
884
+ emuArgs = append (emuArgs , "--dir=" + d )
917
885
}
918
- emuArgs = append (emuArgs , "--dir=" + dir )
919
- emuArgs = append (emuArgs , "--env=PWD=" + dir )
920
886
}
887
+
888
+ dir := result .MainDir
889
+ if isSingleFile {
890
+ dir , _ = os .Getwd ()
891
+ }
892
+ emuArgs = append (emuArgs , "--dir=." )
893
+ emuArgs = append (emuArgs , "--dir=" + dir )
894
+ emuArgs = append (emuArgs , "--env=PWD=" + dir )
921
895
}
922
896
923
897
emuArgs = append (emuArgs , emulator [1 :]... )
0 commit comments