Skip to content

Commit 5a014dd

Browse files
authored
tinygo: add relative and absolute --dir options to wasmtime args (#4431)
main: add relative and absolute --dir options to wasmtime args
1 parent d948941 commit 5a014dd

File tree

2 files changed

+33
-59
lines changed

2 files changed

+33
-59
lines changed

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ tinygo: ## Build the TinyGo compiler
293293
@if [ ! -f "$(LLVM_BUILDDIR)/bin/llvm-config" ]; then echo "Fetch and build LLVM first by running:"; echo " $(MAKE) llvm-source"; echo " $(MAKE) $(LLVM_BUILDDIR)"; exit 1; fi
294294
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GOENVFLAGS) $(GO) build -buildmode exe -o build/tinygo$(EXE) -tags "byollvm osusergo" -ldflags="-X github.com/tinygo-org/tinygo/goenv.GitSha1=`git rev-parse --short HEAD`" .
295295
test: wasi-libc check-nodejs-version
296-
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -timeout=20m -buildmode exe -tags "byollvm osusergo" $(GOTESTPKGS)
296+
CGO_CPPFLAGS="$(CGO_CPPFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) test $(GOTESTFLAGS) -v -timeout=1h -buildmode exe -tags "byollvm osusergo" $(GOTESTPKGS)
297297

298298
# Standard library packages that pass tests on darwin, linux, wasi, and windows, but take over a minute in wasi
299299
TEST_PACKAGES_SLOW = \

main.go

Lines changed: 32 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -283,46 +283,6 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
283283
// Tests are always run in the package directory.
284284
cmd.Dir = result.MainDir
285285

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-
326286
// Run the test.
327287
start := time.Now()
328288
err = cmd.Run()
@@ -848,12 +808,11 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
848808
for _, v := range environmentVars {
849809
emuArgs = append(emuArgs, "--env", v)
850810
}
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...)
857816

858817
// Set this for nicer backtraces during tests, but don't override the user.
859818
if _, ok := os.LookupEnv("WASMTIME_BACKTRACE_DETAILS"); !ok {
@@ -903,21 +862,36 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
903862

904863
name = emulator[0]
905864

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.
906871
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)
917885
}
918-
emuArgs = append(emuArgs, "--dir="+dir)
919-
emuArgs = append(emuArgs, "--env=PWD="+dir)
920886
}
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)
921895
}
922896

923897
emuArgs = append(emuArgs, emulator[1:]...)

0 commit comments

Comments
 (0)