Skip to content

Commit 4a65b6a

Browse files
committed
Ensure anonymous and scope-less packs can be used as remote queries
When we generate the synthetic pack, just ensure that there is a valid name.
1 parent 28c76be commit 4a65b6a

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export async function displayQuickQuery(
118118
// Only rewrite the qlpack file if the database has changed
119119
if (shouldRewrite) {
120120
const quickQueryQlpackYaml: any = {
121-
name: 'quick-query',
121+
name: 'vscode/quick-query',
122122
version: '1.0.0',
123123
libraryPathDependencies: [qlpack]
124124
};

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ async function generateQueryPack(cliServer: cli.CodeQLCliServer, queryFile: stri
119119
})
120120
});
121121

122+
// ensure the qlpack.yml has a valid name
123+
await ensureQueryPackName(queryPackDir);
124+
122125
void logger.log(`Copied ${copiedCount} files to ${queryPackDir}`);
123126

124127
language = await findLanguage(cliServer, Uri.file(targetQueryFileName));
@@ -161,6 +164,25 @@ async function generateQueryPack(cliServer: cli.CodeQLCliServer, queryFile: stri
161164
};
162165
}
163166

167+
/**
168+
* Ensure that the qlpack.yml has a valid name. For local purposes,
169+
* Anonymous packs and names that are not prefixed by a scope (ie `<foo>/`)
170+
* are sufficient. But in order to create a pack, the name must be prefixed.
171+
*
172+
* @param queryPackDir the directory containing the query pack.
173+
*/
174+
async function ensureQueryPackName(queryPackDir: string) {
175+
const pack = yaml.safeLoad(await fs.readFile(path.join(queryPackDir, 'qlpack.yml'), 'utf8')) as { name: string; };
176+
if (!pack.name || !pack.name.includes('/')) {
177+
if (!pack.name) {
178+
pack.name = 'codeql-remote/query';
179+
} else if (!pack.name.includes('/')) {
180+
pack.name = `codeql-remote/${pack.name}`;
181+
}
182+
await fs.writeFile(path.join(queryPackDir, 'qlpack.yml'), yaml.safeDump(pack));
183+
}
184+
}
185+
164186
async function createRemoteQueriesTempDirectory() {
165187
const remoteQueryDir = await tmp.dir({ dir: tmpDir.name, unsafeCleanup: true });
166188
const queryPackDir = path.join(remoteQueryDir.path, 'query-pack');

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ describe('Remote queries', function() {
141141
expect(fs.existsSync(path.join(queryPackDir, 'not-in-pack.ql'))).to.be.false;
142142

143143
// the compiled pack
144-
const compiledPackDir = path.join(queryPackDir, '.codeql/pack/codeql-remote/query/1.0.0/');
144+
const compiledPackDir = path.join(queryPackDir, '.codeql/pack/codeql-remote/query/0.0.0/');
145145
printDirectoryContents(compiledPackDir);
146146
expect(fs.existsSync(path.join(compiledPackDir, 'query.ql'))).to.be.true;
147147
expect(fs.existsSync(path.join(compiledPackDir, 'qlpack.yml'))).to.be.true;
@@ -155,7 +155,7 @@ describe('Remote queries', function() {
155155
// should have generated a correct qlpack file
156156
const qlpackContents: any = yaml.safeLoad(fs.readFileSync(path.join(compiledPackDir, 'qlpack.yml'), 'utf8'));
157157
expect(qlpackContents.name).to.equal('codeql-remote/query');
158-
expect(qlpackContents.version).to.equal('1.0.0');
158+
expect(qlpackContents.version).to.equal('0.0.0');
159159
expect(qlpackContents.dependencies?.['codeql/javascript-all']).to.equal('*');
160160

161161
// dependencies

0 commit comments

Comments
 (0)