Skip to content

Commit 7e5d570

Browse files
fix(web): cd options, shell comments and printed shell commands no longer skew file links
cd -P /dir was dropped because the option was read as the folder. A cd after a # comment, and a sh -c script that was only an argument to another command such as echo, were counted as folders the agent entered. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 5622273 commit 7e5d570

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

‎apps/web/src/workspaceBasenameLookup.test.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ describe("agentWorkingDirectories", () => {
132132
"cd /work/repo-c && cd /work/repo-b && git diff",
133133
"git status",
134134
`/bin/zsh -lc "cd '/work/my repo'; ls"`,
135+
"cd -P /work/repo-a # not this; cd /work/commented",
135136
"cd /work/repo-c\npwd",
136137
),
137-
).toEqual(["/work/repo-c", "/work/my repo", "/work/repo-b"]);
138+
).toEqual(["/work/repo-c", "/work/repo-a", "/work/my repo", "/work/repo-b"]);
138139
});
139140

140141
it("skips operands only the shell can resolve and words that are not the command", () => {
@@ -143,6 +144,7 @@ describe("agentWorkingDirectories", () => {
143144
"cd docs && cd ../other && cd ~/repo && cd $ROOT/x && cd /work/*/src",
144145
"abcd /work/a; echo cd /work/b; git -C /work/c status; cd /work/d",
145146
`echo "cd /work/g"; echo 'x; cd /work/h' && grep -c "cd /work/i" notes`,
147+
"echo bash -c 'cd /work/j'; cd -",
146148
"cd /work/e\\ f && cd `pwd`/x",
147149
),
148150
).toEqual(["/work/d"]);

‎packages/client-runtime/src/workspaceBasenameLookup.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,24 @@ export function pickProjectLinkMatch(
137137
return matches.length === 1 ? (matches[0] ?? null) : null;
138138
}
139139

140-
// A quoted string is one token, so `cd` inside quoted data is not read as a command.
141-
const SHELL_TOKEN = /"[^"]*"|'[^']*'|[;&|()\n]|[ \t]+|[^\s;&|()"']+/g;
140+
// A quoted string or a comment is one token, so `cd` inside either is not read as a command.
141+
const SHELL_TOKEN = /(?<![^\s;&|()])#[^\n]*|"[^"]*"|'[^']*'|[;&|()\n]|[ \t]+|[^\s;&|()"']+/g;
142142
const SHELL_NAME = /(?:^|\/)(?:ba|z|da)?sh$/;
143+
const CD_OPTION = /^-[LPe@]*$|^--$/;
143144

144145
/** The operands of the `cd` commands in `script`, in order, through `sh -c "<script>"`. */
145146
function cdOperands(script: string): Array<string> {
146147
const operands: Array<string> = [];
147148
let words: Array<string> = [];
148149
let wordOpen = false;
149150
const endCommand = () => {
150-
const flag = words.findIndex((word) => /^-[a-z]*c$/.test(word));
151-
const inner = words[flag + 1];
152-
if (words[0] === "cd" && words[1] !== undefined) operands.push(words[1]);
153-
else if (flag > 0 && inner !== undefined && /^(["']).*\1$/s.test(inner)) {
154-
if (words.slice(0, flag).some((word) => SHELL_NAME.test(word))) {
151+
const [name = "", ...rest] = words;
152+
if (name === "cd") {
153+
const operand = rest.find((word) => !CD_OPTION.test(word));
154+
if (operand !== undefined) operands.push(operand);
155+
} else if (SHELL_NAME.test(name)) {
156+
const inner = rest[rest.findIndex((word) => /^-[a-z]*c$/.test(word)) + 1];
157+
if (inner !== undefined && /^(["']).*\1$/s.test(inner)) {
155158
operands.push(...cdOperands(inner.slice(1, -1)));
156159
}
157160
}
@@ -160,7 +163,7 @@ function cdOperands(script: string): Array<string> {
160163
};
161164
for (const [token] of script.matchAll(SHELL_TOKEN)) {
162165
if (/^[;&|()\n]/.test(token)) endCommand();
163-
else if (/^[ \t]/.test(token)) wordOpen = false;
166+
else if (/^[ \t]/.test(token) || (!wordOpen && token.startsWith("#"))) wordOpen = false;
164167
else if (wordOpen) words[words.length - 1] += token;
165168
else {
166169
words.push(token);

0 commit comments

Comments
 (0)