Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Aug 25, 2025

Summary

This PR fixes an issue where Roo Code fails to execute Claude Code when it's installed as a PowerShell script (.ps1 file) on Windows.

Problem

Users on Windows who install Claude Code through PowerShell (e.g., via fnm or other Node.js version managers) receive a .ps1 script instead of a binary executable. When Roo Code tries to execute this script directly, it fails with an error because PowerShell scripts cannot be executed as regular binaries.

Solution

  • Detect when the Claude Code path ends with .ps1 on Windows
  • Execute PowerShell scripts through powershell.exe with appropriate flags:
    • -NoProfile: Skip loading PowerShell profiles for security
    • -ExecutionPolicy Bypass: Allow script execution for this specific invocation only
    • -File: Specify the script file to execute
  • Maintain backward compatibility for regular executables (.exe, .cmd, etc.)
  • Add comprehensive error handling for PowerShell-specific failures

Testing

  • Added comprehensive test suite with 10 test cases covering all scenarios
  • All existing tests continue to pass
  • Tests verify correct PowerShell invocation, parameter passing, and error handling

Security Considerations

  • Uses -NoProfile to prevent loading potentially malicious PowerShell profiles
  • -ExecutionPolicy Bypass is scoped only to the specific script execution
  • No global system changes are made
  • File paths are properly handled to prevent injection attacks

Fixes #7393


Important

Fixes execution of PowerShell scripts on Windows for Claude Code by using powershell.exe with specific flags and adds comprehensive tests.

  • Behavior:
    • Detects .ps1 scripts on Windows and executes them via powershell.exe with -NoProfile, -ExecutionPolicy Bypass, and -File flags in run.ts.
    • Maintains compatibility for non-.ps1 executables.
    • Handles PowerShell not found and script not found errors with specific messages.
  • Testing:
    • Adds run-powershell.spec.ts with 10 test cases for PowerShell script execution, error handling, and parameter passing.
    • Tests ensure correct PowerShell invocation and handling of .ps1 and non-.ps1 files on Windows and other platforms.
  • Security:
    • Uses -NoProfile and -ExecutionPolicy Bypass to mitigate security risks.
    • Properly handles file paths to prevent injection attacks.

This description was created by Ellipsis for 3979d90. You can customize this summary. It will automatically update as commits are pushed.

…gration

- Detect .ps1 files on Windows and execute them through PowerShell
- Add proper PowerShell invocation with bypass execution policy
- Improve error messages for PowerShell-specific failures
- Add comprehensive tests for PowerShell script handling

Fixes #7393
@roomote roomote bot requested review from cte, jr and mrubens as code owners August 25, 2025 18:16
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Aug 25, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewing my own code because apparently I trust no one, not even myself.

// Check if this is a PowerShell script error on Windows
const isWindows = os.platform() === "win32"
const isPowerShellScript = isWindows && claudePath.toLowerCase().endsWith(".ps1")
if (isPowerShellScript && error.message?.includes("powershell.exe")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I notice there's duplicate logic here and in lines 75-82 for detecting PowerShell script errors. Could we extract this into a helper function like isPowerShellNotFoundError() to avoid repetition?

if (isPowerShellScript) {
// For PowerShell scripts on Windows, execute through PowerShell
executablePath = "powershell.exe"
args = ["-NoProfile", "-ExecutionPolicy", "Bypass", "-File", claudePath, "-p"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that we're passing the script path directly to PowerShell without any validation? While execa should handle most escaping, could we add a quick check to ensure the path doesn't contain potentially dangerous characters like semicolons or backticks that might be interpreted by PowerShell?

const options = {
systemPrompt: "You are a helpful assistant",
messages: [{ role: "user" as const, content: "Hello" }],
path: "C:\\Program Files\\Claude\\claude.PS1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great test coverage! Would it be worth adding a test case for PowerShell scripts with spaces in the path? This is common on Windows (e.g., "C:\Program Files..."). I see we test uppercase .PS1 but not paths with spaces.


if (isPowerShellScript) {
// For PowerShell scripts on Windows, execute through PowerShell
executablePath = "powershell.exe"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PowerShell Core (pwsh.exe) is becoming more common on Windows. Should we consider trying pwsh.exe as a fallback if powershell.exe is not found? This could improve compatibility with newer Windows installations.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Aug 25, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Aug 26, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Aug 26, 2025
@daniel-lxs daniel-lxs marked this pull request as draft August 26, 2025 14:27
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Draft / In Progress] in Roo Code Roadmap Aug 26, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Sep 16, 2025
@github-project-automation github-project-automation bot moved this from PR [Draft / In Progress] to Done in Roo Code Roadmap Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PR - Draft / In Progress size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Roo code fails to call Claude code on Windows platform

3 participants