Invoke codeql pack install after adding a quick query

This ensures the pack lock file is in place after the quick query is
generated.
This commit is contained in:
Andrew Eisenberg
2022-06-28 11:34:09 -07:00
committed by Edoardo Pirovano
parent 2debadd3bf
commit 22873a2f3c
3 changed files with 31 additions and 7 deletions

View File

@@ -917,8 +917,12 @@ export class CodeQLCliServer implements Disposable {
return this.runJsonCodeQlCliCommand(['pack', 'download'], packs, 'Downloading packs');
}
async packInstall(dir: string) {
return this.runJsonCodeQlCliCommand(['pack', 'install'], [dir], 'Installing pack dependencies');
async packInstall(dir: string, forceUpdate = false) {
const args = [dir];
if (forceUpdate) {
args.push('--mode', 'update');
}
return this.runJsonCodeQlCliCommand(['pack', 'install'], args, 'Installing pack dependencies');
}
async packBundle(dir: string, workspaceFolders: string[], outputPath: string, precompile = true): Promise<void> {
@@ -1270,7 +1274,7 @@ export class CliVersionConstraint {
/**
* CLI version where the `resolve ml-models` subcommand was enhanced to work with packaging.
*/
public static CLI_VERSION_WITH_PRECISE_RESOLVE_ML_MODELS = new SemVer('2.10.0');
public static CLI_VERSION_WITH_PRECISE_RESOLVE_ML_MODELS = new SemVer('2.10.0');
/**
* CLI version where the `--old-eval-stats` option to the query server was introduced.

View File

@@ -121,7 +121,9 @@ export async function displayQuickQuery(
const quickQueryQlpackYaml: any = {
name: 'vscode/quick-query',
version: '1.0.0',
libraryPathDependencies: [qlpack]
dependencies: {
[qlpack]: '*'
}
};
await fs.writeFile(qlPackFile, QLPACK_FILE_HEADER + yaml.dump(quickQueryQlpackYaml), 'utf8');
}
@@ -130,6 +132,11 @@ export async function displayQuickQuery(
await fs.writeFile(qlFile, getInitialQueryContents(dbItem.language, dbscheme), 'utf8');
}
if (shouldRewrite) {
await cliServer.clearCache();
await cliServer.packInstall(queriesDir, true);
}
await Window.showTextDocument(await workspace.openTextDocument(qlFile));
} catch (e) {
if (e instanceof ResponseError && e.code == ErrorCodes.RequestCancelled) {
@@ -145,5 +152,5 @@ async function checkShouldRewrite(qlPackFile: string, newDependency: string) {
return true;
}
const qlPackContents: any = yaml.load(await fs.readFile(qlPackFile, 'utf8'));
return qlPackContents.libraryPathDependencies?.[0] !== newDependency;
return !qlPackContents.dependencies?.[newDependency];
}

View File

@@ -38,6 +38,8 @@ describe('Queries', function() {
let ctx: ExtensionContext;
let qlpackFile: string;
let qlpackLockFile: string;
let oldQlpackLockFile: string; // codeql v2.6.3 and earlier
let qlFile: string;
@@ -53,6 +55,8 @@ describe('Queries', function() {
cli.quiet = true;
ctx = extension.ctx;
qlpackFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.yml`;
qlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/codeql-pack.lock.yml`;
oldQlpackLockFile = `${ctx.storageUri?.fsPath}/quick-queries/qlpack.lock.yml`;
qlFile = `${ctx.storageUri?.fsPath}/quick-queries/quick-query.ql`;
} else {
throw new Error('Extension not initialized. Make sure cli is downloaded and installed properly.');
@@ -153,7 +157,14 @@ describe('Queries', function() {
fs.readFileSync(qlpackFile, 'utf8')
);
// Should have chosen the js libraries
expect(qlpackContents.libraryPathDependencies[0]).to.include('javascript');
expect(qlpackContents.dependencies['codeql/javascript-all']).to.eq('*');
// Should also have a codeql-pack.lock.yml file
const packFileToUse = fs.pathExistsSync(qlpackLockFile) ? qlpackLockFile : oldQlpackLockFile;
const qlpackLock: any = await yaml.load(
fs.readFileSync(packFileToUse, 'utf8')
);
expect(!!qlpackLock.dependencies['codeql/javascript-all'].version).to.be.true;
});
it('should avoid creating a quick query', async () => {
@@ -161,7 +172,9 @@ describe('Queries', function() {
fs.writeFileSync(qlpackFile, yaml.dump({
name: 'quick-query',
version: '1.0.0',
libraryPathDependencies: ['codeql/javascript-all']
dependencies: {
'codeql/javascript-all': '*'
}
}));
fs.writeFileSync(qlFile, 'xxx');
await commands.executeCommand('codeQL.quickQuery');