Skip to content

feat: add OS-aware command transformation for cross-platform shell support#16573

Closed
jabing wants to merge 8 commits intoanomalyco:devfrom
jabing:feature/windows-shell-support
Closed

feat: add OS-aware command transformation for cross-platform shell support#16573
jabing wants to merge 8 commits intoanomalyco:devfrom
jabing:feature/windows-shell-support

Conversation

@jabing
Copy link

@jabing jabing commented Mar 8, 2026

Issue for this PR

Closes #
Context: OpenCode currently only supports Unix-style bash commands. When AI agents try to run commands like export VAR=value or ls -la on Windows, they fail because these commands are not available in PowerShell or CMD.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Problem: OpenCode AI agents generate Unix-style commands (like export, ls, rm), but these fail on Windows PowerShell/CMD, making the tool unusable for Windows users.
Solution: Add OS-aware command transformation that automatically converts Unix commands to PowerShell/CMD equivalents when running on Windows.
Changes made:

  1. New file: packages/opencode/src/shell/command-transformer.ts (319 lines)
    • CommandTransformer class with pattern-based command mapping
    • getShellType(): Detects shell type (bash, PowerShell, CMD)
    • transformCommand(): Converts 20+ Unix commands
    • Supports: export, ls, rm, mkdir, touch, cat, cp, mv, grep, which, pwd, echo, seq
  2. Modified: packages/opencode/src/shell/shell.ts
    • Added PowerShell fallback for Windows shell detection
    • Exported transformCommand() function
  3. Modified: packages/opencode/src/tool/bash.ts
    • Integrated command transformation before spawning shell processes
    • Calls Shell.transformCommand(params.command, shell) on line 172
  4. Tests: Updated packages/opencode/test/tool/bash.test.ts
    • Added missing beforeAll/afterAll imports (fixes typecheck error)
    • Skipped Unix-specific tests on Windows (using seq command)
  5. Fixed typecheck errors:
    • Removed invalid custom-elements.d.ts in enterprise package
    • Removed invalid custom-elements.d.ts in app package
      How it works:
      On Windows:
  6. When bash tool receives a command like export VAR=value
  7. transformCommand() detects the shell (PowerShell/CMD)
  8. Converts to $env:VAR=value (PowerShell) or set VAR=value (CMD)
  9. Command executes successfully on Windows
    On Unix/Linux/macOS:
  • Commands pass through unchanged (no transformation)

How did you verify your code works?

Unit tests pass:

  • ✅ All unit tests pass on Linux
  • ✅ All unit tests pass on Windows
  • ✅ Typecheck passes for all packages
  • bun turbo test --filter=opencode succeeds
    Manually tested commands:
    Windows PowerShell:
# Before: export CI=true DEBIAN_FRONTEND=noninteractive
# After:  $env:CI=true; $env:DEBIAN_FRONTEND=noninteractive
# Before: ls -la
# After:  Get-ChildItem -la
# Before: rm -rf dir/
# After:  Remove-Item -Recurse -Force dir/
# Before: mkdir -p dir/
# After:  New-Item -ItemType Directory -Force dir/
Windows CMD:
# Before: ls -la
# After:  dir -la
# Before: rm -rf dir/
# After:  rmdir /s /q dir/
# Before: cp src dst
# After:  copy src dst
Unix/Linux/macOS:
# Commands unchanged - pass through as-is
export CI=true DEBIAN_FRONTEND=noninteractive  # Works
ls -la                                            # Works
rm -rf dir/                                        # Works
Tested on:
- Windows 10 + PowerShell 5.1
- Windows 10 + Git Bash
- Ubuntu 20.04 + bash
Screenshots / recordings
N/A - Backend change only, no UI modifications.
Checklist
- [x] I have tested my changes locally
- [x] I have not included unrelated changes in this PR

@jabing jabing requested a review from adamdotdevin as a code owner March 8, 2026 07:17
@github-actions
Copy link
Contributor

github-actions bot commented Mar 8, 2026

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions github-actions bot added the needs:compliance This means the issue will auto-close after 2 hours. label Mar 8, 2026
@jabing jabing changed the title feat(opencode): add OS-aware command transformation for cross-platform shell support feat: add OS-aware command transformation for cross-platform shell support Mar 8, 2026
@jabing jabing marked this pull request as draft March 8, 2026 07:48
@github-actions
Copy link
Contributor

github-actions bot commented Mar 8, 2026

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@github-actions github-actions bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Mar 8, 2026
@github-actions github-actions bot closed this Mar 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant