Skip to content

Commit b481441

Browse files
Emit more relevant error message when failing to add source folder (#1021)
* Emit more relevant error message when failing to add source folder Fixes #1020 * Update changelog * Clarify changelog and error message Co-authored-by: Shati Patel <42641846+shati-patel@users.noreply.github.com> Co-authored-by: Shati Patel <42641846+shati-patel@users.noreply.github.com>
1 parent 6a1d1a4 commit b481441

2 files changed

Lines changed: 45 additions & 23 deletions

File tree

extensions/ql-vscode/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## [UNRELEASED]
44

5+
- 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)
6+
57
## 1.5.7 - 23 November 2021
68

79
- Fix the _CodeQL: Open Referenced File_ command for Windows systems. [#979](https://github.com/github/vscode-codeql/pull/979)

extensions/ql-vscode/src/databases.ts

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export interface DatabaseItem {
258258
* Returns the root uri of the virtual filesystem for this database's source archive,
259259
* as displayed in the filesystem explorer.
260260
*/
261-
getSourceArchiveExplorerUri(): vscode.Uri | undefined;
261+
getSourceArchiveExplorerUri(): vscode.Uri;
262262

263263
/**
264264
* Holds if `uri` belongs to this database's source archive.
@@ -274,6 +274,11 @@ export interface DatabaseItem {
274274
* Gets the state of this database, to be persisted in the workspace state.
275275
*/
276276
getPersistedState(): PersistedDatabaseItem;
277+
278+
/**
279+
* Verifies that this database item has a zipped source folder. Returns an error message if it does not.
280+
*/
281+
verifyZippedSources(): string | undefined;
277282
}
278283

279284
export enum DatabaseEventKind {
@@ -459,13 +464,26 @@ export class DatabaseItemImpl implements DatabaseItem {
459464
/**
460465
* Returns the root uri of the virtual filesystem for this database's source archive.
461466
*/
462-
public getSourceArchiveExplorerUri(): vscode.Uri | undefined {
467+
public getSourceArchiveExplorerUri(): vscode.Uri {
463468
const sourceArchive = this.sourceArchive;
464-
if (sourceArchive === undefined || !sourceArchive.fsPath.endsWith('.zip'))
465-
return undefined;
469+
if (sourceArchive === undefined || !sourceArchive.fsPath.endsWith('.zip')) {
470+
throw new Error(this.verifyZippedSources());
471+
}
466472
return encodeArchiveBasePath(sourceArchive.fsPath);
467473
}
468474

475+
public verifyZippedSources(): string | undefined {
476+
const sourceArchive = this.sourceArchive;
477+
if (sourceArchive === undefined) {
478+
return `${this.name} has no source archive.`;
479+
}
480+
481+
if (!sourceArchive.fsPath.endsWith('.zip')) {
482+
return `${this.name} has a source folder that is unzipped.`;
483+
}
484+
return;
485+
}
486+
469487
/**
470488
* Holds if `uri` belongs to this database's source archive.
471489
*/
@@ -603,26 +621,28 @@ export class DatabaseManager extends DisposableObject {
603621
// This is undesirable, as we might be adding and removing many
604622
// workspace folders as the user adds and removes databases.
605623
const end = (vscode.workspace.workspaceFolders || []).length;
606-
const uri = item.getSourceArchiveExplorerUri();
607-
if (uri === undefined) {
608-
void logger.log(`Couldn't obtain file explorer uri for ${item.name}`);
624+
625+
const msg = item.verifyZippedSources();
626+
if (msg) {
627+
void logger.log(`Could not add source folder because ${msg}`);
628+
return;
609629
}
610-
else {
611-
void logger.log(`Adding workspace folder for ${item.name} source archive at index ${end}`);
612-
if ((vscode.workspace.workspaceFolders || []).length < 2) {
613-
// Adding this workspace folder makes the workspace
614-
// multi-root, which may surprise the user. Let them know
615-
// we're doing this.
616-
void vscode.window.showInformationMessage(`Adding workspace folder for source archive of database ${item.name}.`);
617-
}
618-
vscode.workspace.updateWorkspaceFolders(end, 0, {
619-
name: `[${item.name} source archive]`,
620-
uri,
621-
});
622-
// vscode api documentation says we must to wait for this event
623-
// between multiple `updateWorkspaceFolders` calls.
624-
await eventFired(vscode.workspace.onDidChangeWorkspaceFolders);
630+
631+
const uri = item.getSourceArchiveExplorerUri();
632+
void logger.log(`Adding workspace folder for ${item.name} source archive at index ${end}`);
633+
if ((vscode.workspace.workspaceFolders || []).length < 2) {
634+
// Adding this workspace folder makes the workspace
635+
// multi-root, which may surprise the user. Let them know
636+
// we're doing this.
637+
void vscode.window.showInformationMessage(`Adding workspace folder for source archive of database ${item.name}.`);
625638
}
639+
vscode.workspace.updateWorkspaceFolders(end, 0, {
640+
name: `[${item.name} source archive]`,
641+
uri,
642+
});
643+
// vscode api documentation says we must to wait for this event
644+
// between multiple `updateWorkspaceFolders` calls.
645+
await eventFired(vscode.workspace.onDidChangeWorkspaceFolders);
626646
}
627647
}
628648

@@ -732,7 +752,7 @@ export class DatabaseManager extends DisposableObject {
732752
this.updatePersistedCurrentDatabaseItem();
733753

734754
await vscode.commands.executeCommand('setContext', 'codeQL.currentDatabaseItem', item?.name);
735-
755+
736756
this._onDidChangeCurrentDatabaseItem.fire({
737757
item,
738758
kind: DatabaseEventKind.Change

0 commit comments

Comments
 (0)