Skip to content

Commit 22873a2

Browse files
aeisenbergedoardopirovano
authored andcommitted
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 2debadd commit 22873a2

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ describe('Queries', function() {
3838
let ctx: ExtensionContext;
3939

4040
let qlpackFile: string;
41+
let qlpackLockFile: string;
42+
let oldQlpackLockFile: string; // codeql v2.6.3 and earlier
4143
let qlFile: string;
4244

4345

@@ -53,6 +55,8 @@ describe('Queries', function() {
5355
cli.quiet = true;
5456
ctx = extension.ctx;
5557
qlpackFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.yml`;
58+
qlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/codeql-pack.lock.yml`;
59+
oldQlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.lock.yml`;
5660
qlFile = `${ctx.storageUri?.fsPath}/quick-queries/quick-query.ql`;
5761
} else {
5862
throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.');
@@ -153,15 +157,24 @@ describe('Queries', function() {
153157
fs.readFileSync(qlpackFile, 'utf8')
154158
);
155159
// Should have chosen the js libraries
156-
expect(qlpackContents.libraryPathDependencies[0]).to.include('javascript');
160+
expect(qlpackContents.dependencies['codeql/javascript-all']).to.eq('*');
161+
162+
// Should also have a codeql-pack.lock.yml file
163+
const packFileToUse = fs.pathExistsSync(qlpackLockFile) ? qlpackLockFile : oldQlpackLockFile;
164+
const qlpackLock: any = await yaml.load(
165+
fs.readFileSync(packFileToUse, 'utf8')
166+
);
167+
expect(!!qlpackLock.dependencies['codeql/javascript-all'].version).to.be.true;
157168
});
158169

159170
it('should avoid creating a quick query', async () => {
160171
fs.mkdirpSync(path.dirname(qlpackFile));
161172
fs.writeFileSync(qlpackFile, yaml.dump({
162173
name: 'quick-query',
163174
version: '1.0.0',
164-
libraryPathDependencies: ['codeql/javascript-all']
175+
dependencies: {
176+
'codeql/javascript-all': '*'
177+
}
165178
}));
166179
fs.writeFileSync(qlFile, 'xxx');
167180
await commands.executeCommand('codeQL.quickQuery');

0 commit comments

Comments
 (0)