Skip to content

Commit 06483a8

Browse files
committed
Fallback to processEnv when shell env isn't there
Fixes #240249
1 parent 50f0247 commit 06483a8

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

src/vs/workbench/contrib/terminalContrib/suggest/browser/terminalCompletionService.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { TerminalCapability, type ITerminalCapabilityStore } from '../../../../.
1414
import { GeneralShellType, TerminalShellType } from '../../../../../platform/terminal/common/terminal.js';
1515
import { TerminalSuggestSettingId } from '../common/terminalSuggestConfiguration.js';
1616
import { TerminalCompletionItemKind, type ITerminalCompletion } from './terminalCompletionItem.js';
17+
import { env as processEnv } from '../../../../../base/common/process.js';
1718

1819
export const ITerminalCompletionService = createDecorator<ITerminalCompletionService>('terminalCompletionService');
1920

@@ -87,6 +88,7 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
8788

8889
constructor(
8990
@IConfigurationService private readonly _configurationService: IConfigurationService,
91+
9092
@IFileService private readonly _fileService: IFileService
9193
) {
9294
super();
@@ -251,13 +253,9 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
251253
const type = lastWordFolderHasTildePrefix ? 'tilde' : isAbsolutePath ? 'absolute' : 'relative';
252254
switch (type) {
253255
case 'tilde': {
254-
const env = capabilities.get(TerminalCapability.ShellEnvDetection)?.env;
255-
if (env) {
256-
const home = useWindowsStylePath ? env.get('USERPROFILE') : env.get('HOME');
257-
// TODO: Handle the case where the HOME environment variable is not set
258-
if (home) {
259-
lastWordFolderResource = URI.joinPath(URI.file(home), lastWordFolder.slice(1).replaceAll('\\ ', ' '));
260-
}
256+
const home = getHomeDir(useWindowsStylePath, capabilities);
257+
if (home) {
258+
lastWordFolderResource = URI.joinPath(URI.file(home), lastWordFolder.slice(1).replaceAll('\\ ', ' '));
261259
}
262260
if (!lastWordFolderResource) {
263261
// Use less strong wording here as it's not as strong of a concept on Windows
@@ -389,7 +387,7 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
389387
if (promptValue.startsWith('cd ')) {
390388
const config = this._configurationService.getValue(TerminalSuggestSettingId.CdPath);
391389
if (config === 'absolute' || config === 'relative') {
392-
const cdPath = capabilities.get(TerminalCapability.ShellEnvDetection)?.env?.get('CDPATH');
390+
const cdPath = getEnvVar('CDPATH', capabilities);
393391
if (cdPath) {
394392
const cdPathEntries = cdPath.split(useWindowsStylePath ? ';' : ':');
395393
for (const cdPathEntry of cdPathEntries) {
@@ -446,14 +444,10 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
446444
//
447445
// - (relative) `|` -> `~`
448446
if (type === 'relative' && !lastWordFolder.match(/[\\\/]/)) {
449-
const env = capabilities.get(TerminalCapability.ShellEnvDetection)?.env;
450447
let homeResource: URI | string | undefined;
451-
if (env) {
452-
const home = useWindowsStylePath ? env.get('USERPROFILE') : env.get('HOME');
453-
// TODO: Handle the case where the HOME environment variable is not set
454-
if (home) {
455-
homeResource = URI.joinPath(URI.file(home), lastWordFolder.slice(1).replaceAll('\\ ', ' '));
456-
}
448+
const home = getHomeDir(useWindowsStylePath, capabilities);
449+
if (home) {
450+
homeResource = URI.joinPath(URI.file(home), lastWordFolder.slice(1).replaceAll('\\ ', ' '));
457451
}
458452
if (!homeResource) {
459453
// Use less strong wording here as it's not as strong of a concept on Windows
@@ -474,6 +468,18 @@ export class TerminalCompletionService extends Disposable implements ITerminalCo
474468
}
475469
}
476470

471+
function getEnvVar(key: string, capabilities: ITerminalCapabilityStore): string | undefined {
472+
const env = capabilities.get(TerminalCapability.ShellEnvDetection)?.env;
473+
if (env) {
474+
return env.get(key);
475+
}
476+
return processEnv[key];
477+
}
478+
479+
function getHomeDir(useWindowsStylePath: boolean, capabilities: ITerminalCapabilityStore): string | undefined {
480+
return useWindowsStylePath ? getEnvVar('USERPROFILE', capabilities) : getEnvVar('HOME', capabilities);
481+
}
482+
477483
function getFriendlyPath(uri: URI, pathSeparator: string, kind: TerminalCompletionItemKind): string {
478484
let path = uri.fsPath;
479485
// Ensure folders end with the path separator to differentiate presentation from files

src/vs/workbench/contrib/terminalContrib/suggest/test/browser/terminalCompletionService.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ function assertPartialCompletionsExist(actual: ITerminalCompletion[] | undefined
7878
}
7979
}
8080

81-
const standardTidleItem = Object.freeze({ label: '~', detail: isWindows ? 'Home directory' : '$HOME' });
81+
let homeDir = isWindows ? process.env['USERPROFILE'] : process.env['HOME'];
82+
if (!homeDir!.endsWith('/')) {
83+
homeDir += '/';
84+
}
85+
const standardTidleItem = Object.freeze({ label: '~', detail: homeDir });
8286

8387
suite('TerminalCompletionService', () => {
8488
const store = ensureNoDisposablesAreLeakedInTestSuite();

0 commit comments

Comments
 (0)