src/vs/platform/agentHost/common/commandLineHelpers.ts

126 LOC · 120 covered · 6 uncovered · 27 ranges · 1895 concepts · 18 introducers · 899 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- commandLineHelpers.ts ×3
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { extUriBiasedIgnorePathCase } from '../../../base/common/resources.js';
7 > import { URI } from '../../../base/common/uri.js';
8 >
9 > /**
10 > * Result of {@link extractCdPrefix}: the directory the `cd` jumps to and the
11 > * remaining command after the chain operator.
12 > */
13 > export interface IExtractedCdPrefix {
14 > readonly directory: string;
15 > readonly command: string;
16 > }
17 >
18 > /**
19 > * Extracts a `cd <dir> &&` (or PowerShell equivalent) prefix from a command
20 > * line, returning the directory and remaining command. Does not check whether
21 > * the directory matches anything — callers do that comparison themselves.
22 > *
23 > * The separator between the `cd` and the remaining command may be `&&` or a
24 > * newline (bash treats a bare newline as a command separator like `;`).
25 > * PowerShell additionally accepts `;`. The remaining command may span multiple
26 > * lines (the model frequently emits `cd <dir>` on its own line followed by a
27 > * multi-line script).
28 > *
29 > * Recognized forms:
30 > * - bash: `cd <dir> && <suffix>`, `cd <dir>\n<suffix>`
31 > * - powershell: `cd <dir> && <suffix>`, `cd <dir>; <suffix>`, `cd <dir>\n<suffix>`
32 > * `cd /d <dir> && <suffix>`, `cd /d <dir>; <suffix>`
33 > * `Set-Location <dir> && <suffix>`, `Set-Location <dir>; <suffix>`
34 > * `Set-Location -Path <dir> && <suffix>`, `Set-Location -Path <dir>; <suffix>`
35 > *
36 > * Surrounding double quotes around `<dir>` are stripped.
37 > */
38 > export function extractCdPrefix(commandLine: string, isPowerShell: boolean): IExtractedCdPrefix | undefined {
39 > const cdPrefixMatch = commandLine.match( commandLineHelpers.ts ×2
40 > isPowerShell
41 > ? /^(?:cd(?: \/d)?|Set-Location(?: -Path)?) (?<dir>"[^"]*"|[^\s]+) *(?:&&|;|\r?\n)\s*(?<suffix>[\s\S]+)$/i commandLineHelpers.ts ×1
42 > : /^cd (?<dir>"[^"]*"|[^\s]+) *(?:&&|\r?\n)\s*(?<suffix>[\s\S]+)$/ commandLineHelpers.ts ×1
44 > const cdDir = cdPrefixMatch?.groups?.dir;
45 > const cdSuffix = cdPrefixMatch?.groups?.suffix;
46 > if (cdDir && cdSuffix) {
47 > let cdDirPath = cdDir; commandLineHelpers.ts ×2
48 > if (cdDirPath.startsWith('"') && cdDirPath.endsWith('"')) {
49 > cdDirPath = cdDirPath.slice(1, -1); commandLineHelpers.ts ×1
50 > }
51 > return { directory: cdDirPath, command: cdSuffix }; commandLineHelpers.ts ×2
52 > }
53 > return undefined; commandLineHelpers.ts ×1
54 > }
56 > /**
57 > * If `toolName` is a shell tool (`bash` or `powershell`) and
58 > * `parameters.command` starts with a `cd <workingDirectory> && …` (or
59 > * PowerShell equivalent) prefix, mutate `parameters.command` to drop the
60 > * prefix and return `true`. Returns `false` otherwise.
61 > *
62 > * Path comparison normalizes trailing slashes and is case-insensitive on
63 > * Windows.
64 > */
65 > export function stripRedundantCdPrefix(
66 > toolName: string, commandLineHelpers.ts ×2
67 > parameters: Record<string, unknown> | undefined,
68 > workingDirectory: URI | undefined,
69 > ): boolean {
70 > if (!workingDirectory || !parameters) {
71 > return false; commandLineHelpers.ts ×1
72 > }
73 > const isBash = toolName === 'bash'; commandLineHelpers.ts ×1
74 > const isPowerShell = toolName === 'powershell';
75 > if (!isBash && !isPowerShell) { commandLineHelpers.ts ×2
76 > return false; commandLineHelpers.ts ×1
77 > }
78 > const command = parameters.command; commandLineHelpers.ts ×1
79 > if (typeof command !== 'string') {
80 > return false; commandLineHelpers.ts ×1
81 > }
82 > const extracted = extractCdPrefix(command, isPowerShell); commandLineHelpers.ts ×1
83 > if (!extracted) {
84 > return false; commandLineHelpers.ts ×1
85 > }
86 > if (!sameDirectory(extracted.directory, workingDirectory)) { commandLineHelpers.ts ×5
87 > return false; commandLineHelpers.ts ×1
88 > }
89 > parameters.command = extracted.command; commandLineHelpers.ts ×1
90 > return true;
91 > }
93 > /**
94 > * Compares an extracted `cd <dir>` argument (a raw filesystem path string,
95 > * possibly using either `/` or `\` separators) to a working-directory URI.
96 > * Normalizes separators by routing the extracted string through `URI.file`,
97 > * which converts to the platform-native `fsPath` shape, so that e.g.
98 > * `cd C:/repo` matches a working directory of `C:\repo` on Windows.
99 > *
100 > * Path comparison uses {@link extUriBiasedIgnorePathCase}, which is
101 > * case-insensitive on Windows / macOS.
102 > */
103 > function sameDirectory(extractedDir: string, workingDirectory: URI): boolean { commandLineHelpers.ts ×5
104 > if (!extractedDir) {
105 return false;
106 }
107 > // Strip trailing path separators (either flavor) so e.g. `/repo/project/` commandLineHelpers.ts ×5
108 > // matches `/repo/project`. Without this, URI.file would preserve the
109 > // trailing slash and the URIs would not compare equal. We do this for
110 > // both sides because the working directory may also end in a separator.
111 > const trim = (p: string) => p.replace(/[\\/]+$/, '');
112 > const trimmedExtracted = trim(extractedDir);
113 > const trimmedWd = trim(workingDirectory.fsPath);
114 > if (!trimmedExtracted || !trimmedWd) {
115 return false;
116 }
117 > let extractedUri: URI; commandLineHelpers.ts ×5
118 > let wdUri: URI;
119 > try {
120 > extractedUri = URI.file(trimmedExtracted);
121 > wdUri = URI.file(trimmedWd);
122 > } catch {
123 return false;
124 }
125 > return extUriBiasedIgnorePathCase.isEqual(extractedUri, wdUri); commandLineHelpers.ts ×5
126 > }