Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke codeql pack install after adding a quick query #1411

Merged
merged 2 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions extensions/ql-vscode/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [UNRELEASED]

- Fix a bug where quick queries cannot be compiled if the core libraries are not in the workspace. [#1411](https://github.com/github/vscode-codeql/pull/1411)

## 1.6.7 - 15 June 2022

- Prints end-of-query evaluator log summaries to the Query Log. [#1349](https://github.com/github/vscode-codeql/pull/1349)
Expand Down
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