Skip to content

Commit

Permalink
Use terminal shell env when resolving commands in path
Browse files Browse the repository at this point in the history
Fixes #237587
  • Loading branch information
Tyriar committed Jan 9, 2025
1 parent 5f9f2db commit 2dd1393
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
3 changes: 2 additions & 1 deletion extensions/terminal-suggest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"Other"
],
"enabledApiProposals": [
"terminalCompletionProvider"
"terminalCompletionProvider",
"terminalShellEnv"
],
"scripts": {
"compile": "npx gulp compile-extension:terminal-suggest",
Expand Down
31 changes: 24 additions & 7 deletions extensions/terminal-suggest/src/terminalSuggestMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import codeInsidersCompletionSpec from './completions/code-insiders';
import codeCompletionSpec from './completions/code';
import cdSpec from './completions/cd';

let cachedAvailableCommandsPath: string | undefined;
let cachedAvailableCommands: Set<string> | undefined;
const cachedBuiltinCommands: Map<string, string[] | undefined> = new Map();

Expand Down Expand Up @@ -80,7 +81,7 @@ export async function activate(context: vscode.ExtensionContext) {
return;
}

const commandsInPath = await getCommandsInPath();
const commandsInPath = await getCommandsInPath(terminal.shellIntegration?.env);
const builtinCommands = getBuiltinCommands(shellPath);
if (!commandsInPath || !builtinCommands) {
return;
Expand Down Expand Up @@ -129,6 +130,7 @@ export async function resolveCwdFromPrefix(prefix: string, currentCwd?: vscode.U

// Resolve the absolute path of the prefix
const resolvedPath = path.resolve(currentCwd?.fsPath, relativeFolder);

const stat = await fs.stat(resolvedPath);

// Check if the resolved path exists and is a directory
Expand Down Expand Up @@ -187,15 +189,30 @@ async function isExecutable(filePath: string): Promise<boolean> {
}
}

async function getCommandsInPath(): Promise<Set<string> | undefined> {
if (cachedAvailableCommands) {
return cachedAvailableCommands;
async function getCommandsInPath(env: { [key: string]: string | undefined } = process.env): Promise<Set<string> | undefined> {
// Get PATH value
let pathValue: string | undefined;
if (osIsWindows()) {
const caseSensitivePathKey = Object.keys(env).find(key => key.toLowerCase() === 'path');
if (caseSensitivePathKey) {
pathValue = env[caseSensitivePathKey];
}
} else {
pathValue = env.PATH;
}
const paths = osIsWindows() ? process.env.PATH?.split(';') : process.env.PATH?.split(':');
if (!paths) {
if (pathValue === undefined) {
return;
}
const pathSeparator = osIsWindows() ? '\\' : '/';

// Check cache
if (cachedAvailableCommands && cachedAvailableCommandsPath === pathValue) {
return cachedAvailableCommands;
}

// Extract executables from PATH
const isWindows = osIsWindows();
const paths = pathValue.split(isWindows ? ';' : ':');
const pathSeparator = isWindows ? '\\' : '/';
const executables = new Set<string>();
for (const path of paths) {
try {
Expand Down
3 changes: 2 additions & 1 deletion extensions/terminal-suggest/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"src/**/*",
"src/completions/index.d.ts",
"../../src/vscode-dts/vscode.d.ts",
"../../src/vscode-dts/vscode.proposed.terminalCompletionProvider.d.ts"
"../../src/vscode-dts/vscode.proposed.terminalCompletionProvider.d.ts",
"../../src/vscode-dts/vscode.proposed.terminalShellEnv.d.ts"
]
}

0 comments on commit 2dd1393

Please sign in to comment.