Skip to content

[Windows] Fix run_command tool ENOENT error when PATH is corrupted #60

@rbctmz

Description

@rbctmz

Description

The run_command tool fails on Windows with error spawn powershell.exe ENOENT when the system PATH environment variable is corrupted or incomplete, preventing the agent from executing shell commands.

Environment

  • OS: Windows 10/11
  • Node.js: v22.14.0
  • Electron: (bundled version)
  • Affected Tool: run_command (bash-tool.ts)

Root Cause

The tool uses child_process.exec() with shell: 'powershell.exe', which relies on the system PATH to locate PowerShell. When:

  1. PATH is corrupted or empty
  2. Electron doesn't inherit system PATH properly
  3. Application runs in restricted environment

The error occurs: Error: spawn powershell.exe ENOENT

Steps to Reproduce

  1. Corrupt or clear PATH environment variable
  2. Start LocalDesk application
  3. Ask agent to execute any command (e.g., "list files in current directory")
  4. Agent calls run_command tool
  5. Error: spawn powershell.exe ENOENT

Current Code (bash-tool.ts)

const { stdout, stderr } = await execAsync(finalCommand, { 
  cwd: context.cwd, 
  maxBuffer: 10 * 1024 * 1024,
  shell: isWindows ? 'powershell.exe' : undefined,  // ❌ Fails if not in PATH
  windowsHide: true,
  encoding: 'utf8',
  env: { ...process.env, PYTHONIOENCODING: 'utf-8' }
});

Proposed Solution

Use full path to PowerShell by leveraging the SystemRoot environment variable, which is more reliable than PATH:

// Fix for Windows: use full path to PowerShell if needed
const windowsShell = isWindows 
  ? process.env.SystemRoot 
    ? `${process.env.SystemRoot}\\System32\\WindowsPowerShell\\v1.0\\powershell.exe`
    : 'powershell.exe'  // Fallback to PATH if SystemRoot unavailable
  : undefined;

const { stdout, stderr } = await execAsync(finalCommand, { 
  cwd: context.cwd, 
  maxBuffer: 10 * 1024 * 1024,
  shell: windowsShell,  // ✅ Uses full path
  windowsHide: true,
  encoding: 'utf8',
  env: { ...process.env, PYTHONIOENCODING: 'utf-8' }
});

Benefits

Reliability: Works even with corrupted PATH
Compatibility: SystemRoot is a standard Windows environment variable (usually C:\Windows)
Fallback: If SystemRoot is unavailable, falls back to original behavior
No breaking changes: Maintains compatibility with existing installations

Alternative Approaches Considered

1. Use pwsh.exe (PowerShell Core)

shell: isWindows ? 'pwsh.exe' : undefined

Rejected: Requires PowerShell 7+ installation, not available on all systems

2. Use cmd.exe instead

shell: isWindows ? 'cmd.exe' : undefined

Rejected: cmd.exe has different syntax and encoding issues, would break existing commands

3. Search multiple locations

const powershellPaths = [
  'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe',
  'C:\\Windows\\SysWOW64\\WindowsPowerShell\\v1.0\\powershell.exe',
  'powershell.exe'
];

Rejected: Overcomplicated, SystemRoot approach is cleaner

Testing

Tested scenarios:

  • ✅ Normal Windows installation with intact PATH
  • ✅ Windows with corrupted PATH
  • ✅ Electron launched without inheriting system environment
  • ✅ Restricted user environment

All scenarios work correctly with the proposed fix.

Files to Modify

  • src/electron/libs/tools/bash-tool.ts (lines 47-49)

Implementation

The fix has been implemented and tested. Ready for PR or direct commit.

Related Issues

  • Windows environment variables reliability
  • Electron environment inheritance on Windows
  • Cross-platform shell command execution

Additional Notes

This issue is Windows-specific. macOS and Linux use default shell resolution and are not affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions