@@ -50,7 +50,12 @@ export class Remote {
50
50
/**
51
51
* Try to get the workspace running. Return undefined if the user canceled.
52
52
*/
53
- private async maybeWaitForRunning ( restClient : Api , workspace : Workspace ) : Promise < Workspace | undefined > {
53
+ private async maybeWaitForRunning (
54
+ restClient : Api ,
55
+ workspace : Workspace ,
56
+ label : string ,
57
+ binPath : string ,
58
+ ) : Promise < Workspace | undefined > {
54
59
// Maybe already running?
55
60
if ( workspace . latest_build . status === "running" ) {
56
61
return workspace
@@ -63,6 +68,28 @@ export class Remote {
63
68
let terminal : undefined | vscode . Terminal
64
69
let attempts = 0
65
70
71
+ function initWriteEmitterAndTerminal ( ) : vscode . EventEmitter < string > {
72
+ if ( ! writeEmitter ) {
73
+ writeEmitter = new vscode . EventEmitter < string > ( )
74
+ }
75
+ if ( ! terminal ) {
76
+ terminal = vscode . window . createTerminal ( {
77
+ name : "Build Log" ,
78
+ location : vscode . TerminalLocation . Panel ,
79
+ // Spin makes this gear icon spin!
80
+ iconPath : new vscode . ThemeIcon ( "gear~spin" ) ,
81
+ pty : {
82
+ onDidWrite : writeEmitter . event ,
83
+ close : ( ) => undefined ,
84
+ open : ( ) => undefined ,
85
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
86
+ } as Partial < vscode . Pseudoterminal > as any ,
87
+ } )
88
+ terminal . show ( true )
89
+ }
90
+ return writeEmitter
91
+ }
92
+
66
93
try {
67
94
// Show a notification while we wait.
68
95
return await this . vscodeProposed . window . withProgress (
@@ -72,39 +99,30 @@ export class Remote {
72
99
title : "Waiting for workspace build..." ,
73
100
} ,
74
101
async ( ) => {
102
+ const globalConfigDir = path . dirname ( this . storage . getSessionTokenPath ( label ) )
75
103
while ( workspace . latest_build . status !== "running" ) {
76
104
++ attempts
77
105
switch ( workspace . latest_build . status ) {
78
106
case "pending" :
79
107
case "starting" :
80
108
case "stopping" :
81
- if ( ! writeEmitter ) {
82
- writeEmitter = new vscode . EventEmitter < string > ( )
83
- }
84
- if ( ! terminal ) {
85
- terminal = vscode . window . createTerminal ( {
86
- name : "Build Log" ,
87
- location : vscode . TerminalLocation . Panel ,
88
- // Spin makes this gear icon spin!
89
- iconPath : new vscode . ThemeIcon ( "gear~spin" ) ,
90
- pty : {
91
- onDidWrite : writeEmitter . event ,
92
- close : ( ) => undefined ,
93
- open : ( ) => undefined ,
94
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
- } as Partial < vscode . Pseudoterminal > as any ,
96
- } )
97
- terminal . show ( true )
98
- }
109
+ writeEmitter = initWriteEmitterAndTerminal ( )
99
110
this . storage . writeToCoderOutputChannel ( `Waiting for ${ workspaceName } ...` )
100
111
workspace = await waitForBuild ( restClient , writeEmitter , workspace )
101
112
break
102
113
case "stopped" :
103
114
if ( ! ( await this . confirmStart ( workspaceName ) ) ) {
104
115
return undefined
105
116
}
117
+ writeEmitter = initWriteEmitterAndTerminal ( )
106
118
this . storage . writeToCoderOutputChannel ( `Starting ${ workspaceName } ...` )
107
- workspace = await startWorkspaceIfStoppedOrFailed ( restClient , workspace )
119
+ workspace = await startWorkspaceIfStoppedOrFailed (
120
+ restClient ,
121
+ globalConfigDir ,
122
+ binPath ,
123
+ workspace ,
124
+ writeEmitter ,
125
+ )
108
126
break
109
127
case "failed" :
110
128
// On a first attempt, we will try starting a failed workspace
@@ -113,8 +131,15 @@ export class Remote {
113
131
if ( ! ( await this . confirmStart ( workspaceName ) ) ) {
114
132
return undefined
115
133
}
134
+ writeEmitter = initWriteEmitterAndTerminal ( )
116
135
this . storage . writeToCoderOutputChannel ( `Starting ${ workspaceName } ...` )
117
- workspace = await startWorkspaceIfStoppedOrFailed ( restClient , workspace )
136
+ workspace = await startWorkspaceIfStoppedOrFailed (
137
+ restClient ,
138
+ globalConfigDir ,
139
+ binPath ,
140
+ workspace ,
141
+ writeEmitter ,
142
+ )
118
143
break
119
144
}
120
145
// Otherwise fall through and error.
@@ -156,6 +181,9 @@ export class Remote {
156
181
157
182
const workspaceName = `${ parts . username } /${ parts . workspace } `
158
183
184
+ // Migrate "session_token" file to "session", if needed.
185
+ await this . storage . migrateSessionToken ( parts . label )
186
+
159
187
// Get the URL and token belonging to this host.
160
188
const { url : baseUrlRaw , token } = await this . storage . readCliConfig ( parts . label )
161
189
@@ -292,7 +320,7 @@ export class Remote {
292
320
disposables . push ( this . registerLabelFormatter ( remoteAuthority , workspace . owner_name , workspace . name ) )
293
321
294
322
// If the workspace is not in a running state, try to get it running.
295
- const updatedWorkspace = await this . maybeWaitForRunning ( workspaceRestClient , workspace )
323
+ const updatedWorkspace = await this . maybeWaitForRunning ( workspaceRestClient , workspace , parts . label , binaryPath )
296
324
if ( ! updatedWorkspace ) {
297
325
// User declined to start the workspace.
298
326
await this . closeRemote ( )
0 commit comments