From c7ca1a51e94c2759bf146ebd878e02780a00b7c7 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 08:02:53 +0000 Subject: [PATCH 1/2] fix(cwd): stop passing buffer- and git-derived paths to vim.fn.expand `normalize_dir` in cwd.lua and `build_config` in terminal.lua passed any cwd text that was not an existing directory to `vim.fn.expand`. On Unix, expand runs text between backticks through 'shell'. With git_repo_cwd or a cwd_provider that returns ctx.file_dir, the directory part of a terminal, archive-member or URI buffer name (and the root git prints) reached that call, so a folder name holding a backtick pair ran a command when the user opened or toggled Claude. Add `cwd.expand_leading`, which expands only a leading `~`, `~/`, `$VAR` or `${VAR}` from the environment, without a shell, and use it in both places. The rest of the text is taken as written; normalize_dir still returns nil for text that names no directory, and build_config still passes a non-directory cwd through literally. `$VAR` later in the text, `~user` and globs are no longer expanded. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015koAoMEFEUXUgoDF7Nam12 --- README.md | 6 +-- lua/claudecode/README.md | 4 +- lua/claudecode/cwd.lua | 41 +++++++++++---- lua/claudecode/terminal.lua | 12 ++--- .../integration/cwd_path_characters_spec.lua | 4 +- tests/integration/terminal_cwd_spec.lua | 6 +-- tests/unit/command_args_spec.lua | 51 +++++++++++++++++++ tests/unit/cwd_spec.lua | 48 +++++++++++++++++ 8 files changed, 143 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 60c0b8d9b..508cc4bc4 100644 --- a/README.md +++ b/README.md @@ -460,9 +460,9 @@ You can fix the Claude terminal's working directory regardless of `autochdir` an - `cwd`: static path to use as working directory. - `git_repo_cwd = true`: resolves git root from the current file directory (or cwd if no file). -A `cwd_provider` result or a `cwd` that starts with `~` or `$` always goes through `vim.fn.expand`, so `~/projects/my-app` -and `$HOME/my-app` work. Any other text that names an existing folder is used as written, so a folder name with `$`, `\`, -`[` or `{` in it works. Other text goes through `expand` too. +A leading `~`, `~/`, `$VAR` or `${VAR}` in a `cwd_provider` result or a `cwd` is expanded, so `~/projects/my-app` and +`$HOME/my-app` work. The rest of the text is used as written, so a folder name with `$`, `\`, `[`, `{` or a backtick in it +works. The plugin never passes these paths to `vim.fn.expand`, which would run text between backticks as a shell command. Examples: diff --git a/lua/claudecode/README.md b/lua/claudecode/README.md index e8dd9e72f..067fd204e 100644 --- a/lua/claudecode/README.md +++ b/lua/claudecode/README.md @@ -127,8 +127,8 @@ Module map (direct files): - `terminal.setup` warns about its options through `vim.notify` directly, not through the logger. So its warnings show at once, and `log_level` does not filter them. It warns on a terminal config that is not a table, an invalid `terminal_cmd` or `env`, an invalid value for a known key, and an unknown key [terminal.lua:M.setup] - A per-call `opts_override` changes only a key that has a validator (`split_side`, `split_width_percentage`, `snacks_win_opts`, `cwd`, `git_repo_cwd`, `cwd_provider`) and a non-nil current value. So a per-call `cwd` or `cwd_provider` is ignored unless `setup` set one. Providers get only `split_side`, `split_width_percentage`, `auto_close`, `auto_insert`, `snacks_win_opts`, and the resolved `cwd` [terminal.lua:build_config] - The Claude command is `terminal_cmd`, or `claude` when it is unset. `no_proxy` and `NO_PROXY` always include `localhost`, `127.0.0.1`, and `::1` [terminal.lua:get_claude_command_and_env] -- Terminal cwd precedence: `cwd_provider`, then static `cwd`, then the git root when `git_repo_cwd` is set. A `cwd_provider` result or a static `cwd` that starts with `~` or `$` always goes through `vim.fn.expand`, so `~` and `$VAR` work even when Neovim's current folder holds a folder with that literal name. Any other text that names an existing folder is used as written, so `$`, `\`, `[` and `{` in the folder name stay. Other text goes through `expand` too [terminal.lua:build_config] -- `cwd.git_root` and `cwd.resolve` take a directory path as written: a folder name with `[`, `{`, `*`, `?`, `$`, `\`, a space or a comma stays that folder. A string that is not a directory is expanded (`~`, `$VAR`), and a string that starts with `~` or `$` is always expanded first, even when Neovim's current folder holds a folder with that literal name. The terminal does not call `cwd.resolve`, but `terminal.lua:build_config` applies the same as-written rule to a static `cwd` and a `cwd_provider` result, so an existing folder whose text does not start with `~` or `$` keeps `$` or `\` there too. Unlike `normalize_dir`, it passes expanded text that names no folder on to the terminal instead of dropping it. When `git rev-parse` cannot run or finds no repository, `git_root` walks up from `start_dir` made absolute with `:p`, with its `..` parts simplified, to the nearest folder that holds a `.git` folder. A relative `start_dir` that begins with `~` is not made absolute, because Vim reads `~` as the home folder and `~name` as a user's home folder, so the walk starts from there [cwd.lua:M.git_root] [cwd.lua:normalize_dir] [terminal.lua:build_config] +- Terminal cwd precedence: `cwd_provider`, then static `cwd`, then the git root when `git_repo_cwd` is set. A `cwd_provider` result or a static `cwd` goes through `cwd.expand_leading`, which expands only a leading `~`, `~/`, `$VAR` or `${VAR}`, without a shell, so `~` and `$VAR` work even when Neovim's current folder holds a folder with that literal name. The rest of the text is used as written, so `$`, `\`, `[`, `{` and backticks in the folder name stay. No cwd path reaches `vim.fn.expand`, which runs text between backticks as a shell command, and a buffer name (a `term://` or `zipfile://` buffer, say) can put such text in `ctx.file_dir` [terminal.lua:build_config] [cwd.lua:M.expand_leading] +- `cwd.git_root` and `cwd.resolve` take a directory path as written: a folder name with `[`, `{`, `*`, `?`, `$`, `\`, a space or a comma stays that folder. A leading `~`, `~/`, `$VAR` or `${VAR}` is expanded first by `expand_leading`, without a shell, even when Neovim's current folder holds a folder with that literal name, and a string that then names no directory gives nil. Neither the start folder nor the root git prints reaches `vim.fn.expand`. The terminal does not call `cwd.resolve`, but `terminal.lua:build_config` applies the same as-written rule to a static `cwd` and a `cwd_provider` result, so `$` or `\` past the start of the text stays. Unlike `normalize_dir`, it passes text that names no folder on to the terminal instead of dropping it. When `git rev-parse` cannot run or finds no repository, `git_root` walks up from `start_dir` made absolute with `:p`, with its `..` parts simplified, to the nearest folder that holds a `.git` folder. A relative `start_dir` that begins with `~` is not made absolute, because Vim reads `~` as the home folder and `~name` as a user's home folder, so the walk starts from there [cwd.lua:M.git_root] [cwd.lua:normalize_dir] [terminal.lua:build_config] - `send_to_terminal` never starts a terminal: without a valid terminal buffer it returns `false`. With `focus = true` it calls `M.open()` after a successful send, which can show a hidden terminal. It refuses the `none` and `external` providers, because they have no buffer. It turns CRLF and CR into LF and wraps multi-line text in bracketed-paste markers. It returns `false` when the buffer has no job channel or `chansend` fails [terminal.lua:M.send_to_terminal] - `utils.parse_command` does no globbing or variable expansion, so model aliases such as `opus[1m]` stay intact [utils.lua:M.parse_command] diff --git a/lua/claudecode/cwd.lua b/lua/claudecode/cwd.lua index 7451631ef..3599bf577 100644 --- a/lua/claudecode/cwd.lua +++ b/lua/claudecode/cwd.lua @@ -10,6 +10,32 @@ local function is_directory(path) return vim.fn.isdirectory(path) == 1 end +---Expand a leading `~`, `~/`, `$VAR` or `${VAR}` and leave the rest of the text as written. It never calls +---`vim.fn.expand`: that runs the text between backticks as a shell command and reads `[`, `{`, `*`, `?` and `\` as +---patterns, and these paths can come from a buffer name or from git. An unset variable leaves the text unchanged. +---@param path string +---@return string +function M.expand_leading(path) + local name, rest = path:match("^%$([%w_]+)(.*)$") + if not name then + name, rest = path:match("^%${([%w_]+)}(.*)$") + end + if not name and (path == "~" or path:sub(1, 2) == "~/") then + name, rest = "HOME", path:sub(2) + end + if not name then + return path + end + local value = (vim.env and vim.env[name]) or os.getenv(name) + if (value == nil or value == "") and name == "HOME" and vim.loop and vim.loop.os_homedir then + value = vim.loop.os_homedir() + end + if type(value) == "string" and value ~= "" then + return value .. rest + end + return path +end + ---Normalize and validate a directory path ---@param dir string|nil ---@return string|nil @@ -17,18 +43,13 @@ local function normalize_dir(dir) if type(dir) ~= "string" or dir == "" then return nil end - -- A directory that exists is taken as written. `vim.fn.expand` reads `[`, `{`, `*`, `?`, `$` and `\` in a name as - -- patterns or variables, so it would change the path of a folder that has them, or find none. Text that starts with - -- `~` or `$` asks for expansion, as in a shell and in `:cd`, so it goes through `expand` first: `is_directory` does not - -- expand, and a stray folder named `~` in Neovim's current folder must not take the place of the home folder. - if not dir:match("^[~$]") and is_directory(dir) then + -- Text that starts with `~` or `$` asks for expansion, as in a shell and in `:cd`, so it is expanded before the check: + -- `is_directory` does not expand, and a stray folder named `~` in Neovim's current folder must not take the place of + -- the home folder. Any other text is taken as written, and text that names no directory gives nil. + dir = M.expand_leading(dir) + if is_directory(dir) then return dir end - -- Expand ~ and similar - local expanded = vim.fn.expand(dir) - if is_directory(expanded) then - return expanded - end return nil end diff --git a/lua/claudecode/terminal.lua b/lua/claudecode/terminal.lua index 5a0ec4795..6797c4ca8 100644 --- a/lua/claudecode/terminal.lua +++ b/lua/claudecode/terminal.lua @@ -262,15 +262,9 @@ local function build_config(opts_override) requested_cwd = effective_config.cwd end if requested_cwd then - -- A folder that exists is taken as written. `vim.fn.expand` reads `[`, `{`, `*`, `?`, `$` and `\` in a name as - -- patterns or variables, so it changed the path of a folder that has them. Text that names no folder still goes - -- through `expand`, so `~` and `$VAR` work. A leading `~` or `$` always asks for `expand`, as in a shell and in - -- `:cd`, so a stray folder named `~` in Neovim's cwd does not take the place of the home folder. - if not requested_cwd:match("^[~$]") and vim.fn.isdirectory(requested_cwd) == 1 then - resolved_cwd = requested_cwd - else - resolved_cwd = vim.fn.expand(requested_cwd) - end + -- Only a leading `~` or `$VAR` is expanded, without a shell; the rest is taken as written. `vim.fn.expand` would + -- run text between backticks as a shell command, and a `cwd_provider` result can come from the buffer name. + resolved_cwd = require("claudecode.cwd").expand_leading(requested_cwd) end if not resolved_cwd and effective_config.git_repo_cwd then local ok_r, cwd_mod = pcall(require, "claudecode.cwd") diff --git a/tests/integration/cwd_path_characters_spec.lua b/tests/integration/cwd_path_characters_spec.lua index 50885c5a0..2c8aa7c6a 100644 --- a/tests/integration/cwd_path_characters_spec.lua +++ b/tests/integration/cwd_path_characters_spec.lua @@ -195,8 +195,8 @@ describe("the git root of a folder whose name holds unusual characters", functio end) -- The next two blocks test `cwd.resolve`, not the terminal. `terminal.lua:build_config` does not call it: it expands - -- a static `cwd` and a `cwd_provider` result with `vim.fn.expand` itself, so these cases say nothing about what the - -- terminal does with a folder name that holds `$` or a backslash. + -- a static `cwd` and a `cwd_provider` result with `cwd.expand_leading` itself, so these cases say nothing about what + -- the terminal does with a folder name that holds `$` or a backslash. describe("cwd.resolve with git_repo_cwd", function() it("returns the repository of a file folder whose name has a space and a bracket, when git cannot run", function() local repo = repo_in("resolve", "my [repo]", function(path) diff --git a/tests/integration/terminal_cwd_spec.lua b/tests/integration/terminal_cwd_spec.lua index 320d0a627..6d964763e 100644 --- a/tests/integration/terminal_cwd_spec.lua +++ b/tests/integration/terminal_cwd_spec.lua @@ -15,9 +15,9 @@ end -- The terminal starts in the folder that the `cwd_provider` result or the static `cwd` names. `build_config` ran both -- through `vim.fn.expand`, which reads `[`, `{`, `*`, `?`, `$` and `\` in a name as patterns or variables. So a folder -- named `$HOME-like` or `back\slash` came back as another path, and `[x] {y}` and `it's` came back with a trailing --- slash. Text that starts with `~` or `$` now always goes through `vim.fn.expand`, so `~` and `$VAR` work, even when --- Neovim's current folder holds a folder with that literal name. Any other text that names an existing folder is --- taken as written. Other text goes through `expand` too. Neovim's own `expand` and `isdirectory` decide this, so under +-- slash. A leading `~` or `$VAR` is now expanded without `vim.fn.expand` (which also runs text between backticks as a +-- shell command), so `~` and `$VAR` work, even when Neovim's current folder holds a folder with that literal name. The +-- rest of the text is taken as written. Neovim's own `isdirectory` and environment decide this, so under -- busted's mock vim this is pending; scripts/run_integration_tests_individually.sh runs it in Neovim. describe("the terminal working directory", function() local terminal = require("claudecode.terminal") diff --git a/tests/unit/command_args_spec.lua b/tests/unit/command_args_spec.lua index b6b317dab..335385b83 100644 --- a/tests/unit/command_args_spec.lua +++ b/tests/unit/command_args_spec.lua @@ -459,5 +459,56 @@ describe("ClaudeCode command arguments with a mock vim", function() assert.is_table(last.opts, "termopen options missing") assert.are.equal("/from/provider", last.opts.cwd) end) + + -- A terminal buffer's name embeds the folder it started in. `vim.fn.expand` runs text between backticks as a shell + -- command, so a buffer-derived folder that names no directory must not reach it, nor reach git. + local function open_from_terminal_buffer(terminal_opts) + local saved_expand, saved_systemlist = vim.fn.expand, vim.fn.systemlist + local expanded, git_runs = {}, 0 + vim.fn.expand = function(text) + expanded[#expanded + 1] = text + if text == "%:p" then + return "term:///tmp/`touch pwned`//123:/bin/sh" + end + return saved_expand(text) + end + vim.fn.systemlist = function() + git_runs = git_runs + 1 + return {} + end + terminal_opts.provider = "native" + local ok, err = pcall(function() + claudecode.setup({ auto_start = false, terminal = terminal_opts }) + for _, call in ipairs(vim.api.nvim_create_user_command.calls) do + if call.vals[1] == "ClaudeCode" then + call.vals[2]({}) + break + end + end + end) + vim.fn.expand, vim.fn.systemlist = saved_expand, saved_systemlist + assert(ok, err) + for _, text in ipairs(expanded) do + assert.is_nil(text:find("`", 1, true), "vim.fn.expand got " .. text) + end + assert.equals(0, git_runs) + return executed_commands[#executed_commands].opts.cwd + end + + it("never expands a buffer-derived folder with backticks from git_repo_cwd", function() + assert.is_nil(open_from_terminal_buffer({ git_repo_cwd = true })) + end) + + it("never expands a cwd_provider result with backticks", function() + local file_dir + local cwd = open_from_terminal_buffer({ + cwd_provider = function(ctx) + file_dir = ctx.file_dir + return file_dir + end, + }) + assert.is_truthy(file_dir:find("`touch pwned`", 1, true)) + assert.equals(file_dir, cwd) + end) end) end) diff --git a/tests/unit/cwd_spec.lua b/tests/unit/cwd_spec.lua index a288dc19c..0ba63bab5 100644 --- a/tests/unit/cwd_spec.lua +++ b/tests/unit/cwd_spec.lua @@ -87,6 +87,54 @@ describe("The git root lookup", function() assert.equals(0, #git_calls) end) + describe("with text that names no folder and holds backticks", function() + local saved_expand, expanded + + before_each(function() + saved_expand, expanded = vim.fn.expand, {} + -- `vim.fn.expand` runs text between backticks as a shell command, so none of this text may reach it. + vim.fn.expand = function(text) + expanded[#expanded + 1] = text + return text + end + end) + + after_each(function() + vim.fn.expand = saved_expand + end) + + it("does not expand or run git for a start folder from a terminal buffer name", function() + stub_git({ lines = { dir }, exit_code = 0 }) + + assert.is_nil(cwd.git_root("term://" .. dir .. "/`touch pwned`//123:")) + + assert.same({}, expanded) + assert.equals(0, #git_calls) + end) + + it("does not expand a root that git printed and that names no folder", function() + stub_git({ lines = { dir .. "/`touch pwned`" }, exit_code = 0 }) + + local found = cwd.git_root(dir) + + assert.same({}, expanded) + assert.is_true(found == nil or vim.fn.isdirectory(found) == 1, tostring(found)) + assert.is_nil(found and found:find("`", 1, true)) + end) + end) + + it("expands a leading ~, $VAR or ${VAR} and leaves the rest as written", function() + local home = os.getenv("HOME") + local path = os.getenv("PATH") + assert.equals(home, cwd.expand_leading("~")) + assert.equals(home .. "/`id`", cwd.expand_leading("~/`id`")) + assert.equals(path .. "/x", cwd.expand_leading("$PATH/x")) + assert.equals(path .. "/x", cwd.expand_leading("${PATH}/x")) + assert.equals("/a/`id`/$PATH/*", cwd.expand_leading("/a/`id`/$PATH/*")) + assert.equals("~user/x", cwd.expand_leading("~user/x")) + assert.equals("$CLAUDECODE_UNSET_VAR_FOR_TEST/x", cwd.expand_leading("$CLAUDECODE_UNSET_VAR_FOR_TEST/x")) + end) + it("runs git rev-parse once for each lookup that resolve makes with git_repo_cwd", function() stub_git({ lines = { dir }, exit_code = 0 }) From 258188a9b1d2013de00d01c5e754c4b1de95cff9 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 3 Dec 2026 09:35:44 +0000 Subject: [PATCH 2/2] chore(verify): re-record covered.lock for the cwd expansion fix Only the whole-file hashes of lua/claudecode/terminal.lua and lua/claudecode/cwd.lua changed. The change is confined to cwd resolution in build_config and cwd.lua, which no model step covers; the terminal lifecycle and diff lifecycle models pass against the changed code. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015koAoMEFEUXUgoDF7Nam12 --- .agents/skills/verify-diff-lifecycle/covered.lock | 4 ++-- .agents/skills/verify-terminal-lifecycle/covered.lock | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.agents/skills/verify-diff-lifecycle/covered.lock b/.agents/skills/verify-diff-lifecycle/covered.lock index d5aa96dbe..aa8bffe38 100644 --- a/.agents/skills/verify-diff-lifecycle/covered.lock +++ b/.agents/skills/verify-diff-lifecycle/covered.lock @@ -1,7 +1,7 @@ == lua/claudecode/config.lua file lua/claudecode/config.lua 3afb1bdeff27bfb6 == lua/claudecode/cwd.lua -file lua/claudecode/cwd.lua b034e82c76e8dee4 +file lua/claudecode/cwd.lua 4c81047d31b3595b == lua/claudecode/diff.lua file lua/claudecode/diff.lua 2b7fd97e91645f00 block resolve_saved 36fd89d5b97f0173 @@ -172,7 +172,7 @@ file lua/claudecode/server/tcp.lua 610f324845bca7b5 == lua/claudecode/server/utils.lua file lua/claudecode/server/utils.lua 2915ae87b48f04ca == lua/claudecode/terminal.lua -file lua/claudecode/terminal.lua 2cb4a5721335543c +file lua/claudecode/terminal.lua e0d698185e5906cc == lua/claudecode/terminal/external.lua file lua/claudecode/terminal/external.lua 067b672c0fbb601d == lua/claudecode/terminal/native.lua diff --git a/.agents/skills/verify-terminal-lifecycle/covered.lock b/.agents/skills/verify-terminal-lifecycle/covered.lock index 297f87564..3a8506591 100644 --- a/.agents/skills/verify-terminal-lifecycle/covered.lock +++ b/.agents/skills/verify-terminal-lifecycle/covered.lock @@ -11,7 +11,7 @@ site lua/claudecode/init.lua: terminal.open({}, cmd_args) @ function M._create_c site lua/claudecode/init.lua: terminal.simple_toggle() @ function M._ensure_terminal_visible_if_connected() site lua/claudecode/init.lua: terminal.simple_toggle({}, cmd_args) @ function M._create_commands() == lua/claudecode/terminal.lua -file lua/claudecode/terminal.lua 2cb4a5721335543c +file lua/claudecode/terminal.lua e0d698185e5906cc block load_provider 6054d44a8928401c block get_provider f04cd4256840da00 block ensure_visible 14c1de4d02f650ed