4.1 KiB
Troubleshooting VSCode CLI
Common issues, error messages, and solutions.
Command Not Found
Error: bash: code: command not found
Cause: VSCode CLI not installed in PATH
Solution:
- Open VSCode
- Open Command Palette (Ctrl+Shift+P / Cmd+Shift+P)
- Run: "Shell Command: Install 'code' command in PATH"
- Restart terminal
Verify installation:
which code
# Should output: /usr/local/bin/code (or similar)
Wrong Line/Column Position
Problem: Cursor not at expected position
Causes:
- Line numbers are 1-indexed (line 1 is first line, not 0)
- Column numbers are 0-indexed (column 0 is first character)
- Verify your line/column counting
Examples:
# First line, first character
code -g file.js:1:0 # ✓ Correct
code -g file.js:0:0 # ❌ Goes to line 1 anyway
# Fifth line, eleventh character
code -g file.js:5:10 # ✓ Correct
code -g file.js:5:11 # ❌ Off by one
File Opens But Position Ignored
Problem: File opens but cursor is at wrong location
Possible Causes:
- Syntax Error:
code -g file.js 42 # ❌ Missing colon
code -g file.js:42 # ✓ Correct
- File Doesn't Exist:
# Check file exists first
[[ -f "file.js" ]] && code -g file.js:42
- Line/Column Out of Bounds:
# If file has 50 lines and you specify line 100,
# cursor moves to last line instead
code -g file.js:100 # Goes to line 50 (last line)
Path Not Found
Problem: Error: Unable to resolve nonexistent file
Causes:
- Relative Path from Wrong Directory:
# Current dir: /home/user/
code -g project/src/app.js:42 # ✓ Works if project/src/app.js exists
# Current dir: /home/user/other/
code -g project/src/app.js:42 # ❌ Fails - wrong directory
- Spaces Not Quoted:
code -g my project/file.js:10 # ❌ Fails
code -g "my project/file.js:10" # ✓ Works
Solution: Use absolute paths
ABS_PATH="$(cd "$(dirname "$FILE")" && pwd)/$(basename "$FILE")"
code -g "$ABS_PATH:42"
Multiple Windows Opening
Problem: Too many VSCode windows opening unexpectedly
Cause: Using -n flag repeatedly
Solutions:
# Use -r to reuse existing window
code -r -g file.js:10
# Or omit -n flag (reuses window by default)
code -g file.js:10
Column Position Beyond Line Length
Problem: Specified column exceeds actual line length
Behavior: Cursor moves to end of line instead
Example:
# Line 10 has 30 characters
code -g file.js:10:50 # Goes to column 30 (end of line)
Not an Error: This is expected behavior, VSCode handles it gracefully
File Opens in Wrong VSCode Instance
Problem: File opens in unexpected VSCode window
Solutions:
- Force New Window:
code -n -g file.js:10
- Specify Profile:
code --profile my-profile -g file.js:10
- Use Specific Data Directory:
code --user-data-dir ~/.vscode-work -g file.js:10
Asynchronous Opening Issues
Problem: Script continues before file is open
Behavior: code command returns immediately, file may not be fully opened
Solutions:
- Use
-wflag to wait:
code -w -g file.js:10
# Script waits until user closes file
- Add delay if needed:
code -g file.js:10
sleep 1 # Give VSCode time to open
Permission Denied
Problem: Error: EACCES: permission denied
Causes:
- File doesn't have read permissions
- Directory doesn't have execute permissions
Solutions:
# Check file permissions
ls -l file.js
# Fix if needed
chmod 644 file.js # Read/write for owner, read for others
chmod 755 directory/ # Execute permission for directory
VSCode Opens But Shows Error
Problem: VSCode opens but displays error about file
Common Errors:
- "Unable to open file" - File path is incorrect
- "File is a directory" - Path points to directory, not file
- "Binary file" - File is not a text file
Validation Before Opening:
if [[ -f "$FILE_PATH" ]]; then
code -g "$FILE_PATH:$LINE"
else
echo "Error: Not a file: $FILE_PATH"
fi