[PR #7759] fix(tui): handle quotes in VISUAL/EDITOR env vars #12504

Open
opened 2026-02-16 18:17:24 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/7759

State: open
Merged: No


Fixes #7749

I noticed this issue was assigned but didn't active PR, so I went ahead and fixed it. Happy to collaborate with @assignee if they're also working on this.

What does this PR do?

Fixes the /editor command to correctly handle VISUAL or EDITOR environment variables that contain quoted arguments.

The Problem

When VISUAL contains arguments with quotes like:
```bash
export VISUAL="nvim --cmd 'let g:flatten_wait=1'"
```

The current implementation splits on spaces (`editor.split(" ")`), breaking the quoted argument:

  • Expected: `nvim --cmd "let g:flatten_wait=1"`
  • Actual: `nvim --cmd 'let g:flatten_wait=1'` (parsed as 4 separate args)

This causes nvim to fail with: `E20: Mark not set: 'let`

The Solution

Use shell execution (`sh -c` on Unix, `cmd /c` on Windows) to properly parse quotes and arguments, just like git, ranger, and other CLI tools handle editor invocation.

Also added filepath escaping to prevent command injection attacks.

How did you verify your code works?

  1. Unit tests: Added comprehensive tests in `test/util/editor.test.ts`

    • All 5 tests passing
    • 100% coverage on editor.ts
    • Tests cover: simple editor, quoted arguments, multiple arguments, fallback to EDITOR, no editor set
  2. Type checking: Pre-commit hooks passed (turbo typecheck)

Changes

  • Modified `src/cli/cmd/tui/util/editor.ts` to use shell execution
  • Added filepath escaping for security (`'${filepath.replace(/'/g, "'\''")}`)
  • Added comprehensive unit tests in `test/util/editor.test.ts`
**Original Pull Request:** https://github.com/anomalyco/opencode/pull/7759 **State:** open **Merged:** No --- Fixes #7749 I noticed this issue was assigned but didn't active PR, so I went ahead and fixed it. Happy to collaborate with @assignee if they're also working on this. ## What does this PR do? Fixes the `/editor` command to correctly handle `VISUAL` or `EDITOR` environment variables that contain quoted arguments. ## The Problem When `VISUAL` contains arguments with quotes like: \`\`\`bash export VISUAL="nvim --cmd 'let g:flatten_wait=1'" \`\`\` The current implementation splits on spaces (\`editor.split(" ")\`), breaking the quoted argument: - Expected: \`nvim --cmd "let g:flatten_wait=1"\` - Actual: \`nvim --cmd 'let g:flatten_wait=1'\` (parsed as 4 separate args) This causes nvim to fail with: \`E20: Mark not set: 'let\` ## The Solution Use shell execution (\`sh -c\` on Unix, \`cmd /c\` on Windows) to properly parse quotes and arguments, just like git, ranger, and other CLI tools handle editor invocation. Also added filepath escaping to prevent command injection attacks. ## How did you verify your code works? 1. **Unit tests:** Added comprehensive tests in \`test/util/editor.test.ts\` - All 5 tests passing - 100% coverage on editor.ts - Tests cover: simple editor, quoted arguments, multiple arguments, fallback to EDITOR, no editor set 2. **Type checking:** Pre-commit hooks passed (turbo typecheck) ## Changes - Modified \`src/cli/cmd/tui/util/editor.ts\` to use shell execution - Added filepath escaping for security (\`'${filepath.replace(/'/g, "'\\''")}\`) - Added comprehensive unit tests in \`test/util/editor.test.ts\`
yindo added the pull-request label 2026-02-16 18:17:24 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#12504