Skip to content

Commit 903f5db

Browse files
committed
Avoid running remote queries on v2.6.3 cli or earlier
Also: - Fix the count of copied files - A few typos - Ensure the correct settings are applied for remote queries before running tests.
1 parent 42051f1 commit 903f5db

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed

extensions/ql-vscode/src/cli.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -822,12 +822,15 @@ export class CodeQLCliServer implements Disposable {
822822

823823
async packPacklist(dir: string, includeQueries: boolean): Promise<string[]> {
824824
const args = includeQueries ? [dir] : ['--no-include-queries', dir];
825-
const results = await this.runJsonCodeQlCliCommand(['pack', 'packlist'], args, 'Generating the pack list');
825+
// since 2.7.1, packlist returns an object with a "paths" property that is a list of packs.
826+
// previous versions return a list of packs.
827+
const results: { paths: string[] } | string[] = await this.runJsonCodeQlCliCommand(['pack', 'packlist'], args, 'Generating the pack list');
826828

827-
if (await this.cliConstraints.usesNewPackPacklistLayout()) {
828-
return (results as { paths: string[] }).paths;
829+
// Once we no longer need to support 2.7.0 or earlier, we can remove this and assume all versions return an object.
830+
if ('paths' in results) {
831+
return results.paths;
829832
} else {
830-
return results as string[];
833+
return results;
831834
}
832835
}
833836

@@ -1130,9 +1133,9 @@ export class CliVersionConstraint {
11301133
public static CLI_VERSION_WITH_NO_PRECOMPILE = new SemVer('2.7.1');
11311134

11321135
/**
1133-
* CLI version where `pack packlist` layout changed from array to object
1136+
* CLI version where remote queries are supported.
11341137
*/
1135-
public static CLI_VERSION_PACK_PACKLIST_LAYOUT_CHANGE = new SemVer('2.7.1');
1138+
public static CLI_VERSION_REMOTE_QUERIES = new SemVer('2.6.3');
11361139

11371140
constructor(private readonly cli: CodeQLCliServer) {
11381141
/**/
@@ -1174,7 +1177,8 @@ export class CliVersionConstraint {
11741177
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_WITH_NO_PRECOMPILE);
11751178
}
11761179

1177-
async usesNewPackPacklistLayout() {
1178-
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_PACK_PACKLIST_LAYOUT_CHANGE);
1180+
async supportsRemoteQueries() {
1181+
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_REMOTE_QUERIES);
11791182
}
1183+
11801184
}

extensions/ql-vscode/src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,10 @@ export function getRemoteRepositoryLists(): Record<string, string[]> | undefined
314314
return REMOTE_REPO_LISTS.getValue<Record<string, string[]>>() || undefined;
315315
}
316316

