Use terminal shell env when resolving commands in path (#237588)

---------

Co-authored-by: Daniel Imms <Tyriar@users.noreply.github.com>
Co-authored-by: Anthony Kim <62267334+anthonykim1@users.noreply.github.com>
pull/237656/head
Daniel Imms 2025-01-10 06:05:34 -08:00 committed by GitHub
parent 0e20692aaf
commit 7bfaf73501
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 9 deletions

View File

@ -14,7 +14,8 @@
"Other"
],
"enabledApiProposals": [
"terminalCompletionProvider"
"terminalCompletionProvider",
"terminalShellEnv"
],
"scripts": {
"compile": "npx gulp compile-extension:terminal-suggest",

View File

@ -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();
@ -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;
@ -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
@ -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 {

View File

@ -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"
]
}