forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix shell integration API reliability (#22446)
#22440 It leads to terminals activating forever.
- Loading branch information
Kartik Raj
authored
Nov 8, 2023
1 parent
1b3c1ea
commit 8d174a8
Showing
11 changed files
with
200 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 0 additions & 13 deletions
13
src/client/terminals/envCollectionActivation/shellIntegration.ts
This file was deleted.
Oops, something went wrong.
81 changes: 81 additions & 0 deletions
81
src/client/terminals/envCollectionActivation/shellIntegrationService.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
import { injectable, inject } from 'inversify'; | ||
import { IApplicationShell, ITerminalManager, IWorkspaceService } from '../../common/application/types'; | ||
import { identifyShellFromShellPath } from '../../common/terminal/shellDetectors/baseShellDetector'; | ||
import { TerminalShellType } from '../../common/terminal/types'; | ||
import { createDeferred, sleep } from '../../common/utils/async'; | ||
import { cache } from '../../common/utils/decorators'; | ||
import { traceError, traceVerbose } from '../../logging'; | ||
import { IShellIntegrationService } from '../types'; | ||
|
||
/** | ||
* This is a list of shells which support shell integration: | ||
* https://code.visualstudio.com/docs/terminal/shell-integration | ||
*/ | ||
const ShellIntegrationShells = [ | ||
TerminalShellType.powershell, | ||
TerminalShellType.powershellCore, | ||
TerminalShellType.bash, | ||
TerminalShellType.zsh, | ||
TerminalShellType.fish, | ||
]; | ||
|
||
@injectable() | ||
export class ShellIntegrationService implements IShellIntegrationService { | ||
constructor( | ||
@inject(ITerminalManager) private readonly terminalManager: ITerminalManager, | ||
@inject(IApplicationShell) private readonly appShell: IApplicationShell, | ||
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService, | ||
) {} | ||
|
||
public async isWorking(shell: string): Promise<boolean> { | ||
return this._isWorking(shell).catch((ex) => { | ||
traceError(`Failed to determine if shell supports shell integration`, shell, ex); | ||
return false; | ||
}); | ||
} | ||
|
||
@cache(-1, true) | ||
public async _isWorking(shell: string): Promise<boolean> { | ||
const isEnabled = this.workspaceService | ||
.getConfiguration('terminal') | ||
.get<boolean>('integrated.shellIntegration.enabled')!; | ||
if (!isEnabled) { | ||
traceVerbose('Shell integrated is disabled in user settings.'); | ||
} | ||
const isSupposedToWork = isEnabled && ShellIntegrationShells.includes(identifyShellFromShellPath(shell)); | ||
if (!isSupposedToWork) { | ||
return false; | ||
} | ||
const deferred = createDeferred<void>(); | ||
const timestamp = new Date().getTime(); | ||
const name = `Python ${timestamp}`; | ||
const onDidExecuteTerminalCommand = this.appShell.onDidExecuteTerminalCommand?.bind(this.appShell); | ||
if (!onDidExecuteTerminalCommand) { | ||
// Proposed API is not available, assume shell integration is working at this point. | ||
return true; | ||
} | ||
try { | ||
const disposable = onDidExecuteTerminalCommand((e) => { | ||
if (e.terminal.name === name) { | ||
deferred.resolve(); | ||
} | ||
}); | ||
const terminal = this.terminalManager.createTerminal({ | ||
name, | ||
shellPath: shell, | ||
hideFromUser: true, | ||
}); | ||
terminal.sendText(`echo ${shell}`); | ||
const success = await Promise.race([sleep(3000).then(() => false), deferred.promise.then(() => true)]); | ||
disposable.dispose(); | ||
return success; | ||
} catch (ex) { | ||
traceVerbose(`Proposed API is not available, failed to subscribe to onDidExecuteTerminalCommand`, ex); | ||
// Proposed API is not available, assume shell integration is working at this point. | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.