317+
export async function setRemoteRepositoryLists(lists: Record<string, string[]> | undefined) {
318+
await REMOTE_REPO_LISTS.updateValue(lists, ConfigurationTarget.Global);
319+
}
320+
317321
/**
318322
* The name of the "controller" repository that you want to use with the "Run Remote query" command.
319323
* Note: This command is only available for internal users.

extensions/ql-vscode/src/run-remote-query.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,27 @@ async function generateQueryPack(cliServer: cli.CodeQLCliServer, queryFile: stri
9898

9999
// also copy the lock file (either new name or old name) and the query file itself. These are not included in the packlist.
100100
[path.join(originalPackRoot, 'qlpack.lock.yml'), path.join(originalPackRoot, 'codeql-pack.lock.yml'), queryFile]
101-
.forEach(aboslutePath => {
102-
if (aboslutePath) {
103-
toCopy.push(aboslutePath);
101+
.forEach(absolutePath => {
102+
if (absolutePath) {
103+
toCopy.push(absolutePath);
104104
}
105105
});
106-
void logger.log(`Copying ${toCopy.length} files to ${queryPackDir}`);
106+
107+
let copiedCount = 0;
107108
await fs.copy(originalPackRoot, queryPackDir, {
108109
filter: (file: string) =>
109110
// copy file if it is in the packlist, or it is a parent directory of a file in the packlist
110-
!!toCopy.find(f => f === file || f.startsWith(file + path.sep)
111-
)
111+
!!toCopy.find(f => {
112+
const matches = f === file || f.startsWith(file + path.sep);
113+
if (matches) {
114+
copiedCount++;
115+
}
116+
return matches;
117+
})
112118
});
119+
120+
void logger.log(`Copied ${copiedCount} files to ${queryPackDir}`);
121+
113122
language = await findLanguage(cliServer, Uri.file(targetQueryFileName));
114123

115124
} else {
@@ -173,6 +182,11 @@ export async function runRemoteQuery(
173182
progress: ProgressCallback,
174183
token: CancellationToken
175184
): Promise<void | string> {
185+
if (!(await cliServer.cliConstraints.supportsRemoteQueries())) {
186+
throw new Error(`Remote queries are not supported by this version of CodeQL. Please upgrade to v${cli.CliVersionConstraint.CLI_VERSION_REMOTE_QUERIES
187+
} or later.`);
188+
}
189+
176190
const { remoteQueryDir, queryPackDir } = await createRemoteQueriesTempDirectory();
177191
try {
178192
if (!uri?.fsPath.endsWith('.ql')) {
@@ -214,7 +228,6 @@ export async function runRemoteQuery(
214228
}
215229

216230
if (!repositories || repositories.length === 0) {
217-
// No error message needed, since `getRepositories` already displays one.
218231
throw new UserCancellationException('No repositories to query.');
219232
}
220233

extensions/ql-vscode/src/vscode-tests/cli-integration/run-remote-query.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import * as yaml from 'js-yaml';
88

99
import { runRemoteQuery } from '../../run-remote-query';
1010
import { Credentials } from '../../authentication';
11-
import { CodeQLCliServer } from '../../cli';
11+
import { CliVersionConstraint, CodeQLCliServer } from '../../cli';
1212
import { CodeQLExtensionInterface } from '../../extension';
13-
import { setRemoteControllerRepo } from '../../config';
13+
import { setRemoteControllerRepo, setRemoteRepositoryLists } from '../../config';
1414
import { UserCancellationException } from '../../commandRunner';
1515

16-
describe('Remote queries', function() {
16+
describe.only('Remote queries', function() {
1717
const baseDir = path.join(__dirname, '../../../src/vscode-tests/cli-integration');
1818

1919
let sandbox: sinon.SinonSandbox;
@@ -27,7 +27,8 @@ describe('Remote queries', function() {
2727
let progress: sinon.SinonSpy;
2828
let showQuickPickSpy: sinon.SinonStub;
2929

30-
beforeEach(async () => {
30+
// use `function` so we have access to `this`
31+
beforeEach(async function() {
3132
sandbox = sinon.createSandbox();
3233

3334
const extension = await extensions.getExtension<CodeQLExtensionInterface | Record<string, never>>('GitHub.vscode-codeql')!.activate();
@@ -36,6 +37,12 @@ describe('Remote queries', function() {
3637
} else {
3738
throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.');
3839
}
40+
41+
if (!(await cli.cliConstraints.supportsRemoteQueries())) {
42+
console.log(`Remote queries are not supported on CodeQL CLI v${CliVersionConstraint.CLI_VERSION_REMOTE_QUERIES
43+
}. Skipping this test.`);
44+
this.skip();
45+
}
3946
credentials = {} as unknown as Credentials;
4047
token = {
4148
isCancellationRequested: false
@@ -48,7 +55,8 @@ describe('Remote queries', function() {
4855
.onSecondCall().resolves('javascript' as unknown as QuickPickItem);
4956

5057
// always run in the vscode-codeql repo
51-
void setRemoteControllerRepo('github/vscode-codeql');
58+
await setRemoteControllerRepo('github/vscode-codeql');
59+
await setRemoteRepositoryLists({ 'vscode-codeql': ['github/vscode-codeql'] });
5260
});
5361

5462
afterEach(() => {

0 commit comments

Comments
 (0)