From 12e80b4bc5c0b6fe362d189f781bcf7f25e13fd5 Mon Sep 17 00:00:00 2001 From: Crypt Keeper <64215+codefromthecrypt@users.noreply.github.com> Date: Wed, 15 Mar 2023 13:09:19 +0800 Subject: [PATCH] cli: removes experimental-workdir-inherit flag (#1242) Signed-off-by: Adrian Cole --- cmd/wazero/wazero.go | 24 +++++++++++++++--------- cmd/wazero/wazero_test.go | 1 - 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cmd/wazero/wazero.go b/cmd/wazero/wazero.go index 4291da65..d86eab52 100644 --- a/cmd/wazero/wazero.go +++ b/cmd/wazero/wazero.go @@ -18,6 +18,7 @@ import ( "github.com/tetratelabs/wazero/experimental/gojs" "github.com/tetratelabs/wazero/experimental/logging" "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" + "github.com/tetratelabs/wazero/internal/platform" "github.com/tetratelabs/wazero/internal/version" "github.com/tetratelabs/wazero/sys" ) @@ -128,12 +129,6 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer, exit func(cod "inherits any environment variables from the calling process. "+ "Variables specified with the flag are appended to the inherited list.") - var workdirInherit bool - flags.BoolVar(&workdirInherit, "experimental-workdir-inherit", false, - "inherits the working directory from the calling process. "+ - "Note: This only applies to wasm compiled with `GOARCH=wasm GOOS=js` a.k.a. gojs. "+ - "In windows, the working directory must be on the same volume as the root mount.") - var mounts sliceFlag flags.Var(&mounts, "mount", "filesystem path to expose to the binary in the form of [:][:ro]. "+ @@ -192,7 +187,7 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer, exit func(cod env = append(env, fields[0], fields[1]) } - fsConfig := validateMounts(mounts, stdErr, exit) + rootPath, fsConfig := validateMounts(mounts, stdErr, exit) wasm, err := os.ReadFile(wasmPath) if err != nil { @@ -269,7 +264,14 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer, exit func(cod gojs.MustInstantiate(ctx, rt) config := gojs.NewConfig(conf) - if workdirInherit { + + // Strip the volume of the path, for example C:\ + rootDir := rootPath[len(filepath.VolumeName(rootPath)):] + + // If the user mounted the entire filesystem, try to inherit the CWD. + // This is better than introducing a flag just for GOOS=js, especially + // as removing flags breaks syntax compat. + if platform.ToPosixPath(rootDir) == "/" { config = config.WithOSWorkdir() } @@ -294,7 +296,7 @@ func doRun(args []string, stdOut io.Writer, stdErr logging.Writer, exit func(cod exit(0) } -func validateMounts(mounts sliceFlag, stdErr logging.Writer, exit func(code int)) (config wazero.FSConfig) { +func validateMounts(mounts sliceFlag, stdErr logging.Writer, exit func(code int)) (rootPath string, config wazero.FSConfig) { config = wazero.NewFSConfig() for _, mount := range mounts { if len(mount) == 0 { @@ -342,6 +344,10 @@ func validateMounts(mounts sliceFlag, stdErr logging.Writer, exit func(code int) } else { config = config.WithDirMount(dir, guestPath) } + + if guestPath == "/" { + rootPath = dir + } } return } diff --git a/cmd/wazero/wazero_test.go b/cmd/wazero/wazero_test.go index 27e79233..8d567c73 100644 --- a/cmd/wazero/wazero_test.go +++ b/cmd/wazero/wazero_test.go @@ -361,7 +361,6 @@ func TestRun(t *testing.T) { wazeroOpts: []string{ // --mount=X:\:/ on Windows, --mount=/:/ everywhere else "--mount=" + filepath.VolumeName(bearDir) + string(os.PathSeparator) + ":/", - "--experimental-workdir-inherit=true", }, workdir: bearDir, wasmArgs: []string{"bear.txt"},