Skip to content

Commit 69ba377

Browse files
committed
Invoke codeql pack install after adding a quick query
This ensures the pack lock file is in place after the quick query is generated.
1 parent 6ccbcd9 commit 69ba377

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

extensions/ql-vscode/src/cli.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,8 +917,12 @@ export class CodeQLCliServer implements Disposable {
917917
return this.runJsonCodeQlCliCommand(['pack', 'download'], packs, 'Downloading packs');
918918
}
919919

920-
async packInstall(dir: string) {
921-
return this.runJsonCodeQlCliCommand(['pack', 'install'], [dir], 'Installing pack dependencies');
920+
async packInstall(dir: string, forceUpdate = false) {
921+
const args = [dir];
922+
if (forceUpdate) {
923+
args.push('--mode', 'update');
924+
}
925+
return this.runJsonCodeQlCliCommand(['pack', 'install'], args, 'Installing pack dependencies');
922926
}
923927

924928
async packBundle(dir: string, workspaceFolders: string[], outputPath: string, precompile = true): Promise<void> {
@@ -1270,7 +1274,7 @@ export class CliVersionConstraint {
12701274
/**
12711275
* CLI version where the `resolve ml-models` subcommand was enhanced to work with packaging.
12721276
*/
1273-
public static CLI_VERSION_WITH_PRECISE_RESOLVE_ML_MODELS = new SemVer('2.10.0');
1277+
public static CLI_VERSION_WITH_PRECISE_RESOLVE_ML_MODELS = new SemVer('2.10.0');
12741278

12751279
/**
12761280
* CLI version where the `--old-eval-stats` option to the query server was introduced.

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ export async function displayQuickQuery(
121121
const quickQueryQlpackYaml: any = {
122122
name: 'vscode/quick-query',
123123
version: '1.0.0',
124-
libraryPathDependencies: [qlpack]
124+
dependencies: {
125+
[qlpack]: '*'
126+
}
125127
};
126128
await fs.writeFile(qlPackFile, QLPACK_FILE_HEADER + yaml.dump(quickQueryQlpackYaml), 'utf8');
127129
}
@@ -130,6 +132,11 @@ export async function displayQuickQuery(
130132
await fs.writeFile(qlFile, getInitialQueryContents(dbItem.language, dbscheme), 'utf8');
131133
}
132134

135+
if (shouldRewrite) {
136+
await cliServer.clearCache();
137+
await cliServer.packInstall(queriesDir, true);
138+
}
139+
133140
await Window.showTextDocument(await workspace.openTextDocument(qlFile));
134141
} catch (e) {
135142
if (e instanceof ResponseError && e.code == ErrorCodes.RequestCancelled) {
@@ -145,5 +152,5 @@ async function checkShouldRewrite(qlPackFile: string, newDependency: string) {
145152
return true;
146153
}
147154
const qlPackContents: any = yaml.load(await fs.readFile(qlPackFile, 'utf8'));
148-
return qlPackContents.libraryPathDependencies?.[0] !== newDependency;
155+
return !qlPackContents.dependencies?.[newDependency];
149156
}

extensions/ql-vscode/src/vscode-tests/cli-integration/queries.test.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe('Queries', function() {
3838
let ctx: ExtensionContext;
3939

4040
let qlpackFile: string;
41+
let qlpackLockFile: string;
4142
let qlFile: string;
4243

4344

@@ -53,6 +54,7 @@ describe('Queries', function() {
5354
cli.quiet = true;
5455
ctx = extension.ctx;
5556
qlpackFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.yml`;
57+
qlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/codeql-pack.lock.yml`;
5658
qlFile = `${ctx.storageUri?.fsPath}/quick-queries/quick-query.ql`;
5759
} else {
5860
throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.');
@@ -153,15 +155,23 @@ describe('Queries', function() {
153155
fs.readFileSync(qlpackFile, 'utf8')
154156
);
155157
// Should have chosen the js libraries
156-
expect(qlpackContents.libraryPathDependencies[0]).to.include('javascript');
158+
expect(qlpackContents.dependencies['codeql/javascript-all']).to.eq('*');
159+
160+
// Should also have a codeql-pack.lock.yml file
161+
const qlpackLock: any = await yaml.load(
162+
fs.readFileSync(qlpackLockFile, 'utf8')
163+
);
164+
expect(!!qlpackLock.dependencies['codeql/javascript-all'].version).to.be.true;
157165
});
158166

159167
it('should avoid creating a quick query', async () => {
160168
fs.mkdirpSync(path.dirname(qlpackFile));
161169
fs.writeFileSync(qlpackFile, yaml.dump({
162170
name: 'quick-query',
163171
version: '1.0.0',
164-
libraryPathDependencies: ['codeql/javascript-all']
172+
dependencies: {
173+
'codeql/javascript-all': '*'
174+
}
165175
}));
166176
fs.writeFileSync(qlFile, 'xxx');
167177
await commands.executeCommand('codeQL.quickQuery');

0 commit comments

Comments
 (0)