Skip to content

layerfs: exec never runs on Windows — runShell spawns /bin/sh, which does not exist there (ENOENT) #1325

Open

Description

@zhibo0502

layerfs_exec cannot run on Windows at all. runShell in layerfs.mjs:302 spawns /bin/sh with no platform branch, so the call fails before the command starts — which means turning the plugin's exec option on does nothing for Windows users.

{"project":"demo","branch":"main","exitCode":-1,"stdout":"",
 "stderr":"Error: ENOENT: no such file or directory, uv_spawn '/bin/sh'",
 "changed":false,"commit":null}

Repro

Windows 11 (10.0.26300), magpie 0.1.4452, @magpie-community/tools-layerfs@0.10.3:

  1. magpie plugin options tools-layerfs '{"exec": true}'
  2. provision a project, answer a workspace path (this part works fine)
  3. exec any command, e.g. echo hello

Expected: the command runs in the workspace and its changes are committed, as on POSIX.
Actual: ENOENT, exitCode: -1, no command runs, nothing commits. No workspace is modified, so it reads like the exec switch did nothing.

Cause

const child = spawn("/bin/sh", ["-c", command], {
  cwd: dir,
  detached: true,
  ...
  env: { ...process.env, TMPDIR: join(dir, ".tmp"), XDG_CACHE_HOME: join(dir, ".cache") },
})

Windows has no /bin/sh (Node resolves it from the current drive root, e.g. C:\bin\sh.EXE). The shell there is %ComSpec% — cmd.exe /d /s /c <command>.

Two related Windows gaps in the same function

  • Scratch is not actually kept in the workspace. Only TMPDIR and XDG_CACHE_HOME are redirected; Windows programs read TEMP/TMP. Even with a shell, toolchain temp files would still land in the host's temp directory instead of the workspace .tmp.
  • The timeout does not kill the tree. detached: true plus process.kill(-child.pid, "SIGKILL") is POSIX process-group semantics: on Windows detached opens a new console and the negative-PID kill throws, so the catch fallback kills only cmd.exe and its children keep running past the 55 s cap.

Patch that makes it work here

+const IS_WIN = process.platform === "win32"
+function shellFor(command) {
+  if (IS_WIN) {
+    return {
+      file: process.env.ComSpec || process.env.COMSPEC || "cmd.exe",
+      args: ["/d", "/s", "/c", command],
+    }
+  }
+  return { file: "/bin/sh", args: ["-c", command] }
+}
+
 function runShell(command, dir, timeoutMs) {
   return new Promise((resolvePromise) => {
     ensureScratch(dir)
-    const child = spawn("/bin/sh", ["-c", command], {
+    const scratchEnv = IS_WIN
+      ? { TMPDIR: join(dir, ".tmp"), TMP: join(dir, ".tmp"), TEMP: join(dir, ".tmp"), XDG_CACHE_HOME: join(dir, ".cache") }
+      : { TMPDIR: join(dir, ".tmp"), XDG_CACHE_HOME: join(dir, ".cache") }
+    const sh = shellFor(command)
+    const child = spawn(sh.file, sh.args, {
       cwd: dir,
-      detached: true,
+      detached: !IS_WIN,
       stdio: ["ignore", "pipe", "pipe"],
-      env: { ...process.env, TMPDIR: join(dir, ".tmp"), XDG_CACHE_HOME: join(dir, ".cache") },
+      env: { ...process.env, ...scratchEnv },
     })

With this applied, on the same machine:

  • echo hello → exitCode 0, changed: false, commit: null (a no-change command commits nothing, as documented)
  • create a file + append to another → exitCode 0, auto-commit with added 1 / modified 1
  • ... && exit 3 → exitCode 3, changes still checkpointed
  • TMP, TEMP and XDG_CACHE_HOME all resolve inside the workspace .tmp / .cache

Two things a real fix has to decide, which the patch above does not: the command language becomes cmd on Windows (&&, &, %VAR%, and </> need quoting — an unquoted < in echo fails with was unexpected at this time), and the 55 s cap needs a real tree kill (e.g. taskkill /T /F /PID), otherwise children outlive the timeout.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions