Skip to content

[Bug]:Command Execution Fails on Windows with "Cannot run program 'sh'" Error #1396

Open

Description

@china-xu

Description
When attempting to execute Shell commands via LocalFilesystemWithShell on Windows, the following exception occurs:

io.agentscope.harness.agent.filesystem.local.LocalFilesystemWithShell -- Command execution failed: Cannot run program "sh" (in directory "E:\agentscope\workspace\3846161173172182283"): CreateProcess error=2, 系统找不到指定的文件。
java.io.IOException: Cannot run program "sh" (in directory "E:\agentscope\workspace\3846161173172182283"): CreateProcess error=2, 系统找不到指定的文件。
	at java.base/java.lang.ProcessBuilder.start(ProcessBuilder.java:1143)
	...

The error occurs because Windows does not have the sh (Unix Shell) command available by default, causing the CreateProcess call to fail.

Steps to Reproduce

  1. Run the relevant code on a Windows system.
  2. Invoke LocalFilesystemWithShell.execute() or use ShellExecuteTool to run a Shell command.
  3. Observe the console or logs for the above exception.

Expected Behavior
On Windows systems, the code should either:

  • Automatically detect the OS and switch to an appropriate shell (e.g., cmd or PowerShell).
  • Provide clear instructions to the user about how to configure the shell environment.

Root Cause

  • The code does not check the operating system type and defaults to using the Unix sh command.
  • There is insufficient cross-platform support for Windows environments.

Environment Information

  • OS: Windows
  • Working Directory: E:\agentscope\workspace\3846161173172182283
  • Related Modules: LocalFilesystemWithShell, ShellExecuteTool

Suggested Solutions

  1. OS Detection: Before executing shell commands, detect the current operating system:
    • On Windows, use cmd /c or PowerShell for command execution.
    • On Unix/Linux/macOS, continue using sh.
  2. Configurable Shell Path: Allow users to configure a custom shell path via settings or environment variables.
  3. Documentation: Clearly document that Windows users need to install a sh-compatible environment (e.g., Git Bash, Cygwin) or use alternative cross-platform command execution methods.

Relevant Code/Logs
See the provided stack trace.

Additional Notes
If this is the first time running this feature on Windows, please consider adding cross-platform compatibility notes to the documentation to help other users avoid similar issues.

