Skip to content

Commit

Permalink
Invoke codeql pack install after adding a quick query
Browse files Browse the repository at this point in the history
This ensures the pack lock file is in place after the quick query is
generated.
  • Loading branch information
aeisenberg committed Jun 28, 2022
1 parent 6ccbcd9 commit 369fa95
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
10 changes: 7 additions & 3 deletions extensions/ql-vscode/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 9 additions & 2 deletions extensions/ql-vscode/src/quick-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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) {
Expand All @@ -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];
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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.');
Expand Down Expand Up @@ -153,15 +157,24 @@ 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 () => {
fs.mkdirpSync(path.dirname(qlpackFile));
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');
Expand Down

0 comments on commit 369fa95

Please sign in to comment.