Skip to content
Next Next commit
Proposal TerminalExitStatus
  • Loading branch information
jeanp413 committed Jun 25, 2022
commit 757c5f54c90bfde5406d0436df3229cb8e5d12bc
5 changes: 5 additions & 0 deletions src/vs/platform/terminal/common/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -790,3 +790,8 @@ export interface IShellIntegration {
capabilities: ITerminalCapabilityStore;
deserialize(serialized: ISerializedCommandDetectionCapability): void;
}

export enum TerminalExitReason {
Unknown = 0,
Shutdown = 1
}
2 changes: 1 addition & 1 deletion src/vs/workbench/api/browser/mainThreadTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export class MainThreadTerminalService implements MainThreadTerminalServiceShape
}

private _onTerminalDisposed(terminalInstance: ITerminalInstance): void {
this._proxy.$acceptTerminalClosed(terminalInstance.instanceId, terminalInstance.exitCode);
this._proxy.$acceptTerminalClosed(terminalInstance.instanceId, terminalInstance.exitCode, terminalInstance.exitReason);
}

private _onTerminalOpened(terminalInstance: ITerminalInstance): void {
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/api/common/extHost.api.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,7 @@ export function createApiFactoryAndRegisterActors(accessor: ServicesAccessor): I
TaskPanelKind: extHostTypes.TaskPanelKind,
TaskRevealKind: extHostTypes.TaskRevealKind,
TaskScope: extHostTypes.TaskScope,
TerminalExitReason: extHostTypes.TerminalExitReason,
TerminalLink: extHostTypes.TerminalLink,
TerminalLocation: extHostTypes.TerminalLocation,
TerminalProfile: extHostTypes.TerminalProfile,
Expand Down
4 changes: 2 additions & 2 deletions src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import * as quickInput from 'vs/platform/quickinput/common/quickInput';
import { IRemoteConnectionData, TunnelDescription } from 'vs/platform/remote/common/remoteAuthorityResolver';
import { ClassifiedEvent, GDPRClassification, StrictPropertyCheck } from 'vs/platform/telemetry/common/gdprTypings';
import { TelemetryLevel } from 'vs/platform/telemetry/common/telemetry';
import { ICreateContributedTerminalProfileOptions, IProcessProperty, IShellLaunchConfigDto, ITerminalEnvironment, ITerminalLaunchError, ITerminalProfile, TerminalLocation } from 'vs/platform/terminal/common/terminal';
import { ICreateContributedTerminalProfileOptions, IProcessProperty, IShellLaunchConfigDto, ITerminalEnvironment, ITerminalLaunchError, ITerminalProfile, TerminalExitReason, TerminalLocation } from 'vs/platform/terminal/common/terminal';
import { ThemeColor, ThemeIcon } from 'vs/platform/theme/common/themeService';
import { ProvidedPortAttributes, TunnelCreationOptions, TunnelOptions, TunnelPrivacyId, TunnelProviderFeatures } from 'vs/platform/tunnel/common/tunnel';
import { WorkspaceTrustRequestOptions } from 'vs/platform/workspace/common/workspaceTrust';
Expand Down Expand Up @@ -1817,7 +1817,7 @@ export interface ITerminalDimensionsDto {
}

export interface ExtHostTerminalServiceShape {
$acceptTerminalClosed(id: number, exitCode: number | undefined): void;
$acceptTerminalClosed(id: number, exitCode: number | undefined, exitReason: TerminalExitReason | undefined): void;
$acceptTerminalOpened(id: number, extHostTerminalId: string | undefined, name: string, shellLaunchConfig: IShellLaunchConfigDto): void;
$acceptActiveTerminalChanged(id: number | null): void;
$acceptTerminalProcessId(id: number, processId: number): void;
Expand Down
10 changes: 5 additions & 5 deletions src/vs/workbench/api/common/extHostTerminalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createDecorator } from 'vs/platform/instantiation/common/instantiation'
import { URI } from 'vs/base/common/uri';
import { IExtHostRpcService } from 'vs/workbench/api/common/extHostRpcService';
import { IDisposable, DisposableStore, Disposable } from 'vs/base/common/lifecycle';
import { Disposable as VSCodeDisposable, EnvironmentVariableMutatorType } from './extHostTypes';
import { Disposable as VSCodeDisposable, EnvironmentVariableMutatorType, TerminalExitReason } from './extHostTypes';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { localize } from 'vs/nls';
import { NotSupportedError } from 'vs/base/common/errors';
Expand Down Expand Up @@ -203,8 +203,8 @@ export class ExtHostTerminal {
this._name = name;
}

public setExitCode(code: number | undefined) {
this._exitStatus = Object.freeze({ code });
public setExitCode(code: number | undefined, reason: TerminalExitReason | undefined) {
this._exitStatus = Object.freeze({ code, reason });
}

public setDimensions(cols: number, rows: number): boolean {
Expand Down Expand Up @@ -500,11 +500,11 @@ export abstract class BaseExtHostTerminalService extends Disposable implements I
}
}

public async $acceptTerminalClosed(id: number, exitCode: number | undefined): Promise<void> {
public async $acceptTerminalClosed(id: number, exitCode: number | undefined, exitReason: TerminalExitReason | undefined): Promise<void> {
const index = this._getTerminalObjectIndexById(this._terminals, id);
if (index !== null) {
const terminal = this._terminals.splice(index, 1)[0];
terminal.setExitCode(exitCode);
terminal.setExitCode(exitCode, exitReason);
this._onDidCloseTerminal.fire(terminal.value);
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/api/common/extHostTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,11 @@ export enum SourceControlInputBoxValidationType {
Information = 2
}

export enum TerminalExitReason {
Unknown = 0,
Shutdown = 1
}

export class TerminalLink implements vscode.TerminalLink {
constructor(
public startIndex: number,
Expand Down
11 changes: 5 additions & 6 deletions src/vs/workbench/contrib/terminal/browser/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { FindReplaceState } from 'vs/editor/contrib/find/browser/findState';
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import { IKeyMods } from 'vs/platform/quickinput/common/quickInput';
import { ITerminalCapabilityStore, ITerminalCommand } from 'vs/platform/terminal/common/capabilities/capabilities';
import { IExtensionTerminalProfile, IProcessPropertyMap, IShellIntegration, IShellLaunchConfig, ITerminalDimensions, ITerminalLaunchError, ITerminalProfile, ITerminalTabLayoutInfoById, ProcessPropertyType, TerminalIcon, TerminalLocation, TerminalShellType, TitleEventSource } from 'vs/platform/terminal/common/terminal';
import { IExtensionTerminalProfile, IProcessPropertyMap, IShellIntegration, IShellLaunchConfig, ITerminalDimensions, ITerminalLaunchError, ITerminalProfile, ITerminalTabLayoutInfoById, ProcessPropertyType, TerminalExitReason, TerminalIcon, TerminalLocation, TerminalShellType, TitleEventSource } from 'vs/platform/terminal/common/terminal';
import { IGenericMarkProperties } from 'vs/platform/terminal/common/terminalProcess';
import { IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
Expand Down Expand Up @@ -567,6 +567,8 @@ export interface ITerminalInstance {

readonly exitCode: number | undefined;

readonly exitReason: TerminalExitReason | undefined;

readonly areLinksReady: boolean;

/**
Expand Down Expand Up @@ -651,12 +653,9 @@ export interface ITerminalInstance {
/**
* Dispose the terminal instance, removing it from the panel/service and freeing up resources.
*
* @param immediate Whether the kill should be immediate or not. Immediate should only be used
* when VS Code is shutting down or in cases where the terminal dispose was user initiated.
* The immediate===false exists to cover an edge case where the final output of the terminal can
* get cut off. If immediate kill any terminal processes immediately.
* @param isShutdown Whether the kill was triggered by lifecycle shutdown
*/
dispose(immediate?: boolean): void;

@jeanp413 Jean Pierre (jeanp413) Jun 22, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is immediate deprecated? There was only one place that used it (in terminalQuickAccess.ts) but I changed it to terminalService.safeDisposeTerminal so it's the same as other calls.
If not I'll revet this change.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not totally sure what the current state of this is, but it was always a bit of a hack to add a param to dispose like this so I'd want to move away from it if we were to change things in this area.

dispose(isShutdown?: boolean): void;

/**
* Inform the process that the terminal is now detached.
Expand Down
10 changes: 6 additions & 4 deletions src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storag
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { ITerminalCommand, TerminalCapability } from 'vs/platform/terminal/common/capabilities/capabilities';
import { TerminalCapabilityStoreMultiplexer } from 'vs/platform/terminal/common/capabilities/terminalCapabilityStore';
import { IProcessDataEvent, IProcessPropertyMap, IShellLaunchConfig, ITerminalDimensionsOverride, ITerminalLaunchError, PosixShellType, ProcessPropertyType, TerminalIcon, TerminalLocation, TerminalSettingId, TerminalShellType, TitleEventSource, WindowsShellType } from 'vs/platform/terminal/common/terminal';
import { IProcessDataEvent, IProcessPropertyMap, IShellLaunchConfig, ITerminalDimensionsOverride, ITerminalLaunchError, PosixShellType, ProcessPropertyType, TerminalExitReason, TerminalIcon, TerminalLocation, TerminalSettingId, TerminalShellType, TitleEventSource, WindowsShellType } from 'vs/platform/terminal/common/terminal';
import { escapeNonWindowsPath } from 'vs/platform/terminal/common/terminalEnvironment';
import { activeContrastBorder, scrollbarSliderActiveBackground, scrollbarSliderBackground, scrollbarSliderHoverBackground } from 'vs/platform/theme/common/colorRegistry';
import { IColorTheme, ICssStyleCollector, IThemeService, registerThemingParticipant, ThemeIcon } from 'vs/platform/theme/common/themeService';
Expand Down Expand Up @@ -170,6 +170,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
private _isVisible: boolean;
private _isDisposed: boolean;
private _exitCode: number | undefined;
private _exitReason: TerminalExitReason | undefined;
private _skipTerminalCommands: string[];
private _shellType: TerminalShellType;
private _title: string = '';
Expand Down Expand Up @@ -275,6 +276,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
get areLinksReady(): boolean { return this._areLinksReady; }
get initialDataEvents(): string[] | undefined { return this._initialDataEvents; }
get exitCode(): number | undefined { return this._exitCode; }
get exitReason(): TerminalExitReason | undefined { return this._exitReason; }
get hadFocusOnExit(): boolean { return this._hadFocusOnExit; }
get isTitleSetByProcess(): boolean { return !!this._messageTitleDisposable; }
get shellLaunchConfig(): IShellLaunchConfig { return this._shellLaunchConfig; }
Expand Down Expand Up @@ -1305,8 +1307,7 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
return confirmation.confirmed;
}


override dispose(immediate?: boolean): void {
override dispose(isShutdown?: boolean): void {
if (this._isDisposed) {
return;
}
Expand Down Expand Up @@ -1345,7 +1346,8 @@ export class TerminalInstance extends Disposable implements ITerminalInstance {
this._pressAnyKeyToCloseListener = undefined;
}

this._processManager.dispose(immediate);
this._exitReason = isShutdown ? TerminalExitReason.Shutdown : TerminalExitReason.Unknown;
this._processManager.dispose();
// Process manager dispose/shutdown doesn't fire process exit, trigger with undefined if it
// hasn't happened yet
this._onProcessExit(undefined);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { localize } from 'vs/nls';
import { IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput';
import { IPickerQuickAccessItem, PickerQuickAccessProvider, TriggerAction } from 'vs/platform/quickinput/browser/pickerQuickAccess';
import { matchesFuzzy } from 'vs/base/common/filters';
import { ITerminalEditorService, ITerminalGroupService, ITerminalInstance } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ITerminalEditorService, ITerminalGroupService, ITerminalInstance, ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { TerminalCommandId } from 'vs/workbench/contrib/terminal/common/terminal';
import { IThemeService, ThemeIcon } from 'vs/platform/theme/common/themeService';
Expand All @@ -24,6 +24,7 @@ export class TerminalQuickAccessProvider extends PickerQuickAccessProvider<IPick

constructor(
@IEditorService private readonly _editorService: IEditorService,
@ITerminalEditorService private readonly _terminalService: ITerminalService,
@ITerminalEditorService private readonly _terminalEditorService: ITerminalEditorService,
@ITerminalGroupService private readonly _terminalGroupService: ITerminalGroupService,
@ICommandService private readonly _commandService: ICommandService,
Expand Down Expand Up @@ -119,7 +120,7 @@ export class TerminalQuickAccessProvider extends PickerQuickAccessProvider<IPick
this._commandService.executeCommand(TerminalCommandId.Rename, terminal);
return TriggerAction.NO_ACTION;
case 1:
terminal.dispose(true);
this._terminalService.safeDisposeTerminal(terminal);
return TriggerAction.REMOVE_ITEM;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ export class TerminalService implements ITerminalService {

// Force dispose of all terminal instances
for (const instance of this.instances) {
instance.dispose();
instance.dispose(true);
}

// Clear terminal layout info only when not persisting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const allApiProposals = Object.freeze({
telemetry: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.telemetry.d.ts',
terminalDataWriteEvent: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.terminalDataWriteEvent.d.ts',
terminalDimensions: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.terminalDimensions.d.ts',
terminalExitReason: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.terminalExitReason.d.ts',
terminalNameChangeEvent: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.terminalNameChangeEvent.d.ts',
testCoverage: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.testCoverage.d.ts',
testObserver: 'https://raw.githubusercontent.com/microsoft/vscode/main/src/vscode-dts/vscode.proposed.testObserver.d.ts',
Expand Down
35 changes: 35 additions & 0 deletions src/vscode-dts/vscode.proposed.terminalExitReason.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

declare module 'vscode' {

// https://github.com/microsoft/vscode/issues/130231

/**
* Terminal exit reason kind.
*/
export enum TerminalExitReason {
/**
* Unknow reason, possible reasons could be:
* - Triggered by user action
* - Triggered by running process
* - Triggered by an extension
*/
Unknown = 0,
Comment thread
jeanp413 marked this conversation as resolved.

/**
* The window closed/reloaded.
*/
Shutdown = 1
}

export interface TerminalExitStatus {
/**
* The reason that triggered the exit of a terminal.
*/
readonly reason: TerminalExitReason | undefined;
Comment thread
jeanp413 marked this conversation as resolved.
Outdated
}

}