gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Failure to execute shell commands with Windows

Open titooradhakrishnan opened this issue 1 year ago • 13 comments

What happened?

Description: The agent was unable to perform a simple image manipulation task on a Windows machine. The core issue appears to be a persistent failure in the run_shell_command tool when handling absolute file paths on Windows, especially paths containing spaces (e.g., C:\Users\Titoo Radhakrishnan\Desktop\New folder (2)).

Attempts and Failures:

  1. Python Scripts: Multiple attempts to run a Python script using run_shell_command failed with [Errno 22] Invalid argument or SyntaxError: unterminated string literal. This occurred with various path formats (, /) and quoting (", ').
  2. Change Directory: Attempts to cd into the target directory within the command failed with The filename, directory name, or volume label syntax is incorrect.
  3. PowerShell: Attempts using PowerShell cmdlets and .NET libraries appeared to execute without error but produced no output file, suggesting a silent failure.
  4. Verification: The agent successfully used the write_file tool to create a text file in the target directory, confirming write permissions were not the issue.

What did you expect to happen?

Description: The agent was unable to perform a simple image manipulation task on a Windows machine. The core issue appears to be a persistent failure in the run_shell_command tool when handling absolute file paths on Windows, especially paths containing spaces (e.g., C:\Users\Titoo Radhakrishnan\Desktop\New folder (2)).

Attempts and Failures:

  1. Python Scripts: Multiple attempts to run a Python script using run_shell_command failed with [Errno 22] Invalid argument or SyntaxError: unterminated string literal. This occurred with various path formats (, /) and quoting (", ').
  2. Change Directory: Attempts to cd into the target directory within the command failed with The filename, directory name, or volume label syntax is incorrect.
  3. PowerShell: Attempts using PowerShell cmdlets and .NET libraries appeared to execute without error but produced no output file, suggesting a silent failure.
  4. Verification: The agent successfully used the write_file tool to create a text file in the target directory, confirming write permissions were not the issue.

Client information

  • CLI Version: 0.1.9
  • Git Commit: 34935d6
  • Operating System: win32 v22.17.0
  • Sandbox Environment: no sandbox
  • Model Version: gemini-2.5-pro
  • Memory Usage: 134.8 MB

Login information

No response

Anything else we need to know?

No response Conclusion: The run_shell_command tool seems to have a fundamental problem with passing quoted paths to the Windows shell, making it impossible to work with files in common user directories.

titooradhakrishnan avatar Jul 02 '25 14:07 titooradhakrishnan

I "may" be having a similar issue (although it seems to be better now) with my project folder located on my desktop. Possible something with that file path is wonky or something?

paradroidlabs avatar Jul 03 '25 03:07 paradroidlabs

Here is a specific example that illustrates the issue with command line argument quoting. When gemini wants to launch this command:

git add . && git commit -m "style: Apply C# formatting"

It runs this program:

cmd.exe /c "git add . && git commit -m \"style: Apply C# formatting\""

Which launch the git commit command with this command:

git  commit -m \"style: Apply C# formatting\"

When this command line is processed with the example program for CommandLineToArgvW, the arguments are split like this:

C:\temp\ConsoleApplication1\x64\Debug>ConsoleApplication1.exe git  commit -m \"style: Apply C# formatting\"
0: ConsoleApplication1.exe
1: git
2: commit
3: -m
4: "style:
5: Apply
6: C#
7: formatting"

If the backslashes are removed, the arguments are parsed as expected.

C:\temp\ConsoleApplication1\x64\Debug>ConsoleApplication1.exe git  commit -m "style: Apply C# formatting"
0: ConsoleApplication1.exe
1: git
2: commit
3: -m
4: style: Apply C# formatting

AustinWise avatar Jul 03 '25 18:07 AustinWise

This might be fixed by #2187. This issue might be a duplicate of #2889.

AustinWise avatar Jul 03 '25 18:07 AustinWise

As a work around if you argue with Gemini CLI enough it will often find an alternative way to work on Windows.

eg "try harder you are on windows use type etc to read files"

Gemini also created a suite of python scripts itself to read and write to Windows files!

It suggested I use this prompt in future:

"On Windows, please modify [file_path] to [describe the desired change]. If your direct file modification tools encounter issues, please generate and execute a Python script to perform the change."

Image

lockster-at-secondskin avatar Jul 08 '25 09:07 lockster-at-secondskin

Found possible duplicate issues (updated):

  • #4973: (0.9146103757155389)
  • #4279: (0.9022998751580656)

gsquared94 avatar Jul 30 '25 05:07 gsquared94

This may be fixed. First step is to investigate and evaluate whether this is still an issue for some users.

jacob314 avatar Aug 19 '25 15:08 jacob314

Looks like it's working though there is a subtle difference in windows vs macos:

In windows:

 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✓  Shell echo "foo    bar" (A simple echo command to test handling of multiple spaces.)                                                                         │
 │                                                                                                                                                                 │
 │    "foo    bar"                                                                                                                                                 │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

In MacOs

 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✓  Shell echo "foo      bar" (Run an echo command with multiple spaces to test quote handling.)                                                                 │
 │                                                                                                                                                                 │
 │    foo      bar                                                                                                                                                 │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

I will investigate further with git commit -m "foo bar"

scidomino avatar Aug 26 '25 17:08 scidomino

Indeed. after getting git working I can repro the issue at head:

✦ Next, I will commit the staged file with the message "foo #C bar".
 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Shell git commit -m "foo #C bar" (Commits the staged changes to the Git repository with the user-provided message "foo #C bar".)                             │
 │                                                                                                                                                                 │
 │                                                                                                                                                                 │
 │    error: pathspec '#C' did not match any file(s) known to git                                                                                                  │
 │    error: pathspec 'bar"' did not match any file(s) known to git                                                                                                │
 │                                                                                                                                                                 │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ The last command failed because the '#' symbol in the commit message was interpreted as the start of a comment. I'll correct this by enclosing the message in single
  quotes to ensure the entire string is used.
 ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Shell git commit -m 'foo #C bar' (Commits the staged changes to the Git repository with the user-provided message "foo #C bar", using single quotes to prevent shell … │
 │                                                                                                                                                                           │
 │                                                                                                                                                                           │
 │    error: pathspec '#C' did not match any file(s) known to git                                                                                                            │
 │    error: pathspec 'bar'' did not match any file(s) known to git                                                                                                          │
 │                                                                                                                                                                           │
 ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯```

scidomino avatar Aug 26 '25 19:08 scidomino

Looks like we are in the process of changing how we execute shell commands: https://github.com/google-gemini/gemini-cli/pull/6491

I'm going to test this with shouldUseNodePtyShell:true

scidomino avatar Aug 26 '25 21:08 scidomino

The bug is present if both versions of the command. The old version will always be there as a fallback so I will need to fix both paths.

scidomino avatar Aug 26 '25 21:08 scidomino

The problem still exists for \n on Windows. Gemini really struggled with it.

wzm2256 avatar Feb 10 '26 06:02 wzm2256

@wzm2256 I don't understand your comment. Do you mean "windows still has trouble with line returns?" Yeah, escaping text is hard for the models. I don't know if there's much we can do to help it.

scidomino avatar Feb 10 '26 17:02 scidomino

@wzm2256 I don't understand your comment. Do you mean "windows still has trouble with line returns?" Yeah, escaping text is hard for the models. I don't know if there's much we can do to help it.

Yes. That is what I meant. When gemini tries to write print('\n This is a message') to a python file, sometimes it will write print(' This is a message') to the file, which will cause SyntaxError: unterminated string literal when runing the file.

wzm2256 avatar Feb 11 '26 03:02 wzm2256