|
| 1 | +# Terminal Shell Integration Guide |
| 2 | + |
| 3 | +## What is Shell Integration? |
| 4 | + |
| 5 | +Shell integration is a [new feature in VSCode 1.93](https://code.visualstudio.com/updates/v1_93#_terminal-shell-integration-api) that allows extensions like Roo Code to run commands in your terminal and read their output. Command output allows Roo Code to react to the result of the command on its own, without you having to handhold by copy-pasting the output in yourself. It's also quite powerful when running development servers as it allows Roo Code to fix errors as the server logs them. |
| 6 | + |
| 7 | +## How Shell Integration Works |
| 8 | + |
| 9 | +Shell integration uses special terminal sequences (like ANSI escape codes) to mark different stages of command execution in your terminal. Here's how it works: |
| 10 | + |
| 11 | +1. **Activation**: When you open a terminal in VSCode, your shell sends an activation sequence (`\x1b]633;A\x07`) to initialize shell integration. |
| 12 | + |
| 13 | +2. **Command Lifecycle**: VSCode shell integration uses OSC (Operating System Command) sequence 633 to track each command's lifecycle: |
| 14 | + - OSC 633 ; A - Mark prompt start |
| 15 | + - OSC 633 ; B - Mark command start |
| 16 | + - OSC 633 ; C - Mark command executed (pre-output) |
| 17 | + - OSC 633 ; D [; `<exitcode>`] - Mark command finished with optional exit code |
| 18 | + - OSC 633 ; E ; `<commandline>` [; `<nonce>`] - Explicitly set command line with optional nonce |
| 19 | + - OSC 633 ; P ; `<Property>=<Value>` - Set properties like current working directory |
| 20 | + |
| 21 | +3. **Shell Integration Hooks**: Each shell uses specific hooks for different stages of command execution: |
| 22 | + |
| 23 | + - Bash: |
| 24 | + * Initial activation: `PROMPT_COMMAND` (sets up initial environment) |
| 25 | + * Command start: `trap DEBUG` (triggers before command execution) |
| 26 | + * Command stop: `PROMPT_COMMAND` (triggers after command completion) |
| 27 | + |
| 28 | + - Zsh: |
| 29 | + * Initial activation: `precmd` (sets up initial environment) |
| 30 | + * Command start: `preexec` (triggers before command execution) |
| 31 | + * Command stop: `precmd` (triggers after command completion) |
| 32 | + |
| 33 | + - PowerShell: |
| 34 | + * Initial activation: `prompt` function (sets up initial environment) |
| 35 | + * Command input: `PSConsoleHostReadLine` (captures command input) |
| 36 | + * Command start/stop: `prompt` function (handles both command start and completion) |
| 37 | + |
| 38 | + - Fish: |
| 39 | + * Initial activation: `fish_prompt` (sets up initial environment) |
| 40 | + * Command start: `fish_preexec` (triggers before command execution) |
| 41 | + * Command stop: `fish_prompt` (triggers after command completion) |
| 42 | + |
| 43 | +> **Note:** This hook implementation information was discovered through AI inspection of the VSCode source tree. If you find any inaccuracies, please submit a pull request with corrections. |
| 44 | +
|
| 45 | +These hooks emit OSC (Operating System Command) sequences that VSCode recognizes to track: |
| 46 | + - Prompt start/end positions |
| 47 | + - Command start/execution/finish events |
| 48 | + - Current working directory changes |
| 49 | + - Command line content and execution status |
| 50 | + |
| 51 | +4. **Output Capture**: When a command runs, VSCode captures: |
| 52 | + - Command text and working directory |
| 53 | + - Start and end times |
| 54 | + - Exit codes |
| 55 | + - Output streams (stdout/stderr) |
| 56 | + |
| 57 | +This allows VSCode and extensions like Roo Code to: |
| 58 | +- Know exactly when commands start and finish |
| 59 | +- Capture command output reliably |
| 60 | +- Track command success/failure |
| 61 | +- Update the terminal UI with command status |
| 62 | +- React to command results automatically |
| 63 | +- Track the current working directory |
| 64 | + |
| 65 | +## How to Fix "Shell Integration Unavailable" |
| 66 | + |
| 67 | +### Step 1: Update VSCode or Cursor |
| 68 | + |
| 69 | +First, make sure you're using the latest version of VSCode or Cursor: |
| 70 | + |
| 71 | +1. Open VSCode or Cursor |
| 72 | +2. Press `Cmd + Shift + P` (Mac) or `Ctrl + Shift + P` (Windows/Linux) |
| 73 | +3. Type "Check for Updates" (VSCode) or "Attempt Update" (Cursor) and select it |
| 74 | +4. Restart VSCode/Cursor after the update |
| 75 | + |
| 76 | +### Step 2: Configure VSCode to Use the Correct Shell |
| 77 | + |
| 78 | +1. Open VSCode |
| 79 | +2. Press `Cmd + Shift + P` (Mac) or `Ctrl + Shift + P` (Windows/Linux) |
| 80 | +3. Type "Terminal: Select Default Profile" and choose it |
| 81 | +4. Select one of the supported shells: zsh, bash, fish, or PowerShell. |
| 82 | + |
| 83 | +### Step 3: Restart VSCode |
| 84 | + |
| 85 | +After making these changes: |
| 86 | + |
| 87 | +1. Quit VSCode completely |
| 88 | +2. Reopen VSCode |
| 89 | +3. Start a new Roo Code session (you can resume your previous task and try to run the command again, this time with Roo Code being able to see the output) |
| 90 | + |
| 91 | +## Additional Troubleshooting for Windows Users |
| 92 | + |
| 93 | +If you're using Windows and still experiencing issues with shell integration after trying the previous steps, it's recommended you use Git Bash. |
| 94 | + |
| 95 | +### Git Bash |
| 96 | + |
| 97 | +Git Bash is a terminal emulator that provides a Unix-like command line experience on Windows. To use Git Bash, you need to: |
| 98 | + |
| 99 | +1. Download and run the Git for Windows installer from [https://git-scm.com/downloads/win](https://git-scm.com/downloads/win) |
| 100 | +2. Quit and re-open VSCode |
| 101 | +3. Press `Ctrl + Shift + P` to open the Command Palette |
| 102 | +4. Type "Terminal: Select Default Profile" and choose it |
| 103 | +5. Select "Git Bash" |
| 104 | + |
| 105 | +### PowerShell |
| 106 | + |
| 107 | +If you'd still like to use PowerShell, make sure you're using an updated version (at least v7+). |
| 108 | + - Check your current PowerShell version by running: `$PSVersionTable.PSVersion` |
| 109 | + - If your version is below 7, [update PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/whats-new/migrating-from-windows-powershell-51-to-powershell-7?view=powershell-7.4#installing-powershell-7). |
| 110 | + |
| 111 | +#### Understanding PowerShell Execution Policies |
| 112 | + |
| 113 | +PowerShell uses execution policies to determine which scripts can run on your system. Here are the most common policies: |
| 114 | + |
| 115 | +- `Restricted`: No PowerShell scripts can run. This is the default setting. |
| 116 | +- `AllSigned`: All scripts, including local ones, must be signed by a trusted publisher. |
| 117 | +- `RemoteSigned`: Scripts created locally can run, but scripts downloaded from the internet must be signed. |
| 118 | +- `Unrestricted`: No restrictions. Any script can run, though you will be warned before running internet-downloaded scripts. |
| 119 | + |
| 120 | +For development work in VSCode, the `RemoteSigned` policy is generally recommended. It allows locally created scripts to run without restrictions while maintaining security for downloaded scripts. To learn more about PowerShell execution policies and understand the security implications of changing them, visit Microsoft's documentation: [About Execution Policies](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_execution_policies). |
| 121 | + |
| 122 | +#### Steps to Change the Execution Policy |
| 123 | + |
| 124 | +1. Open PowerShell as an Administrator: Press `Win + X` and select "Windows PowerShell (Administrator)" or "Windows Terminal (Administrator)". |
| 125 | + |
| 126 | +2. Check Current Execution Policy by running this command: |
| 127 | + ```powershell |
| 128 | + Get-ExecutionPolicy |
| 129 | + ``` |
| 130 | + - If the output is already `RemoteSigned`, `Unrestricted`, or `Bypass`, you likely don't need to change your execution policy. These policies should allow shell integration to work. |
| 131 | + - If the output is `Restricted` or `AllSigned`, you may need to change your policy to enable shell integration. |
| 132 | +
|
| 133 | +3. Change the Execution Policy by running the following command: |
| 134 | + ```powershell |
| 135 | + Set-ExecutionPolicy RemoteSigned -Scope CurrentUser |
| 136 | + ``` |
| 137 | + - This sets the policy to `RemoteSigned` for the current user only, which is safer than changing it system-wide. |
| 138 | +
|
| 139 | +4. Confirm the Change by typing `Y` and pressing Enter when prompted. |
| 140 | +
|
| 141 | +5. Verify the Policy Change by running `Get-ExecutionPolicy` again to confirm the new setting. |
| 142 | +
|
| 143 | +6. Restart VSCode and try the shell integration again. |
| 144 | +
|
| 145 | +## Manual Shell Integration Installation |
| 146 | +
|
| 147 | +If you're still experiencing issues after trying the basic troubleshooting steps, you can try manually installing shell integration: |
| 148 | +
|
| 149 | +### Installation Instructions |
| 150 | +
|
| 151 | +For Bash users: |
| 152 | +1. Run `code ~/.bashrc` in the terminal to open your bash configuration file. |
| 153 | +2. Add the following line to your `~/.bashrc`: |
| 154 | +```bash |
| 155 | +[[ "$TERM_PROGRAM" == "vscode" ]] && . "$(code --locate-shell-integration-path bash)" |
| 156 | +``` |
| 157 | + |
| 158 | +For Zsh users: |
| 159 | +1. Run `code ~/.zshrc` in the terminal to open your zsh configuration file. |
| 160 | +2. Add the following line to your `~/.zshrc`: |
| 161 | +```bash |
| 162 | +[[ "$TERM_PROGRAM" == "vscode" ]] && . "$(code --locate-shell-integration-path zsh)" |
| 163 | +``` |
| 164 | + |
| 165 | +For PowerShell users: |
| 166 | +1. Run `code $Profile` in the terminal to open your PowerShell profile. |
| 167 | +2. Add the following line to the file: |
| 168 | +```powershell |
| 169 | +if ($env:TERM_PROGRAM -eq "vscode") { . "$(code --locate-shell-integration-path pwsh)" } |
| 170 | +``` |
| 171 | + |
| 172 | +For Fish users: |
| 173 | +1. Run `code ~/.config/fish/config.fish` in the terminal to open your fish configuration file. |
| 174 | +2. Add the following line to your `~/.config/fish/config.fish`: |
| 175 | +```fish |
| 176 | +string match -q "$TERM_PROGRAM" "vscode"; and . (code --locate-shell-integration-path fish) |
| 177 | +``` |
| 178 | + |
| 179 | +### About the Shell Integration Path Command |
| 180 | + |
| 181 | +The `code --locate-shell-integration-path` command returns the path to shell-specific integration scripts. It supports four shell types: |
| 182 | +- bash - returns path to `shellIntegration-bash.sh` |
| 183 | +- zsh - returns path to `shellIntegration-rc.zsh` |
| 184 | +- pwsh - returns path to `shellIntegration.ps1` |
| 185 | +- fish - returns path to `shellIntegration.fish` |
| 186 | + |
| 187 | +Any other value will throw an error: "Error using `--locate-shell-integration-path`: Invalid shell type". |
| 188 | + |
| 189 | +## Unusual Terminal Output |
| 190 | + |
| 191 | +If you're seeing unusual output with rectangles, lines, escape sequences, or control characters, it may be related to terminal customization tools. Common culprits include: |
| 192 | + |
| 193 | +- Powerlevel10k: A zsh theme that adds visual elements to the prompt. If using Powerlevel10k, you must set `typeset -g POWERLEVEL9K_TERM_SHELL_INTEGRATION=true` in your `~/.zshrc` before sourcing the theme. This enables proper integration with VSCode's shell integration, which differs from the integration provided by VSCE. |
| 194 | +- Oh My Zsh: A framework for managing zsh configurations |
| 195 | +- Fish shell themes |
| 196 | + |
| 197 | +To troubleshoot: |
| 198 | + |
| 199 | +1. For Powerlevel10k users: |
| 200 | + ```bash |
| 201 | + # Add this line before sourcing powerlevel10k in ~/.zshrc |
| 202 | + typeset -g POWERLEVEL9K_TERM_SHELL_INTEGRATION=true |
| 203 | + ``` |
| 204 | + |
| 205 | +2. If issues persist, temporarily disable these tools in your shell configuration file (e.g., `~/.zshrc` for Zsh) |
| 206 | +3. If the issue resolves, gradually re-enable features to identify the conflicting component |
| 207 | + |
| 208 | +## Advanced Troubleshooting |
| 209 | + |
| 210 | +### Shell Integration Not Available |
| 211 | + |
| 212 | +Shell integration activation sequence (`\x1b]633;A\x07`) was not received. Shell integration features are disabled. Verify you are using a supported shell (Bash, Zsh, PowerShell, Fish) and shell integration is enabled in settings. |
| 213 | + |
| 214 | +### Command Stream Not Available |
| 215 | + |
| 216 | +Command stream failed: missing start sequence (`\x1b]633;C\x07`) or command already ended (`\x1b]633;D\x07`). Command output cannot be captured. Check if the command started correctly or has completed execution. |
| 217 | + |
| 218 | +### Verifying Shell Integration Status |
| 219 | + |
| 220 | +Run these commands to verify shell integration sequences are installed: |
| 221 | + |
| 222 | +#### Bash |
| 223 | +```bash |
| 224 | +# View all shell integration sequences |
| 225 | +set | grep -i '[16]33;' |
| 226 | + |
| 227 | +# Should show VSCode shell integration functions like: |
| 228 | +# printf '\e]633;E;%s;%s\a' - Command line set |
| 229 | +# printf '\e]633;C\a' - Command executed |
| 230 | +# printf '\e]633;D\a' - Command finished |
| 231 | +# printf '\e]633;B\a' - Command start |
| 232 | +# printf '\e]633;A\a' - Shell integration activated |
| 233 | +# printf '\e]633;P;Prompt=%s\a' - Prompt marker |
| 234 | + |
| 235 | +# Check for PROMPT_COMMAND and DEBUG trap |
| 236 | +echo "$PROMPT_COMMAND" | grep vsc |
| 237 | +trap -p DEBUG | grep vsc |
| 238 | +``` |
| 239 | + |
| 240 | +#### Zsh |
| 241 | +```zsh |
| 242 | +# List all VSCode shell integration functions |
| 243 | +functions | grep -i vsc # Should show __vsc_precmd, __vsc_preexec, etc. |
| 244 | + |
| 245 | +# Verify shell integration hooks are installed |
| 246 | +typeset -p precmd_functions preexec_functions # Should include __vsc_precmd and __vsc_preexec |
| 247 | + |
| 248 | +# Check Powerlevel10k integration (if installed) |
| 249 | +(( ${+functions[p10k]} )) && typeset -p POWERLEVEL9K_TERM_SHELL_INTEGRATION # Should show "true" |
| 250 | +``` |
| 251 | + |
| 252 | +#### PowerShell |
| 253 | +```powershell |
| 254 | +# View shell integration functions |
| 255 | +Get-Command -Name "*VSC*" -CommandType Function |
| 256 | +
|
| 257 | +# Check prompt function |
| 258 | +Get-Content Function:\Prompt | Select-String "VSCode" |
| 259 | +``` |
| 260 | + |
| 261 | +#### Fish |
| 262 | +```fish |
| 263 | +# Check for shell integration functions |
| 264 | +functions | grep -i vsc |
| 265 | +
|
| 266 | +# Verify fish_prompt and fish_preexec hooks |
| 267 | +functions fish_prompt | grep -i vsc |
| 268 | +functions fish_preexec | grep -i vsc |
| 269 | +``` |
| 270 | + |
| 271 | +Visual indicators of active shell integration: |
| 272 | +1. Shell integration indicator in terminal title bar |
| 273 | +2. Command detection highlighting |
| 274 | +3. Working directory updates in terminal title |
| 275 | +4. Command duration reporting |
| 276 | +5. Exit code reporting for failed commands |
| 277 | + |
| 278 | +## Troubleshooting Terminal Execution Failures |
| 279 | + |
| 280 | +If commands that previously worked suddenly stop working: |
| 281 | +1. Close the current terminal |
| 282 | +2. Let Roo Code open a new terminal for you |
| 283 | +3. Try your command again |
| 284 | + |
| 285 | +This often resolves temporary shell integration issues. However, if you experience persistent terminal execution failures and can reliably reproduce the problem, please [open a ticket](https://github.com/RooVetGit/Roo-Code/issues/new) with: |
| 286 | +- A step-by-step reproduction of the problem |
| 287 | +- Your operating system and VSCode/Cursor version |
| 288 | +- The shell you're using (Bash, Zsh, PowerShell, Fish) |
| 289 | +- Any relevant error messages or terminal output |
| 290 | + |
| 291 | +This will help us investigate and fix the underlying issue. |
| 292 | + |
| 293 | +## Known Issues |
| 294 | + |
| 295 | +### Incomplete Terminal Output |
| 296 | + |
| 297 | +There is an ongoing issue in VSCode where terminal command output may not be completely captured by extensions like Roo Code. This can result in: |
| 298 | +- Missing portions of command output |
| 299 | +- Incomplete error messages |
| 300 | +- Delayed or missing command completion signals |
| 301 | + |
| 302 | +If you experience this issue, try: |
| 303 | +1. Closing and reopening the terminal |
| 304 | +2. Running the command again |
| 305 | +3. If the problem persists, you may need to manually copy-paste relevant output to Roo Code |
| 306 | + |
| 307 | +For detailed troubleshooting guidance, refer to the [Cline Wiki - Troubleshooting Shell Integration](https://github.com/cline/cline/wiki/Troubleshooting-%E2%80%90-Shell-Integration-Unavailable) guide. |
| 308 | + |
| 309 | +## Troubleshooting Resources |
| 310 | + |
| 311 | +- [VSCode Terminal Output Issue #237208](https://github.com/microsoft/vscode/issues/237208): Tracking the incomplete terminal output capture issue (ongoing as of March 8, 2025) |
| 312 | +- [VSCode Terminal Integration Test Repository](https://github.com/KJ7LNW/vsce-test-terminal-integration): A tool for validating shell integration functionality in your environment |
| 313 | +- [Cline Wiki - Troubleshooting Shell Integration](https://github.com/cline/cline/wiki/Troubleshooting-%E2%80%90-Shell-Integration-Unavailable): Comprehensive troubleshooting guide by Cline's original creator |
| 314 | +- [Shell Integration Improvements PR](https://github.com/cline/cline/pull/1089): Enhanced shell integration behavior (merged in Roo Code as of March 8, 2025, pending merge in Cline) |
| 315 | +- [Roo Code Shell Integration Architecture PR](https://github.com/RooVetGit/Roo-Code/pull/1365): Detailed discussion and proposed architectural changes for more reliable shell integration (pending as of March 8, 2025) |
| 316 | + |
| 317 | +## Support |
| 318 | + |
| 319 | +If you've followed these steps and are still experiencing problems, please: |
| 320 | + |
| 321 | +1. Check the [Roo Code GitHub Issues](https://github.com/RooVetGit/Roo-Code/issues) to see if others have reported similar problems |
| 322 | +2. If not, create a new issue with details about your operating system, VSCode/Cursor version, and the steps you've tried |
| 323 | + |
| 324 | +For additional help, join our [Discord](https://discord.com/channels/1332146336664915968/1332212137568899162). |
| 325 | + |
| 326 | +_Content adapted from [Cline Wiki - Troubleshooting Shell Integration Unavailable](https://github.com/cline/cline/wiki/Troubleshooting-%E2%80%90-Shell-Integration-Unavailable)_ |
0 commit comments