Skip to content

Commit 8286850

Browse files
authored
Merge pull request #230 from jcreedcmu/jcreed/quick-query-bug
Fix weird behavior of Quick Query in non-multi-root workspaces
2 parents 1a4d729 + 9aaffb9 commit 8286850

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

extensions/ql-vscode/src/quick-query.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { UserCancellationException } from './run-queries';
1212

1313
const QUICK_QUERIES_DIR_NAME = 'quick-queries';
1414
const QUICK_QUERY_QUERY_NAME = 'quick-query.ql';
15+
const QUICK_QUERY_WORKSPACE_FOLDER_NAME = 'Quick Queries';
1516

1617
export function isQuickQueryPath(queryPath: string): boolean {
1718
return path.basename(queryPath) === QUICK_QUERY_QUERY_NAME;
@@ -75,6 +76,17 @@ async function getQuickQueriesDir(ctx: ExtensionContext): Promise<string> {
7576
export async function displayQuickQuery(ctx: ExtensionContext, cliServer: CodeQLCliServer, databaseUI: DatabaseUI) {
7677
try {
7778

79+
const workspaceFolders = workspace.workspaceFolders || [];
80+
const queriesDir = await getQuickQueriesDir(ctx);
81+
82+
function updateQuickQueryDir(index: number, len: number) {
83+
workspace.updateWorkspaceFolders(
84+
index,
85+
len,
86+
{ uri: Uri.file(queriesDir), name: QUICK_QUERY_WORKSPACE_FOLDER_NAME }
87+
);
88+
}
89+
7890
// If there is already a quick query open, don't clobber it, just
7991
// show it.
8092
const existing = workspace.textDocuments.find(doc => path.basename(doc.uri.fsPath) === QUICK_QUERY_QUERY_NAME);
@@ -83,19 +95,29 @@ export async function displayQuickQuery(ctx: ExtensionContext, cliServer: CodeQL
8395
return;
8496
}
8597

86-
const queriesDir = await getQuickQueriesDir(ctx);
87-
88-
// We need this folder in workspace folders so the language server
89-
// knows how to find its qlpack.yml
90-
if (workspace.workspaceFolders === undefined
91-
|| !workspace.workspaceFolders.some(folder => folder.uri.fsPath === queriesDir)) {
92-
workspace.updateWorkspaceFolders(
93-
(workspace.workspaceFolders || []).length,
94-
0,
95-
{ uri: Uri.file(queriesDir), name: "Quick Queries" }
96-
);
98+
// We need to have a multi-root workspace to make quick query work
99+
// at all. Changing the workspace from single-root to multi-root
100+
// causes a restart of the whole extension host environment, so we
101+
// basically can't do anything that survives that restart.
102+
//
103+
// So if we are currently in a single-root workspace (of which the
104+
// only reliable signal appears to be `workspace.workspaceFile`
105+
// being undefined) just let the user know that they're in for a
106+
// restart.
107+
if (workspace.workspaceFile === undefined) {
108+
const makeMultiRoot = await helpers.showBinaryChoiceDialog('Quick query requires multiple folders in the workspace. Reload workspace as multi-folder workspace?');
109+
if (makeMultiRoot) {
110+
updateQuickQueryDir(workspaceFolders.length, 0);
111+
}
112+
return;
97113
}
98114

115+
const index = workspaceFolders.findIndex(folder => folder.name === QUICK_QUERY_WORKSPACE_FOLDER_NAME)
116+
if (index === -1)
117+
updateQuickQueryDir(workspaceFolders.length, 0);
118+
else
119+
updateQuickQueryDir(index, 1);
120+
99121
// We're going to infer which qlpack to use from the current database
100122
const dbItem = await databaseUI.getDatabaseItem();
101123
if (dbItem === undefined) {

0 commit comments

Comments
 (0)