Skip to content

Commit 244448e

Browse files
authored
Merge pull request microsoft#256711 from microsoft/copilot/fix-256710
Handle cd /d on Windows in command rewriting
2 parents ce13309 + 8f39eef commit 244448e

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/runInTerminalTool.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
428428
const isPwsh = isPowerShell(shell, os);
429429
const cdPrefixMatch = commandLine.match(
430430
isPwsh
431-
? /^(?:cd|Set-Location(?: -Path)?) (?<dir>[^\s]+) ?(?:&&|;)\s+(?<suffix>.+)$/i
431+
? /^(?:cd(?: \/d)?|Set-Location(?: -Path)?) (?<dir>[^\s]+) ?(?:&&|;)\s+(?<suffix>.+)$/i
432432
: /^cd (?<dir>[^\s]+) &&\s+(?<suffix>.+)$/
433433
);
434434
const cdDir = cdPrefixMatch?.groups?.dir;

src/vs/workbench/contrib/terminalContrib/chatAgentTools/test/browser/runInTerminalTool.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,84 @@ suite('RunInTerminalTool', () => {
500500

501501
strictEqual(result, 'npm test');
502502
});
503+
504+
test('should handle cd /d flag when directory matches cwd', async () => {
505+
const testDir = 'C:\\test\\workspace';
506+
const options = createRewriteParams(`cd /d ${testDir} && echo hello`, 'session-1');
507+
workspaceService.setWorkspace({
508+
folders: [{ uri: { fsPath: testDir } }]
509+
} as any);
510+
511+
const result = await runInTerminalTool.rewriteCommandIfNeeded(options, undefined, 'pwsh');
512+
513+
strictEqual(result, 'echo hello');
514+
});
515+
516+
test('should handle cd /d flag with quoted paths when directory matches cwd', async () => {
517+
const testDir = 'C:\\test\\workspace';
518+
const options = createRewriteParams(`cd /d "${testDir}" && echo hello`, 'session-1');
519+
workspaceService.setWorkspace({
520+
folders: [{ uri: { fsPath: testDir } }]
521+
} as any);
522+
523+
const result = await runInTerminalTool.rewriteCommandIfNeeded(options, undefined, 'pwsh');
524+
525+
strictEqual(result, 'echo hello');
526+
});
527+
528+
test('should handle cd /d flag with quoted paths from issue example', async () => {
529+
const testDir = 'd:\\microsoft\\vscode';
530+
const options = createRewriteParams(`cd /d "${testDir}" && .\\scripts\\test.bat`, 'session-1');
531+
workspaceService.setWorkspace({
532+
folders: [{ uri: { fsPath: testDir } }]
533+
} as any);
534+
535+
const result = await runInTerminalTool.rewriteCommandIfNeeded(options, undefined, 'pwsh');
536+
537+
strictEqual(result, '.\\scripts\\test.bat');
538+
});
539+
540+
test('should not rewrite cd /d when directory does not match cwd', async () => {
541+
const testDir = 'C:\\test\\workspace';
542+
const differentDir = 'C:\\different\\path';
543+
const command = `cd /d ${differentDir} && echo hello`;
544+
const options = createRewriteParams(command, 'session-1');
545+
workspaceService.setWorkspace({
546+
folders: [{ uri: { fsPath: testDir } }]
547+
} as any);
548+
549+
const result = await runInTerminalTool.rewriteCommandIfNeeded(options, undefined, 'pwsh');
550+
551+
strictEqual(result, command);
552+
});
553+
554+
test('should handle cd /d flag with instance priority', async () => {
555+
const instanceDir = 'C:\\instance\\workspace';
556+
const workspaceDir = 'C:\\workspace\\service';
557+
const command = `cd /d ${instanceDir} && npm test`;
558+
const parameters = createRewriteParams(command, 'session-1');
559+
560+
workspaceService.setWorkspace({
561+
folders: [{ uri: { fsPath: workspaceDir } }]
562+
} as any);
563+
const instance = createInstanceWithCwd({ fsPath: instanceDir } as any);
564+
565+
const result = await runInTerminalTool.rewriteCommandIfNeeded(parameters, instance, 'pwsh');
566+
567+
strictEqual(result, 'npm test');
568+
});
569+
570+
test('should handle cd /d flag with semicolon separator', async () => {
571+
const testDir = 'C:\\test\\workspace';
572+
const options = createRewriteParams(`cd /d ${testDir}; echo hello`, 'session-1');
573+
workspaceService.setWorkspace({
574+
folders: [{ uri: { fsPath: testDir } }]
575+
} as any);
576+
577+
const result = await runInTerminalTool.rewriteCommandIfNeeded(options, undefined, 'pwsh');
578+
579+
strictEqual(result, 'echo hello');
580+
});
503581
});
504582
});
505583
});

0 commit comments

Comments
 (0)