Skip to content

Commit 0edeaf6

Browse files
authored
tinygo: revise and simplify wasmtime argument handling (#4555)
1 parent 76d5b3d commit 0edeaf6

File tree

5 files changed

+34
-32
lines changed

5 files changed

+34
-32
lines changed

compileopts/target.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ func defaultTarget(options *Options) (*TargetSpec, error) {
452452
"--stack-first",
453453
"--no-demangle",
454454
)
455-
spec.Emulator = "wasmtime --dir={tmpDir}::/tmp {}"
455+
spec.Emulator = "wasmtime run --dir={tmpDir}::/tmp {}"
456456
spec.ExtraFiles = append(spec.ExtraFiles,
457457
"src/runtime/asm_tinygowasm.S",
458458
"src/internal/task/task_asyncify_wasm.S",

main.go

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -769,9 +769,6 @@ func Run(pkgName string, options *compileopts.Options, cmdArgs []string) error {
769769
// passes command line arguments and environment variables in a way appropriate
770770
// for the given emulator.
771771
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) {
772-
773-
isSingleFile := strings.HasSuffix(pkgName, ".go")
774-
775772
// Determine whether we're on a system that supports environment variables
776773
// and command line parameters (operating systems, WASI) or not (baremetal,
777774
// 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
784781
needsEnvInVars = true
785782
}
786783
}
787-
var args, emuArgs, env []string
784+
var args, env []string
788785
var extraCmdEnv []string
789786
if needsEnvInVars {
790787
runtimeGlobals := make(map[string]string)
@@ -804,20 +801,6 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
804801
"runtime": runtimeGlobals,
805802
}
806803
}
807-
} else if config.EmulatorName() == "wasmtime" {
808-
for _, v := range environmentVars {
809-
emuArgs = append(emuArgs, "--env", v)
810-
}
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...)
816-
817-
// Set this for nicer backtraces during tests, but don't override the user.
818-
if _, ok := os.LookupEnv("WASMTIME_BACKTRACE_DETAILS"); !ok {
819-
extraCmdEnv = append(extraCmdEnv, "WASMTIME_BACKTRACE_DETAILS=1")
820-
}
821804
} else {
822805
// Pass environment variables and command line parameters as usual.
823806
// This also works on qemu-aarch64 etc.
@@ -860,7 +843,7 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
860843
return result, err
861844
}
862845

863-
name = emulator[0]
846+
name, emulator = emulator[0], emulator[1:]
864847

865848
// wasmtime is a WebAssembly runtime CLI with WASI enabled by default.
866849
// 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
869852
// outside the package directory. Other tests require temporary
870853
// writeable directories. We allow this by adding wasmtime flags below.
871854
if name == "wasmtime" {
855+
var emuArgs []string
856+
857+
// Extract the wasmtime subcommand (e.g. "run" or "serve")
858+
if len(emulator) > 1 {
859+
emuArgs = append(emuArgs, emulator[0])
860+
emulator = emulator[1:]
861+
}
862+
863+
wd, _ := os.Getwd()
864+
872865
// Below adds additional wasmtime flags in case a test reads files
873866
// outside its directory, like "../testdata/e.txt". This allows any
874867
// relative directory up to the module root, even if the test never
875868
// reads any files.
876869
if config.TestConfig.CompileTestBinary {
870+
// Set working directory to package dir
871+
wd = result.MainDir
872+
877873
// Add relative dirs (../, ../..) up to module root (for wasip1)
878874
dirs := dirsToModuleRootRel(result.MainDir, result.ModuleRoot)
879875

@@ -883,19 +879,25 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
883879
for _, d := range dirs {
884880
emuArgs = append(emuArgs, "--dir="+d)
885881
}
882+
} else {
883+
emuArgs = append(emuArgs, "--dir=.")
886884
}
887885

888-
dir := result.MainDir
889-
if isSingleFile {
890-
dir, _ = os.Getwd()
886+
emuArgs = append(emuArgs, "--dir="+wd)
887+
emuArgs = append(emuArgs, "--env=PWD="+wd)
888+
for _, v := range environmentVars {
889+
emuArgs = append(emuArgs, "--env", v)
891890
}
892-
emuArgs = append(emuArgs, "--dir=.")
893-
emuArgs = append(emuArgs, "--dir="+dir)
894-
emuArgs = append(emuArgs, "--env=PWD="+dir)
891+
892+
// Set this for nicer backtraces during tests, but don't override the user.
893+
if _, ok := os.LookupEnv("WASMTIME_BACKTRACE_DETAILS"); !ok {
894+
extraCmdEnv = append(extraCmdEnv, "WASMTIME_BACKTRACE_DETAILS=1")
895+
}
896+
897+
emulator = append(emuArgs, emulator...)
895898
}
896899

897-
emuArgs = append(emuArgs, emulator[1:]...)
898-
args = append(emuArgs, args...)
900+
args = append(emulator, args...)
899901
}
900902
var cmd *exec.Cmd
901903
if ctx != nil {
@@ -925,7 +927,7 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
925927

926928
// Run binary.
927929
if config.Options.PrintCommands != nil {
928-
config.Options.PrintCommands(cmd.Path, cmd.Args...)
930+
config.Options.PrintCommands(cmd.Path, cmd.Args[1:]...)
929931
}
930932
err = run(cmd, result)
931933
if err != nil {

targets/wasip1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
"extra-files": [
2424
"src/runtime/asm_tinygowasm.S"
2525
],
26-
"emulator": "wasmtime --dir={tmpDir}::/tmp {}"
26+
"emulator": "wasmtime run --dir={tmpDir}::/tmp {}"
2727
}

targets/wasip2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"extra-files": [
2626
"src/runtime/asm_tinygowasm.S"
2727
],
28-
"emulator": "wasmtime --wasm component-model -Sinherit-network -Sallow-ip-name-lookup --dir={tmpDir}::/tmp {}",
28+
"emulator": "wasmtime run --wasm component-model -Sinherit-network -Sallow-ip-name-lookup --dir={tmpDir}::/tmp {}",
2929
"wit-package": "{root}/lib/wasi-cli/wit/",
3030
"wit-world": "wasi:cli/command"
3131
}

targets/wasm-unknown.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
"extra-files": [
2525
"src/runtime/asm_tinygowasm.S"
2626
],
27-
"emulator": "wasmtime --dir={tmpDir}::/tmp {}"
27+
"emulator": "wasmtime run --dir={tmpDir}::/tmp {}"
2828
}

0 commit comments

Comments
 (0)