@@ -10,7 +10,7 @@ export namespace serverTaskPresenter {
1010 export async function presentServerTaskView ( ) {
1111 const execution = await getPresenterTaskExecution ( ) ;
1212 const terminals = vscode . window . terminals ;
13- const presenterTerminals = terminals . filter ( terminal => terminal . name . endsWith ( execution . task . name ) ) ;
13+ const presenterTerminals = terminals . filter ( terminal => terminal . name . indexOf ( execution . task . name ) >= 0 ) ;
1414 if ( presenterTerminals . length > 0 ) {
1515 presenterTerminals [ 0 ] . show ( ) ;
1616 }
@@ -20,6 +20,8 @@ export namespace serverTaskPresenter {
2020let presenterTaskExecution : TaskExecution = null ;
2121
2222async function getPresenterTaskExecution ( ) : Promise < TaskExecution > {
23+ await killExistingExecutions ( ) ;
24+
2325 if ( ! ! presenterTaskExecution ) {
2426 return Promise . resolve ( presenterTaskExecution ) ;
2527 }
@@ -39,11 +41,32 @@ async function getPresenterTaskExecution(): Promise<TaskExecution> {
3941 return presenterTaskExecution = await tasks . executeTask ( presenterTask ) ;
4042}
4143
44+ // Fix #1180. When switching to multiroot workspace by "Add Folder to Workspace...", vscode restarts the extension
45+ // host without deactivating the extension. See https://github.com/microsoft/vscode/issues/69335
46+ // This is to clean up the existing task execution and terminal created by previous extension instance because they
47+ // are no longer accessible to the current extension instance afte the restart.
48+ // TODO - As mentioned in https://github.com/microsoft/vscode/issues/69335, vscode will no long restart because of
49+ // workspace changes. We can revisit this issue to see if we can remove the fix.
50+ async function killExistingExecutions ( ) {
51+ if ( ! ! presenterTaskExecution ) {
52+ return ;
53+ }
54+
55+ let execs = tasks . taskExecutions ;
56+ execs = execs . filter ( exec => exec . task . name . indexOf ( JAVA_SERVER_TASK_PRESENTER_TASK_NAME ) >= 0 ) ;
57+ execs . forEach ( exec => exec . terminate ( ) ) ;
58+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
59+
60+ let terminals = vscode . window . terminals ;
61+ terminals = terminals . filter ( terminal => terminal . name . indexOf ( JAVA_SERVER_TASK_PRESENTER_TASK_NAME ) >= 0 ) ;
62+ terminals . forEach ( terminal => terminal . dispose ( ) ) ;
63+ await new Promise ( resolve => setTimeout ( resolve , 0 ) ) ;
64+ }
65+
4266class ServerTaskTerminal implements Pseudoterminal {
4367 private _onDidWriteEvent = new EventEmitter < string > ( ) ;
4468 private _onDidCloseEvent = new EventEmitter < number | void > ( ) ;
4569 private _subscription : Disposable = null ;
46- private _rows : number = 1 ;
4770
4871 onDidWrite : Event < string > = this . _onDidWriteEvent . event ;
4972 onDidClose ?: Event < number | void > = this . _onDidCloseEvent . event ;
0 commit comments