Skip to content

Commit

Permalink
main: fix cwd for package and single-file tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski committed Apr 24, 2024
1 parent 79b158e commit cd32f3b
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ func Run(pkgName string, options *compileopts.Options, cmdArgs []string) error {
// passes command line arguments and evironment 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,
Expand Down Expand Up @@ -841,16 +844,6 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
}
}
} else if config.EmulatorName() == "wasmtime" {
// Wasmtime needs some special flags to pass environment variables
// and allow reading from the current directory.
switch config.Options.Target {
case "wasip1":
emuArgs = append(emuArgs, "--dir=.")
case "wasip2":
cwd, _ := os.Getwd()
emuArgs = append(emuArgs, "--dir="+cwd)
emuArgs = append(emuArgs, "--env=PWD="+cwd)
}
for _, v := range environmentVars {
emuArgs = append(emuArgs, "--env", v)
}
Expand Down Expand Up @@ -906,7 +899,26 @@ func buildAndRun(pkgName string, config *compileopts.Config, stdout io.Writer, c
if err != nil {
return result, err
}

name = emulator[0]

if name == "wasmtime" {
// Wasmtime needs some special flags to pass environment variables
// and allow reading from the current directory.
switch config.Options.Target {
case "wasip1":
emuArgs = append(emuArgs, "--dir=.")
case "wasip2":
dir := result.MainDir
if isSingleFile {
cwd, _ := os.Getwd()
dir = cwd
}
emuArgs = append(emuArgs, "--dir="+dir)
emuArgs = append(emuArgs, "--env=PWD="+dir)
}
}

emuArgs = append(emuArgs, emulator[1:]...)
args = append(emuArgs, args...)
}
Expand Down

0 comments on commit cd32f3b

Please sign in to comment.