aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Reddig <[email protected]>2024-10-28 17:57:24 +0100
committerGitHub <[email protected]>2024-10-28 17:57:24 +0100
commit0edeaf657f4d1024f60ca438084c61c8139dd566 (patch)
tree37900b4bb69455bbcd3bb3d06e0ff88a9cef748f
parent76d5b3d786034b5d58167a4b47ec36539bbdf55a (diff)
downloadtinygo-0edeaf657f4d1024f60ca438084c61c8139dd566.tar.gz
tinygo-0edeaf657f4d1024f60ca438084c61c8139dd566.zip
tinygo: revise and simplify wasmtime argument handling (#4555)
-rw-r--r--compileopts/target.go2
-rw-r--r--main.go58
-rw-r--r--targets/wasip1.json2
-rw-r--r--targets/wasip2.json2
-rw-r--r--targets/wasm-unknown.json2
5 files changed, 34 insertions, 32 deletions
diff --git a/compileopts/target.go b/compileopts/target.go
index b5df5b911..3dc8af02f 100644
--- a/compileopts/target.go
+++ b/compileopts/target.go
@@ -452,7 +452,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
"--stack-first",
"--no-demangle",
)
- spec.Emulator = "wasmtime --dir={tmpDir}::/tmp {}"
+ spec.Emulator = "wasmtime run --dir={tmpDir}::/tmp {}"
spec.ExtraFiles = append(spec.ExtraFiles,
"src/runtime/asm_tinygowasm.S",
"src/internal/task/task_asyncify_wasm.S",
diff --git a/main.go b/main.go
index fe8a3fb15..254e140cf 100644
--- a/main.go
+++ b/main.go
@@ -769,9 +769,6 @@ func Run(pkgName string, options *compileopts.Options, cmdArgs []string) error {
// passes command line arguments and environment variables in a way appropriate
// for the given emulator.
func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, cmdArgs, environmentVars []string, timeout time.Duration, run func(cmd *exec.Cmd, result builder.BuildResult) error) (builder.BuildResult, error) {
-
- isSingleFile := strings.HasSuffix(pkgName, ".go")
-
// Determine whether we're on a system that supports environment variables
// and command line parameters (operating systems, WASI) or not (baremetal,
// WebAssembly in the browser). If we're on a system without an environment,
@@ -784,7 +781,7 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
needsEnvInVars = true
}
}
- var args, emuArgs, env []string
+ var args, env []string
var extraCmdEnv []string
if needsEnvInVars {
runtimeGlobals := make(map[string]string)
@@ -804,20 +801,6 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
"runtime": runtimeGlobals,
}
}
- } else if config.EmulatorName() == "wasmtime" {
- for _, v := range environmentVars {
- emuArgs = append(emuArgs, "--env", v)
- }
-
- // Use of '--' argument no longer necessary as of Wasmtime v14:
- // https://github.com/bytecodealliance/wasmtime/pull/6946
- // args = append(args, "--")
- args = append(args, cmdArgs...)
-
- // Set this for nicer backtraces during tests, but don't override the user.
- if _, ok := os.LookupEnv("WASMTIME_BACKTRACE_DETAILS"); !ok {
- extraCmdEnv = append(extraCmdEnv, "WASMTIME_BACKTRACE_DETAILS=1")
- }
} else {
// Pass environment variables and command line parameters as usual.
// This also works on qemu-aarch64 etc.
@@ -860,7 +843,7 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
return result, err
}
- name = emulator[0]
+ name, emulator = emulator[0], emulator[1:]
// wasmtime is a WebAssembly runtime CLI with WASI enabled by default.
// By default, only stdio is allowed. For example, while STDOUT routes
@@ -869,11 +852,24 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
// outside the package directory. Other tests require temporary
// writeable directories. We allow this by adding wasmtime flags below.
if name == "wasmtime" {
+ var emuArgs []string
+
+ // Extract the wasmtime subcommand (e.g. "run" or "serve")
+ if len(emulator) > 1 {
+ emuArgs = append(emuArgs, emulator[0])
+ emulator = emulator[1:]
+ }
+
+ wd, _ := os.Getwd()
+
// Below adds additional wasmtime flags in case a test reads files
// outside its directory, like "../testdata/e.txt". This allows any
// relative directory up to the module root, even if the test never
// reads any files.
if config.TestConfig.CompileTestBinary {
+ // Set working directory to package dir
+ wd = result.MainDir
+
// Add relative dirs (../, ../..) up to module root (for wasip1)
dirs := dirsToModuleRootRel(result.MainDir, result.ModuleRoot)
@@ -883,19 +879,25 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
for _, d := range dirs {
emuArgs = append(emuArgs, "--dir="+d)
}
+ } else {
+ emuArgs = append(emuArgs, "--dir=.")
}
- dir := result.MainDir
- if isSingleFile {
- dir, _ = os.Getwd()
+ emuArgs = append(emuArgs, "--dir="+wd)
+ emuArgs = append(emuArgs, "--env=PWD="+wd)
+ for _, v := range environmentVars {
+ emuArgs = append(emuArgs, "--env", v)
}
- emuArgs = append(emuArgs, "--dir=.")
- emuArgs = append(emuArgs, "--dir="+dir)
- emuArgs = append(emuArgs, "--env=PWD="+dir)
+
+ // Set this for nicer backtraces during tests, but don't override the user.
+ if _, ok := os.LookupEnv("WASMTIME_BACKTRACE_DETAILS"); !ok {
+ extraCmdEnv = append(extraCmdEnv, "WASMTIME_BACKTRACE_DETAILS=1")
+ }
+
+ emulator = append(emuArgs, emulator...)
}
- emuArgs = append(emuArgs, emulator[1:]...)
- args = append(emuArgs, args...)
+ args = append(emulator, args...)
}
var cmd *exec.Cmd
if ctx != nil {
@@ -925,7 +927,7 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
// Run binary.
if config.Options.PrintCommands != nil {
- config.Options.PrintCommands(cmd.Path, cmd.Args...)
+ config.Options.PrintCommands(cmd.Path, cmd.Args[1:]...)
}
err = run(cmd, result)
if err != nil {
diff --git a/targets/wasip1.json b/targets/wasip1.json
index 8d1966e78..4181f16ee 100644
--- a/targets/wasip1.json
+++ b/targets/wasip1.json
@@ -23,5 +23,5 @@
"extra-files": [
"src/runtime/asm_tinygowasm.S"
],
- "emulator": "wasmtime --dir={tmpDir}::/tmp {}"
+ "emulator": "wasmtime run --dir={tmpDir}::/tmp {}"
}
diff --git a/targets/wasip2.json b/targets/wasip2.json
index 7c8394c8e..786536f2b 100644
--- a/targets/wasip2.json
+++ b/targets/wasip2.json
@@ -25,7 +25,7 @@
"extra-files": [
"src/runtime/asm_tinygowasm.S"
],
- "emulator": "wasmtime --wasm component-model -Sinherit-network -Sallow-ip-name-lookup --dir={tmpDir}::/tmp {}",
+ "emulator": "wasmtime run --wasm component-model -Sinherit-network -Sallow-ip-name-lookup --dir={tmpDir}::/tmp {}",
"wit-package": "{root}/lib/wasi-cli/wit/",
"wit-world": "wasi:cli/command"
}
diff --git a/targets/wasm-unknown.json b/targets/wasm-unknown.json
index 59cd94db4..f07a2406e 100644
--- a/targets/wasm-unknown.json
+++ b/targets/wasm-unknown.json
@@ -24,5 +24,5 @@
"extra-files": [
"src/runtime/asm_tinygowasm.S"
],
- "emulator": "wasmtime --dir={tmpDir}::/tmp {}"
+ "emulator": "wasmtime run --dir={tmpDir}::/tmp {}"
}