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

Fix AST viewer for refactored language packs #939

Merged
merged 5 commits into from
Sep 2, 2021

Conversation

dbartol
Copy link
Contributor

@dbartol dbartol commented Sep 1, 2021

Most of the languages have recently been refactored into separate library and query packs, with the contextual queries defined in the query pack. In the near future, these contextual queries will move to the library pack.

Current CLI releases throw an error in codeql resolve queries when the extension tries to search the library pack for contextual queries. This change makes two related fixes:

  1. If the queries are not found in the library pack, it then scans the corresponding standard query pack as a fallback.
  2. It detects the problematic combination of CLI and packs, and avoids scanning the library pack at all in those cases. If no queries are found in the problematic scenario, the error message instructs the user to upgrade to the latest CLI version, instead of claiming that the language simply doesn't support the contextual queries yet.

This change depends on CLI 2.6.1, which is being released soon, adding the --allow-library-packs option to codeql resolve queries. That PR is already open against the CLI.

Replace this with a description of the changes your pull request makes.

Checklist

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • @github/docs-content-codeql has been cc'd in all issues for UI or other user-facing changes made by this pull request.

Most of the languages have recently been refactored into separate library and query packs, with the contextual queries defined in the query pack. In the near future, these contextual queries will move to the library pack.

Current CLI releases throw an error in `codeql resolve queries` when the extension tries to search the library pack for contextual queries. This change makes two related fixes:

1. If the queries are not found in the library pack, it then scans the corresponding standard query pack as a fallback.
2. It detects the problematic combination of CLI and packs, and avoids scanning the library pack at all in those cases. If no queries are found in the problematic scenario, the error message instructs the user to upgrade to the latest CLI version, instead of claiming that the language simply doesn't support the contextual queries yet.

This change depends on CLI 2.6.1, which is being released soon, adding the `--allow-library-packs` option to `codeql resolve queries`. That PR is already open against the CLI.
@dbartol dbartol added bug Something isn't working Complexity: Low A good task for newcomers to learn, or experienced team members to complete quickly. labels Sep 1, 2021
@dbartol dbartol requested a review from a team as a code owner September 1, 2021 23:25
@dbartol
Copy link
Contributor Author

dbartol commented Sep 1, 2021

@aeisenberg I've tested all of the relevant CLI/language pack combinations manually. Is there an easy way to automate such testing?

Copy link
Contributor

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and clever. I have a handful of comments, but none of them are really blocking this from going in. I am not really concerned about calling the CLI multiple times since it is only atmost twice.

extensions/ql-vscode/CHANGELOG.md Outdated Show resolved Hide resolved
@@ -775,11 +775,15 @@ export class CodeQLCliServer implements Disposable {
* the default CLI search path is used.
* @returns A list of query files found.
*/
resolveQueriesInSuite(suite: string, additionalPacks: string[], searchPath?: string[]): Promise<string[]> {
async resolveQueriesInSuite(suite: string, additionalPacks: string[], searchPath?: string[]): Promise<string[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wasn't this async before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, its only return statement was returning an appropriately-typed promise from a function call, so there was no need to use await inside the implementation.

include: {
kind: kindOfKeyType(keyType),
'tags contain': tagOfKeyType(keyType)
const queries = await cli.resolveQueriesInSuite(suiteFile, helpers.getOnDiskWorkspaceFolders());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about calling the CLI in a loop. Each call is a few seconds since it needs to restart the vm. If it's only going to be called 2 or 3 times for each AST Viewer call, maybe that's fine.

Alternatively, could you combine all the qlpacks into one suite? It would look like this:

const allPacks = [
  {
    qlpack: 'codeql/cpp-queries',
    include: { ... }
  },
  {
    qlpack: 'codeql/cpp-all',
    include: { ... }
  }
];

And then you can just send this suite directly to the CLI. I think it would work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use the CodeQL CLI server (codeql execute cli-server) . Probably not in this pull request, but it could be something to consider in a future refactoring.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, did a little bit of digging here and we are using the cli server for all calls to the cli. The queries are handled by the query server and other commands by the cli server.

This means that it is less important to combine these pack references into a single suite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the packs are all properly refactored, we'll always find the queries in the dbscheme library pack, so we'll never search the query pack. The only situation where we'll search both packs is with a >=2.6.1 CLI and packs that were released at the same time as 2.6.1. By the next dist upgrade, the contextual queries will already be in the library pack.

let blameCli: boolean;

if (cliCanHandleLibraryPack) {
// The CLI can handle both library packs and query packs, so search both packs in order.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah...so it's only ever 2 calls to the CLI for resolve queries. Still might make sense to combine them into a single suite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above. Even the two-call case is only for a specific combination of CLI/pack versions.

extensions/ql-vscode/src/helpers.ts Outdated Show resolved Hide resolved
@aeisenberg
Copy link
Contributor

aeisenberg commented Sep 2, 2021

Hmmm...getting some test failures. Looks like there's a new field that should be mocked.

Let me see what the fix is.

@aeisenberg aeisenberg force-pushed the dbartol/ast-viewer-refactored-packs branch from efc0d8b to 4c1689f Compare September 2, 2021 17:00
Test that old CLIs properly ignore the library packs.
@aeisenberg aeisenberg force-pushed the dbartol/ast-viewer-refactored-packs branch from 4c1689f to f3136a0 Compare September 2, 2021 17:13
@aeisenberg
Copy link
Contributor

Fixed the unit test and added a new one. Feel free to merge when all workflows pass.

@aeisenberg aeisenberg enabled auto-merge (rebase) September 2, 2021 18:05
@aeisenberg aeisenberg merged commit 71f374d into main Sep 2, 2021
@aeisenberg aeisenberg deleted the dbartol/ast-viewer-refactored-packs branch September 2, 2021 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Complexity: Low A good task for newcomers to learn, or experienced team members to complete quickly.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants