From 5c13876968ecb3d57f9cbf1d396feb0745ea32e6 Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Thu, 25 Nov 2021 11:14:24 -0800 Subject: [PATCH 1/3] Emit more relevant error message when failing to add source folder Fixes #1020 --- extensions/ql-vscode/src/databases.ts | 66 +++++++++++++++++---------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/extensions/ql-vscode/src/databases.ts b/extensions/ql-vscode/src/databases.ts index 15a90d99c38..e732cbf5263 100644 --- a/extensions/ql-vscode/src/databases.ts +++ b/extensions/ql-vscode/src/databases.ts @@ -258,7 +258,7 @@ export interface DatabaseItem { * Returns the root uri of the virtual filesystem for this database's source archive, * as displayed in the filesystem explorer. */ - getSourceArchiveExplorerUri(): vscode.Uri | undefined; + getSourceArchiveExplorerUri(): vscode.Uri; /** * Holds if `uri` belongs to this database's source archive. @@ -274,6 +274,11 @@ export interface DatabaseItem { * Gets the state of this database, to be persisted in the workspace state. */ getPersistedState(): PersistedDatabaseItem; + + /** + * Verifies that this database item has a zipped source folder. Returns an error message if it does not. + */ + verifyZippedSources(): string | undefined; } export enum DatabaseEventKind { @@ -459,13 +464,26 @@ export class DatabaseItemImpl implements DatabaseItem { /** * Returns the root uri of the virtual filesystem for this database's source archive. */ - public getSourceArchiveExplorerUri(): vscode.Uri | undefined { + public getSourceArchiveExplorerUri(): vscode.Uri { const sourceArchive = this.sourceArchive; - if (sourceArchive === undefined || !sourceArchive.fsPath.endsWith('.zip')) - return undefined; + if (sourceArchive === undefined || !sourceArchive.fsPath.endsWith('.zip')) { + throw new Error(this.verifyZippedSources()); + } return encodeArchiveBasePath(sourceArchive.fsPath); } + public verifyZippedSources(): string | undefined { + const sourceArchive = this.sourceArchive; + if (sourceArchive === undefined) { + return `${this.name} has no source archive.`; + } + + if (!sourceArchive.fsPath.endsWith('.zip')) { + return `${this.name} has a source archive that is unzipped.`; + } + return; + } + /** * Holds if `uri` belongs to this database's source archive. */ @@ -603,26 +621,28 @@ export class DatabaseManager extends DisposableObject { // This is undesirable, as we might be adding and removing many // workspace folders as the user adds and removes databases. const end = (vscode.workspace.workspaceFolders || []).length; - const uri = item.getSourceArchiveExplorerUri(); - if (uri === undefined) { - void logger.log(`Couldn't obtain file explorer uri for ${item.name}`); + + const msg = item.verifyZippedSources(); + if (msg) { + void logger.log(`Could not add source folder because ${msg}`); + return; } - else { - void logger.log(`Adding workspace folder for ${item.name} source archive at index ${end}`); - if ((vscode.workspace.workspaceFolders || []).length < 2) { - // Adding this workspace folder makes the workspace - // multi-root, which may surprise the user. Let them know - // we're doing this. - void vscode.window.showInformationMessage(`Adding workspace folder for source archive of database ${item.name}.`); - } - vscode.workspace.updateWorkspaceFolders(end, 0, { - name: `[${item.name} source archive]`, - uri, - }); - // vscode api documentation says we must to wait for this event - // between multiple `updateWorkspaceFolders` calls. - await eventFired(vscode.workspace.onDidChangeWorkspaceFolders); + + const uri = item.getSourceArchiveExplorerUri(); + void logger.log(`Adding workspace folder for ${item.name} source archive at index ${end}`); + if ((vscode.workspace.workspaceFolders || []).length < 2) { + // Adding this workspace folder makes the workspace + // multi-root, which may surprise the user. Let them know + // we're doing this. + void vscode.window.showInformationMessage(`Adding workspace folder for source archive of database ${item.name}.`); } + vscode.workspace.updateWorkspaceFolders(end, 0, { + name: `[${item.name} source archive]`, + uri, + }); + // vscode api documentation says we must to wait for this event + // between multiple `updateWorkspaceFolders` calls. + await eventFired(vscode.workspace.onDidChangeWorkspaceFolders); } } @@ -732,7 +752,7 @@ export class DatabaseManager extends DisposableObject { this.updatePersistedCurrentDatabaseItem(); await vscode.commands.executeCommand('setContext', 'codeQL.currentDatabaseItem', item?.name); - + this._onDidChangeCurrentDatabaseItem.fire({ item, kind: DatabaseEventKind.Change From 281b674a627017116e93bcbb0e1daa6c425e62ce Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Thu, 25 Nov 2021 11:16:19 -0800 Subject: [PATCH 2/3] Update changelog --- extensions/ql-vscode/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/extensions/ql-vscode/CHANGELOG.md b/extensions/ql-vscode/CHANGELOG.md index 16b7fc2cca3..918a43e7eef 100644 --- a/extensions/ql-vscode/CHANGELOG.md +++ b/extensions/ql-vscode/CHANGELOG.md @@ -2,6 +2,8 @@ ## [UNRELEASED] +- Emit more explicit error message when a user tries to add unzipped sources to the workspace. [1021](https://github.com/github/vscode-codeql/pull/1021) + ## 1.5.7 - 23 November 2021 - Fix the _CodeQL: Open Referenced File_ command for Windows systems. [#979](https://github.com/github/vscode-codeql/pull/979) From 7c038ab1d7010b0e39dc9f6e552261995afbad5e Mon Sep 17 00:00:00 2001 From: Andrew Eisenberg Date: Mon, 29 Nov 2021 08:18:59 -0800 Subject: [PATCH 3/3] Clarify changelog and error message Co-authored-by: Shati Patel <42641846+shati-patel@users.noreply.github.com> --- extensions/ql-vscode/CHANGELOG.md | 2 +- extensions/ql-vscode/src/databases.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/ql-vscode/CHANGELOG.md b/extensions/ql-vscode/CHANGELOG.md index 918a43e7eef..6453575d756 100644 --- a/extensions/ql-vscode/CHANGELOG.md +++ b/extensions/ql-vscode/CHANGELOG.md @@ -2,7 +2,7 @@ ## [UNRELEASED] -- Emit more explicit error message when a user tries to add unzipped sources to the workspace. [1021](https://github.com/github/vscode-codeql/pull/1021) +- Emit a more explicit error message when a user tries to add a database with an unzipped source folder to the workspace. [1021](https://github.com/github/vscode-codeql/pull/1021) ## 1.5.7 - 23 November 2021 diff --git a/extensions/ql-vscode/src/databases.ts b/extensions/ql-vscode/src/databases.ts index e732cbf5263..92bbad0e779 100644 --- a/extensions/ql-vscode/src/databases.ts +++ b/extensions/ql-vscode/src/databases.ts @@ -479,7 +479,7 @@ export class DatabaseItemImpl implements DatabaseItem { } if (!sourceArchive.fsPath.endsWith('.zip')) { - return `${this.name} has a source archive that is unzipped.`; + return `${this.name} has a source folder that is unzipped.`; } return; }