Skip to content

Commit e58201e

Browse files
committed
Ensure server uses a well-known query pack name
1 parent a9b9502 commit e58201e

File tree

2 files changed

+18
-32
lines changed

2 files changed

+18
-32
lines changed

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

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ interface QueriesResponse {
3939
*/
4040
const REPO_REGEX = /^(?:[a-zA-Z0-9]+-)*[a-zA-Z0-9]+\/[a-zA-Z0-9-_]+$/;
4141

42+
/**
43+
* Well-known names for the query pack used by the server.
44+
*/
45+
const QUERY_PACK_NAME = 'codeql-remote/query';
46+
4247
/**
4348
* Gets the repositories to run the query against.
4449
*/
@@ -127,9 +132,6 @@ async function generateQueryPack(cliServer: cli.CodeQLCliServer, queryFile: stri
127132
})
128133
});
129134

130-
// ensure the qlpack.yml has a valid name
131-
await ensureQueryPackName(queryPackDir);
132-
133135
void logger.log(`Copied ${copiedCount} files to ${queryPackDir}`);
134136

135137
language = await findLanguage(cliServer, Uri.file(targetQueryFileName));
@@ -144,7 +146,7 @@ async function generateQueryPack(cliServer: cli.CodeQLCliServer, queryFile: stri
144146
await fs.copy(queryFile, targetQueryFileName);
145147
void logger.log('Generating synthetic query pack');
146148
const syntheticQueryPack = {
147-
name: 'codeql-remote/query',
149+
name: QUERY_PACK_NAME,
148150
version: '0.0.0',
149151
dependencies: {
150152
[`codeql/${language}-all`]: '*',
@@ -156,8 +158,7 @@ async function generateQueryPack(cliServer: cli.CodeQLCliServer, queryFile: stri
156158
throw new UserCancellationException('Could not determine language.');
157159
}
158160

159-
// fix the default suite of the query pack dir
160-
await fixDefaultSuite(queryPackDir, packRelativePath);
161+
await ensureNameAndSuite(queryPackDir, packRelativePath);
161162

162163
const bundlePath = await getPackedBundlePath(queryPackDir);
163164
void logger.log(`Compiling and bundling query pack from ${queryPackDir} to ${bundlePath}. (This may take a while.)`);
@@ -171,25 +172,6 @@ async function generateQueryPack(cliServer: cli.CodeQLCliServer, queryFile: stri
171172
};
172173
}
173174

174-
/**
175-
* Ensure that the qlpack.yml has a valid name. For local purposes,
176-
* Anonymous packs and names that are not prefixed by a scope (ie `<foo>/`)
177-
* are sufficient. But in order to create a pack, the name must be prefixed.
178-
*
179-
* @param queryPackDir the directory containing the query pack.
180-
*/
181-
async function ensureQueryPackName(queryPackDir: string) {
182-
const pack = yaml.safeLoad(await fs.readFile(path.join(queryPackDir, 'qlpack.yml'), 'utf8')) as { name: string; };
183-
if (!pack.name || !pack.name.includes('/')) {
184-
if (!pack.name) {
185-
pack.name = 'codeql-remote/query';
186-
} else if (!pack.name.includes('/')) {
187-
pack.name = `codeql-remote/${pack.name}`;
188-
}
189-
await fs.writeFile(path.join(queryPackDir, 'qlpack.yml'), yaml.safeDump(pack));
190-
}
191-
}
192-
193175
async function findPackRoot(queryFile: string): Promise<string> {
194176
// recursively find the directory containing qlpack.yml
195177
let dir = path.dirname(queryFile);
@@ -439,14 +421,18 @@ export async function attemptRerun(
439421
* Updates the default suite of the query pack. This is used to ensure
440422
* only the specified query is run.
441423
*
424+
* Also, ensure the query pack name is set to the name expected by the server.
425+
*
442426
* @param queryPackDir The directory containing the query pack
443427
* @param packRelativePath The relative path to the query pack from the root of the query pack
444428
*/
445-
async function fixDefaultSuite(queryPackDir: string, packRelativePath: string): Promise<void> {
429+
async function ensureNameAndSuite(queryPackDir: string, packRelativePath: string): Promise<void> {
446430
const packPath = path.join(queryPackDir, 'qlpack.yml');
447431
const qlpack = yaml.safeLoad(await fs.readFile(packPath, 'utf8')) as QlPack;
448432
delete qlpack.defaultSuiteFile;
449433

434+
qlpack.name = QUERY_PACK_NAME;
435+
450436
qlpack.defaultSuite = [{
451437
description: 'Query suite for remote query'
452438
}, {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe('Remote queries', function() {
106106
fs.existsSync(path.join(compiledPackDir, 'codeql-pack.lock.yml'))
107107
).to.be.true;
108108
expect(fs.existsSync(path.join(compiledPackDir, 'not-in-pack.ql'))).to.be.false;
109-
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', 'github/remote-query-pack', '0.0.0', await pathSerializationBroken());
109+
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', '0.0.0', await pathSerializationBroken());
110110

111111
// dependencies
112112
const libraryDir = path.join(compiledPackDir, '.codeql/libraries/codeql');
@@ -147,7 +147,7 @@ describe('Remote queries', function() {
147147
printDirectoryContents(compiledPackDir);
148148
expect(fs.existsSync(path.join(compiledPackDir, 'in-pack.ql'))).to.be.true;
149149
expect(fs.existsSync(path.join(compiledPackDir, 'qlpack.yml'))).to.be.true;
150-
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', 'codeql-remote/query', '0.0.0', await pathSerializationBroken());
150+
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'in-pack.ql', '0.0.0', await pathSerializationBroken());
151151

152152
// depending on the cli version, we should have one of these files
153153
expect(
@@ -202,7 +202,7 @@ describe('Remote queries', function() {
202202
expect(fs.existsSync(path.join(compiledPackDir, 'otherfolder/lib.qll'))).to.be.true;
203203
expect(fs.existsSync(path.join(compiledPackDir, 'subfolder/in-pack.ql'))).to.be.true;
204204
expect(fs.existsSync(path.join(compiledPackDir, 'qlpack.yml'))).to.be.true;
205-
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'subfolder/in-pack.ql', 'github/remote-query-pack', '0.0.0', await pathSerializationBroken());
205+
verifyQlPack(path.join(compiledPackDir, 'qlpack.yml'), 'subfolder/in-pack.ql', '0.0.0', await pathSerializationBroken());
206206

207207
// depending on the cli version, we should have one of these files
208208
expect(
@@ -212,7 +212,7 @@ describe('Remote queries', function() {
212212
expect(fs.existsSync(path.join(compiledPackDir, 'not-in-pack.ql'))).to.be.false;
213213
// should have generated a correct qlpack file
214214
const qlpackContents: any = yaml.safeLoad(fs.readFileSync(path.join(compiledPackDir, 'qlpack.yml'), 'utf8'));
215-
expect(qlpackContents.name).to.equal('github/remote-query-pack');
215+
expect(qlpackContents.name).to.equal('codeql-remote/query');
216216
expect(qlpackContents.version).to.equal('0.0.0');
217217
expect(qlpackContents.dependencies?.['codeql/javascript-all']).to.equal('*');
218218

@@ -238,7 +238,7 @@ describe('Remote queries', function() {
238238
}
239239
});
240240

241-
function verifyQlPack(qlpackPath: string, queryPath: string, packName: string, packVersion: string, pathSerializationBroken: boolean) {
241+
function verifyQlPack(qlpackPath: string, queryPath: string, packVersion: string, pathSerializationBroken: boolean) {
242242
const qlPack = yaml.safeLoad(fs.readFileSync(qlpackPath, 'utf8')) as QlPack;
243243

244244
if (pathSerializationBroken) {
@@ -250,7 +250,7 @@ describe('Remote queries', function() {
250250
delete (qlPack as any).buildMetadata;
251251

252252
expect(qlPack).to.deep.equal({
253-
name: packName,
253+
name: 'codeql-remote/query',
254254
version: packVersion,
255255
dependencies: {
256256
'codeql/javascript-all': '*',

0 commit comments

Comments
 (0)