Activity

  1. added
    bug
    on May 15, 2026
  2. added
    help wanted
    good first issue
    on May 15, 2026
  3. wlgusqkr commented on May 15, 2026

    @wlgusqkr

    Hi, I'd like to work on this. Could it be assigned to me? Thanks!

  4. zshs000 commented on May 15, 2026

    @zshs000

    I think we can refer to pi's Windows compatibility approach:
    Docs: https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/docs/windows.md
    Key design points from pi:

    1. Auto-detection: Checks for available shells in priority order (custom path → sh → bash → powershell → cmd.exe)
    2. Configuration first: Users can set a custom shell path via shellPath in settings.json
    3. Clear documentation: Explicitly tells users to install Git for Windows, with configuration examples

    AgentScope could adopt a similar design — add auto-detection logic in LocalFilesystemWithShell and a shellPath config option in LocalFilesystemSpec.

  5. wlgusqkr commented on May 20, 2026

    @wlgusqkr

    I have analyzed the issue and put together an implementation plan. Since AgentScope Java currently doesn't have a global settings.json system like the Pi project, I would love to get your feedback on a few design choices before I start writing the code.

    Here is my analysis and proposed approach:

    1. Current Findings

    • The issue originates in LocalFilesystemWithShell (within agentscope-harness), not ShellCommandTool.
    • ShellExecuteTool delegates execution to AbstractSandboxFilesystem.execute(...).
    • Ultimately, LocalFilesystemWithShell.execute(...) is called when using the default local filesystem mode or LocalFilesystemSpec.
    • Currently, LocalFilesystemSpec only supports fields like executeTimeoutSeconds, maxOutputBytes, env, inheritEnv, and virtualMode.
    • Unlike Pi, AgentScope Java doesn't seem to have a global settings.json configuration system yet.

    2. Proposed Implementation: Shell Selection Logic

    For Unix/macOS, we keep the existing sh -c approach. For Windows, we will implement a shell discovery logic with the following priority:

    1. Explicitly configured shellPath (if provided)
    2. sh (if available in PATH)
    3. Git Bash default installation path (C:\Program Files\Git\bin\bash.exe)
    4. bash.exe (if available in PATH)
    5. If nothing is found, throw a clear exception guiding the user to install a compatible shell or set the shellPath.

    Depending on the detected shell, the ProcessBuilder arguments will be adjusted:

    • sh: sh -c <command>
    • bash: bash -lc <command>

    3. Open Design Questions (Feedback Requested)

    Before proceeding, I would like to get the maintainers' opinions on two main decisions:

    Question 1: Where should we configure shellPath?

    • **Option A: Add directly to LocalFilesystemSpec**Java

      HarnessAgent.builder()
          .filesystem(new LocalFilesystemSpec()
              .shellPath("C:\\Program Files\\Git\\bin\\bash.exe"))
          .build();
      

      (Pros: Fits existing config style, small scope. Cons: Requires users to explicitly configure LocalFilesystemSpec instead of using the default mode; placing local environment paths inside Java code can feel a bit awkward.)

    • Option B: Add a dedicated config object (e.g., LocalShellConfig)

    Java

      new LocalFilesystemSpec()
          .shellConfig(LocalShellConfig.builder()
              .shellPath("C:\\Program Files\\Git\\bin\\bash.exe").build())
    

    (Pros: Easily extensible for future shell types/args. Cons: Might be overkill for just this issue.)

    • Option C: Read from a local settings file (e.g., .agentscope/settings.json)(Pros: Cleanest UX, users don't need to modify Java code for environment setup. Cons: Requires building a new global configuration system, expanding the scope of this PR significantly.)

    Question 2: Should we require a Bash-compatible shell on Windows?

    Most commands generated by agents (e.g., ls -la, grep -R, export NODE_ENV=test) follow Unix semantics. These will likely fail or behave unexpectedly if executed in cmd.exe or powershell.exe.

    • Option 1 (My preference for V1): Strict Bash Compatibility. We strictly look for sh/bash. If not found, we throw a clear error guiding the user to install Git for Windows or set shellPath. This guarantees that agent commands execute consistently across platforms.
    • Option 2: Best-effort Fallback. We add powershell.exe and cmd.exe to the bottom of the discovery priority. This prevents the "Cannot run program" error on clean Windows machines, but we risk agents failing downstream due to command syntax errors.

    Could you let me know which approach you prefer for the shellPath configuration and the PowerShell/cmd fallback? Once we decide, I'll submit a PR!

  6. added
    area/harness
    on May 28, 2026
  7. wlgusqkr commented on Jun 3, 2026

    @wlgusqkr

    Hi @zshs000 , just a gentle ping on this!

    To save you some time, I’ve summarized my previous analysis and the implementation plan below. Let me know if you have any thoughts on the options so I can start working on the PR. Thanks!

    Summary: Windows Shell Execution Support in LocalFilesystemWithShell

    1.Proposed Approach
    I plan to implement shell discovery logic specifically for Windows. The discovery priority will be:
    Explicit shellPath -> sh (in PATH) -> Git Bash default path -> bash.exe (in PATH).
    If nothing is found, it will throw an exception. The ProcessBuilder arguments will automatically adjust based on the detected shell.

    2.Open Design Questions (Feedback Needed)

    Q1: Where should we configure shellPath?

    Option A: Add directly to LocalFilesystemSpec. (Pros: Simple. Cons: Requires users to hardcode local environment paths inside Java code).

    Option B: Add a dedicated LocalShellConfig object. (Pros: Extensible. Cons: Might be overkill for this specific issue).

    Option C: Introduce a global .agentscope/settings.json. (Pros: Cleanest UX. Cons: Significantly expands the scope of this PR).

    Q2: Should we strictly require a Bash-compatible shell on Windows?

    Option 1 : Strict Bash compatibility. If not found, throw a clear error guiding the user to install Git Bash. This guarantees that Unix-style commands generated by agents execute consistently.

    Option 2: Best-effort fallback to PowerShell/cmd. Prevents initial "Cannot run program" errors, but risks downstream syntax errors when agents generate Unix-specific commands.

  8. zshs000 commented on Jun 3, 2026

    @zshs000

    Hi @wlgusqkr , thanks for the ping!
    Just to clarify - I'm not a maintainer of this project, I just shared some thoughts on this issue earlier 🙂 But here's my personal opinion as someone who suggested the Pi approach:
    For Q1: I'd recommend Option A
    Adding shellPath directly to LocalFilesystemSpec makes the most sense for this PR. It's simple, follows the existing configuration pattern, and keeps the scope focused. Option C (global settings.json) would be a great separate feature, but it's definitely out of scope for this fix.
    For Q2: I strongly recommend Option 1
    Strict Bash compatibility is the right approach here. As you mentioned, agents almost always generate Unix-style commands - falling back to PowerShell/cmd would just cause confusing downstream errors. Git Bash is very common for Windows developers, and a clear error message guiding users to install it is much better than silent failures.
    Your implementation plan looks great! You might also want to ping the actual maintainers (@LearningGp ) for their official sign-off before you start the PR.
    Good luck with this - looking forward to seeing Windows support land! 🚀

  9. wlgusqkr commented on Jun 3, 2026

    @wlgusqkr

    @zshs000, thanks for clarifying and for the great feedback! I completely agree that Option A and Option 1 make the most sense for keeping the scope manageable and ensuring consistent behavior.

    Hi @LearningGp , just pinging you as the maintainer to get your official thoughts. Would you be aligned with proceeding with Option A and Option 1 based on the summary above? Let me know if I have the green light to start working on the PR! Thanks.

  10. LearningGp commented on Jun 4, 2026

    @LearningGp
    Member
  11. ningmao-hlyz commented on Nov 11, 2026

    @ningmao-hlyz
    Contributor

    Since this has been idle since June: main now falls back to cmd.exe /c on Windows, so the crash
    in the report is gone, but the agreed design never landed. Every command is still forced through
    cmd, where POSIX syntax (&& chains, quoting, ls-style commands) behaves differently from the
    sh -c the same agent produces on Linux.

    I'm picking this up unless @wlgusqkr is still on it. Scope, following the Option A + Option 1
    conclusion above:

    • LocalFilesystemSpec.shellPath(String) for an explicit shell (Option A).
    • Discovery when unset: configured path -> sh on PATH -> Git Bash default install locations ->
      bash.exe on PATH -> cmd.exe (today's behaviour, with a warning).
    • -c for POSIX shells, /c for cmd.exe, -Command for PowerShell, so an explicit
      shellPath works for either family.

    One more thing I hit while testing this on Windows, which I will fix in the same change: when a
    custom env is set with inheritEnv=false, execute() clears the environment and keeps only the
    caller's variables, so PATH is gone and the shell starts but cannot resolve anything:

    ProcessBuilder("cmd.exe", "/c", "where git") with a cleared env
      -> 'where' is not recognized as an internal or external command
    

    A configurable shell is not much use if the child process cannot find its own tools, so the fix
    restores the variables the OS needs to start a process (PATH, SystemRoot, PATHEXT, COMSPEC,
    TEMP/TMP on Windows, HOME/LANG on POSIX) before applying the caller's overrides.

  12. moved this from Backlog to In progress in AgentScope Javaon Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/harnessbuggood first issuehelp wanted

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions