Skip to content

Commit e63ab8c

Browse files
authored
Fix uncaught errors reported when no workspace is open (intersystems-community#1008)
1 parent 122741f commit e63ab8c

File tree

4 files changed

+40
-3
lines changed

4 files changed

+40
-3
lines changed

src/commands/export.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ export async function exportList(files: string[], workspaceFolder: string, names
193193
}
194194
const { atelier, folder, addCategory, map } = config("export", workspaceFolder);
195195

196+
if (!workspaceFolder) {
197+
// No workspace folders are open
198+
return;
199+
}
196200
const root = [
197201
uriOfWorkspaceFolder(workspaceFolder).fsPath,
198202
typeof folder === "string" && folder.length ? folder : null,

src/extension.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,10 @@ export async function activate(context: vscode.ExtensionContext): Promise<any> {
943943
.map(async (f) => {
944944
// Determine the file name
945945
const workspace = workspaceFolderOfUri(f);
946+
if (!workspace) {
947+
// No workspace folders are open
948+
return null;
949+
}
946950
const workspacePath = uriOfWorkspaceFolder(workspace).fsPath;
947951
const filePathNoWorkspaceArr = f.fsPath.replace(workspacePath + path.sep, "").split(path.sep);
948952
const { folder, addCategory } = config("export", workspace);

src/providers/DocumentContentProvider.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class DocumentContentProvider implements vscode.TextDocumentContentProvid
3434
public static getAsFile(name: string, workspaceFolder: string): string {
3535
const { atelier, folder, addCategory, map } = config("export", workspaceFolder);
3636

37+
if (!workspaceFolder) {
38+
return;
39+
}
3740
const root = [uriOfWorkspaceFolder(workspaceFolder).fsPath, folder].join(path.sep);
3841
const fileName = getFileName(root, name, atelier, addCategory, map);
3942
if (fs.existsSync(fileName)) {
@@ -44,6 +47,9 @@ export class DocumentContentProvider implements vscode.TextDocumentContentProvid
4447
public static getAsFolder(name: string, workspaceFolder: string, category?: string): string {
4548
const { atelier, folder, addCategory } = config("export", workspaceFolder);
4649

50+
if (!workspaceFolder) {
51+
return;
52+
}
4753
const root = [uriOfWorkspaceFolder(workspaceFolder).fsPath, folder].join(path.sep);
4854
const folderName = getFolderName(root, name, atelier, addCategory ? category : null);
4955
if (fs.existsSync(folderName)) {
@@ -71,7 +77,7 @@ export class DocumentContentProvider implements vscode.TextDocumentContentProvid
7177
wFolderUri = uriOfWorkspaceFolder(workspaceFolder);
7278
}
7379
let uri: vscode.Uri;
74-
if (wFolderUri.scheme === FILESYSTEM_SCHEMA || wFolderUri.scheme === FILESYSTEM_READONLY_SCHEMA) {
80+
if (wFolderUri && (wFolderUri.scheme === FILESYSTEM_SCHEMA || wFolderUri.scheme === FILESYSTEM_READONLY_SCHEMA)) {
7581
const flat = new URLSearchParams(wFolderUri.query).get("flat") == "1";
7682
const fileExt = name.split(".").pop();
7783
const fileName = name

src/utils/index.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export function cspAppsForUri(uri: vscode.Uri): string[] {
7373
* @param fileExt The extension of the file.
7474
*/
7575
function 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
*/
103107
export 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 {
136144
export 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

Comments
 (0)