Skip to content

Commit 2d01c33

Browse files
committed
Avoid running remote queries on v2.6.3 cli or earlier
Also: - Fix the count of copied files - A few typos
1 parent 42051f1 commit 2d01c33

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

extensions/ql-vscode/src/cli.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,11 @@ export class CliVersionConstraint {
11341134
*/
11351135
public static CLI_VERSION_PACK_PACKLIST_LAYOUT_CHANGE = new SemVer('2.7.1');
11361136

1137+
/**
1138+
* CLI version where remote queries are supported.
1139+
*/
1140+
public static CLI_VERSION_REMOTE_QUERIES = new SemVer('2.6.3');
1141+
11371142
constructor(private readonly cli: CodeQLCliServer) {
11381143
/**/
11391144
}
@@ -1177,4 +1182,9 @@ export class CliVersionConstraint {
11771182
async usesNewPackPacklistLayout() {
11781183
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_PACK_PACKLIST_LAYOUT_CHANGE);
11791184
}
1185+
1186+
async supportsRemoteQueries() {
1187+
return this.isVersionAtLeast(CliVersionConstraint.CLI_VERSION_REMOTE_QUERIES);
1188+
}
1189+
11801190
}

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: 10 additions & 3 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';
1313
import { setRemoteControllerRepo } 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

0 commit comments

Comments
 (0)