Skip to content

Commit d4a73d0

Browse files
committed
Fixes microsoft#78751 - normalize paths for shells
1 parent 70f8912 commit d4a73d0

6 files changed

Lines changed: 85 additions & 15 deletions

File tree

‎src/vs/workbench/contrib/terminal/browser/terminal.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export interface ITerminalService {
141141
* @param path The path to be escaped and formatted.
142142
* @returns An escaped version of the path to be execuded in the terminal.
143143
*/
144-
preparePathForTerminalAsync(path: string, executable: string | undefined, title: string): Promise<string>;
144+
preparePathForTerminalAsync(path: string, executable: string | undefined, title: string, shellType: TerminalShellType): Promise<string>;
145145

146146
extHostReady(remoteAuthority: string): void;
147147
requestSpawnExtHostProcess(proxy: ITerminalProcessExtHostProxy, shellLaunchConfig: IShellLaunchConfig, activeWorkspaceRootUri: URI | undefined, cols: number, rows: number, isWorkspaceShellAllowed: boolean): void;
@@ -167,6 +167,14 @@ export interface ISearchOptions {
167167
incremental?: boolean;
168168
}
169169

170+
export enum WindowsShellType {
171+
CommandPrompt,
172+
PowerShell,
173+
Wsl,
174+
GitBash
175+
}
176+
export type TerminalShellType = WindowsShellType | undefined;
177+
170178
export interface ITerminalInstance {
171179
/**
172180
* The ID of the terminal instance, this is an arbitrary number only used to identify the
@@ -236,6 +244,11 @@ export interface ITerminalInstance {
236244
*/
237245
readonly title: string;
238246

247+
/**
248+
* The shell type of the terminal.
249+
*/
250+
readonly shellType: TerminalShellType;
251+
239252
/**
240253
* The focus state of the terminal before exiting.
241254
*/
@@ -431,6 +444,11 @@ export interface ITerminalInstance {
431444
*/
432445
setTitle(title: string, eventSource: TitleEventSource): void;
433446

447+
/**
448+
* Sets the shell type of the terminal instance.
449+
*/
450+
setShellType(shellType: TerminalShellType): void;
451+
434452
waitForTitle(): Promise<string>;
435453

436454
setDimensions(dimensions: ITerminalDimensions): void;

‎src/vs/workbench/contrib/terminal/browser/terminalActions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ export class RunActiveFileInTerminalAction extends Action {
722722
return Promise.resolve(undefined);
723723
}
724724

725-
return this.terminalService.preparePathForTerminalAsync(uri.fsPath, instance.shellLaunchConfig.executable, instance.title).then(path => {
725+
return this.terminalService.preparePathForTerminalAsync(uri.fsPath, instance.shellLaunchConfig.executable, instance.title, instance.shellType).then(path => {
726726
instance.sendText(path, true);
727727
return this.terminalService.showPanel();
728728
});

‎src/vs/workbench/contrib/terminal/browser/terminalInstance.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { TerminalConfigHelper } from 'vs/workbench/contrib/terminal/browser/term
3131
import { TerminalLinkHandler } from 'vs/workbench/contrib/terminal/browser/terminalLinkHandler';
3232
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
3333
import { IAccessibilityService, AccessibilitySupport } from 'vs/platform/accessibility/common/accessibility';
34-
import { ITerminalInstanceService, ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
34+
import { ITerminalInstanceService, ITerminalInstance, TerminalShellType } from 'vs/workbench/contrib/terminal/browser/terminal';
3535
import { TerminalProcessManager } from 'vs/workbench/contrib/terminal/browser/terminalProcessManager';
3636
import { Terminal as XTermTerminal, IBuffer, ITerminalAddon } from 'xterm';
3737
import { SearchAddon, ISearchOptions } from 'xterm-addon-search';
@@ -182,6 +182,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
182182
private _isVisible: boolean;
183183
private _isDisposed: boolean;
184184
private _skipTerminalCommands: string[];
185+
private _shellType: TerminalShellType;
185186
private _title: string = '';
186187
private _wrapperElement: (HTMLElement & { xterm?: XTermTerminal }) | undefined;
187188
private _xterm: XTermTerminal | undefined;
@@ -230,6 +231,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
230231
public get hadFocusOnExit(): boolean { return this._hadFocusOnExit; }
231232
public get isTitleSetByProcess(): boolean { return !!this._messageTitleDisposable; }
232233
public get shellLaunchConfig(): IShellLaunchConfig { return this._shellLaunchConfig; }
234+
public get shellType(): TerminalShellType { return this._shellType; }
233235
public get commandTracker(): CommandTrackerAddon | undefined { return this._commandTrackerAddon; }
234236
public get navigationMode(): INavigationMode | undefined { return this._navigationModeAddon; }
235237

@@ -1342,6 +1344,10 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
13421344
this._processManager.ptyProcessReady.then(() => this._processManager.setDimensions(cols, rows));
13431345
}
13441346

1347+
public setShellType(shellType: TerminalShellType) {
1348+
this._shellType = shellType;
1349+
}
1350+
13451351
public setTitle(title: string | undefined, eventSource: TitleEventSource): void {
13461352
if (!title) {
13471353
return;

‎src/vs/workbench/contrib/terminal/browser/terminalPanel.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export class TerminalPanel extends Panel {
292292

293293
const terminal = this._terminalService.getActiveInstance();
294294
if (terminal) {
295-
return this._terminalService.preparePathForTerminalAsync(path, terminal.shellLaunchConfig.executable, terminal.title).then(preparedPath => {
295+
return this._terminalService.preparePathForTerminalAsync(path, terminal.shellLaunchConfig.executable, terminal.title, terminal.shellType).then(preparedPath => {
296296
terminal.sendText(preparedPath, false);
297297
});
298298
}

‎src/vs/workbench/contrib/terminal/browser/terminalService.ts‎

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { IInstantiationService } from 'vs/platform/instantiation/common/instanti
1616
import { IExtensionService } from 'vs/workbench/services/extensions/common/extensions';
1717
import { IFileService } from 'vs/platform/files/common/files';
1818
import { TerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminalInstance';
19-
import { ITerminalService, ITerminalInstance, ITerminalTab } from 'vs/workbench/contrib/terminal/browser/terminal';
19+
import { ITerminalService, ITerminalInstance, ITerminalTab, TerminalShellType, WindowsShellType } from 'vs/workbench/contrib/terminal/browser/terminal';
2020
import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteAgentService';
2121
import { TerminalConfigHelper } from 'vs/workbench/contrib/terminal/browser/terminalConfigHelper';
2222
import { IQuickInputService, IQuickPickItem, IPickOptions } from 'vs/platform/quickinput/common/quickInput';
@@ -504,7 +504,7 @@ export class TerminalService implements ITerminalService {
504504
});
505505
}
506506

507-
public preparePathForTerminalAsync(originalPath: string, executable: string, title: string): Promise<string> {
507+
public preparePathForTerminalAsync(originalPath: string, executable: string, title: string, shellType: TerminalShellType): Promise<string> {
508508
return new Promise<string>(c => {
509509
if (!executable) {
510510
c(originalPath);
@@ -527,18 +527,41 @@ export class TerminalService implements ITerminalService {
527527
if (isWindows) {
528528
// 17063 is the build number where wsl path was introduced.
529529
// Update Windows uriPath to be executed in WSL.
530-
const lowerExecutable = executable.toLowerCase();
531-
if (this._terminalNativeService.getWindowsBuildNumber() >= 17063 &&
532-
(lowerExecutable.indexOf('wsl') !== -1 || (lowerExecutable.indexOf('bash.exe') !== -1 && lowerExecutable.toLowerCase().indexOf('git') === -1))) {
533-
c(this._terminalNativeService.getWslPath(originalPath));
534-
return;
535-
} else if (hasSpace) {
536-
c('"' + originalPath + '"');
530+
if (shellType !== undefined) {
531+
if (shellType === WindowsShellType.GitBash) {
532+
c(originalPath.replace(/\\/g, '/'));
533+
return;
534+
}
535+
else if (shellType === WindowsShellType.Wsl) {
536+
if (this._terminalNativeService.getWindowsBuildNumber() >= 17063) {
537+
c(this._terminalNativeService.getWslPath(originalPath));
538+
} else {
539+
c(originalPath.replace(/\\/g, '/'));
540+
}
541+
return;
542+
}
543+
544+
if (hasSpace) {
545+
c('"' + originalPath + '"');
546+
} else {
547+
c(originalPath);
548+
}
537549
} else {
538-
c(originalPath);
550+
const lowerExecutable = executable.toLowerCase();
551+
if (this._terminalNativeService.getWindowsBuildNumber() >= 17063 &&
552+
(lowerExecutable.indexOf('wsl') !== -1 || (lowerExecutable.indexOf('bash.exe') !== -1 && lowerExecutable.toLowerCase().indexOf('git') === -1))) {
553+
c(this._terminalNativeService.getWslPath(originalPath));
554+
return;
555+
} else if (hasSpace) {
556+
c('"' + originalPath + '"');
557+
} else {
558+
c(originalPath);
559+
}
539560
}
561+
540562
return;
541563
}
564+
542565
c(escapeNonWindowsPath(originalPath));
543566
});
544567
}

‎src/vs/workbench/contrib/terminal/electron-browser/windowsShellHelper.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IWindowsShellHelper, TitleEventSource } from 'vs/workbench/contrib/term
99
import { Terminal as XTermTerminal } from 'xterm';
1010
import * as WindowsProcessTreeType from 'windows-process-tree';
1111
import { Disposable } from 'vs/base/common/lifecycle';
12-
import { ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
12+
import { ITerminalInstance, TerminalShellType, WindowsShellType } from 'vs/workbench/contrib/terminal/browser/terminal';
1313

1414
const SHELL_EXECUTABLES = [
1515
'cmd.exe',
@@ -83,6 +83,7 @@ export class WindowsShellHelper extends Disposable implements IWindowsShellHelpe
8383
this.getShellName().then(title => {
8484
if (!this._isDisposed) {
8585
this._terminalInstance.setTitle(title, TitleEventSource.Process);
86+
this._terminalInstance.setShellType(this.getShellType(title));
8687
}
8788
});
8889
}
@@ -139,4 +140,26 @@ export class WindowsShellHelper extends Disposable implements IWindowsShellHelpe
139140
});
140141
return this._currentRequest;
141142
}
143+
144+
public getShellType(executable: string): TerminalShellType {
145+
switch (executable.toLowerCase()) {
146+
case 'cmd.exe':
147+
return WindowsShellType.CommandPrompt;
148+
case 'powershell.exe':
149+
case 'pwsh.exe':
150+
return WindowsShellType.PowerShell;
151+
case 'bash.exe':
152+
return WindowsShellType.GitBash;
153+
case 'wsl.exe':
154+
case 'ubuntu.exe':
155+
case 'ubuntu1804.exe':
156+
case 'kali.exe':
157+
case 'debian.exe':
158+
case 'opensuse-42.exe':
159+
case 'sles-12.exe':
160+
return WindowsShellType.Wsl;
161+
default:
162+
return undefined;
163+
}
164+
}
142165
}

0 commit comments

Comments
 (0)