@@ -73,6 +73,10 @@ export function cspAppsForUri(uri: vscode.Uri): string[] {
73
73
* @param fileExt The extension of the file.
74
74
*/
75
75
function getServerDocName ( localPath : string , workspace : string , fileExt : string ) : string {
76
+ if ( ! workspace ) {
77
+ // No workspace folders are open
78
+ return null ;
79
+ }
76
80
const workspacePath = uriOfWorkspaceFolder ( workspace ) . fsPath ;
77
81
const filePathNoWorkspaceArr = localPath . replace ( workspacePath + path . sep , "" ) . split ( path . sep ) ;
78
82
const uri = vscode . Uri . file ( localPath ) ;
@@ -102,6 +106,10 @@ function getServerDocName(localPath: string, workspace: string, fileExt: string)
102
106
*/
103
107
export function isImportableLocalFile ( file : vscode . TextDocument ) : boolean {
104
108
const workspace = currentWorkspaceFolder ( file ) ;
109
+ if ( workspace == "" ) {
110
+ // No workspace folders are open
111
+ return false ;
112
+ }
105
113
const workspacePath = uriOfWorkspaceFolder ( workspace ) . fsPath ;
106
114
const filePathNoWorkspaceArr = file . fileName . replace ( workspacePath + path . sep , "" ) . split ( path . sep ) ;
107
115
const isCSP = cspAppsForUri ( file . uri ) . findIndex ( ( cspApp ) => file . uri . path . includes ( cspApp + "/" ) ) != - 1 ;
@@ -136,6 +144,10 @@ export function isImportableLocalFile(file: vscode.TextDocument): boolean {
136
144
export function currentFileFromContent ( fileName : string , content : string ) : CurrentFile {
137
145
const uri = vscode . Uri . file ( fileName ) ;
138
146
const workspaceFolder = workspaceFolderOfUri ( uri ) ;
147
+ if ( ! workspaceFolder ) {
148
+ // No workspace folders are open
149
+ return null ;
150
+ }
139
151
const fileExt = fileName . split ( "." ) . pop ( ) . toLowerCase ( ) ;
140
152
let name = "" ;
141
153
let ext = "" ;
@@ -333,6 +345,8 @@ export function currentWorkspaceFolder(document?: vscode.TextDocument): string {
333
345
// document might not be part of the workspace (e.g. the XXX.code-workspace JSON file)
334
346
if ( folder ) {
335
347
return folder ;
348
+ } else {
349
+ return "" ;
336
350
}
337
351
}
338
352
const firstFolder =
@@ -361,7 +375,11 @@ export function workspaceFolderOfUri(uri: vscode.Uri): string {
361
375
return "" ;
362
376
}
363
377
364
- export function uriOfWorkspaceFolder ( workspaceFolder : string = currentWorkspaceFolder ( ) ) : vscode . Uri {
378
+ export function uriOfWorkspaceFolder ( workspaceFolder : string = currentWorkspaceFolder ( ) ) : vscode . Uri | undefined {
379
+ if ( ! workspaceFolder || ! vscode . workspace . workspaceFolders || vscode . workspace . workspaceFolders . length == 0 ) {
380
+ // There are no workspace folders open
381
+ return undefined ;
382
+ }
365
383
return (
366
384
vscode . workspace . workspaceFolders . find ( ( el ) : boolean => el . name . toLowerCase ( ) === workspaceFolder . toLowerCase ( ) ) ||
367
385
vscode . workspace . workspaceFolders . find ( ( el ) : boolean => el . uri . authority == workspaceFolder )
@@ -394,7 +412,12 @@ export async function portFromDockerCompose(): Promise<{ port: number; docker: b
394
412
}
395
413
396
414
const result = { port : null , docker : true , service } ;
397
- const workspaceFolderPath = uriOfWorkspaceFolder ( ) . fsPath ;
415
+ const workspaceFolder = uriOfWorkspaceFolder ( ) ;
416
+ if ( ! workspaceFolder ) {
417
+ // No workspace folders are open
418
+ return { docker : false , port : null } ;
419
+ }
420
+ const workspaceFolderPath = workspaceFolder . fsPath ;
398
421
const workspaceRootPath = vscode . workspace . workspaceFolders [ 0 ] . uri . fsPath ;
399
422
400
423
const cwd : string = await fileExists ( vscode . Uri . file ( path . join ( workspaceFolderPath , file ) ) ) . then ( ( exists ) => {
0 commit comments