@@ -73,6 +73,10 @@ export function cspAppsForUri(uri: vscode.Uri): string[] {
7373 * @param fileExt The extension of the file.
7474 */
7575function getServerDocName ( localPath : string , workspace : string , fileExt : string ) : string {
76+ if ( ! workspace ) {
77+ // No workspace folders are open
78+ return null ;
79+ }
7680 const workspacePath = uriOfWorkspaceFolder ( workspace ) . fsPath ;
7781 const filePathNoWorkspaceArr = localPath . replace ( workspacePath + path . sep , "" ) . split ( path . sep ) ;
7882 const uri = vscode . Uri . file ( localPath ) ;
@@ -102,6 +106,10 @@ function getServerDocName(localPath: string, workspace: string, fileExt: string)
102106 */
103107export function isImportableLocalFile ( file : vscode . TextDocument ) : boolean {
104108 const workspace = currentWorkspaceFolder ( file ) ;
109+ if ( workspace == "" ) {
110+ // No workspace folders are open
111+ return false ;
112+ }
105113 const workspacePath = uriOfWorkspaceFolder ( workspace ) . fsPath ;
106114 const filePathNoWorkspaceArr = file . fileName . replace ( workspacePath + path . sep , "" ) . split ( path . sep ) ;
107115 const isCSP = cspAppsForUri ( file . uri ) . findIndex ( ( cspApp ) => file . uri . path . includes ( cspApp + "/" ) ) != - 1 ;
@@ -136,6 +144,10 @@ export function isImportableLocalFile(file: vscode.TextDocument): boolean {
136144export function currentFileFromContent ( fileName : string , content : string ) : CurrentFile {
137145 const uri = vscode . Uri . file ( fileName ) ;
138146 const workspaceFolder = workspaceFolderOfUri ( uri ) ;
147+ if ( ! workspaceFolder ) {
148+ // No workspace folders are open
149+ return null ;
150+ }
139151 const fileExt = fileName . split ( "." ) . pop ( ) . toLowerCase ( ) ;
140152 let name = "" ;
141153 let ext = "" ;
@@ -333,6 +345,8 @@ export function currentWorkspaceFolder(document?: vscode.TextDocument): string {
333345 // document might not be part of the workspace (e.g. the XXX.code-workspace JSON file)
334346 if ( folder ) {
335347 return folder ;
348+ } else {
349+ return "" ;
336350 }
337351 }
338352 const firstFolder =
@@ -361,7 +375,11 @@ export function workspaceFolderOfUri(uri: vscode.Uri): string {
361375 return "" ;
362376}
363377
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+ }
365383 return (
366384 vscode . workspace . workspaceFolders . find ( ( el ) : boolean => el . name . toLowerCase ( ) === workspaceFolder . toLowerCase ( ) ) ||
367385 vscode . workspace . workspaceFolders . find ( ( el ) : boolean => el . uri . authority == workspaceFolder )
@@ -394,7 +412,12 @@ export async function portFromDockerCompose(): Promise<{ port: number; docker: b
394412 }
395413
396414 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 ;
398421 const workspaceRootPath = vscode . workspace . workspaceFolders [ 0 ] . uri . fsPath ;
399422
400423 const cwd : string = await fileExists ( vscode . Uri . file ( path . join ( workspaceFolderPath , file ) ) ) . then ( ( exists ) => {
0 commit comments