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

Contextual queries: Support running when the library pack is in the package cache #1735

Merged
merged 5 commits into from
Nov 9, 2022

Conversation

adityasharad
Copy link
Contributor

@adityasharad adityasharad commented Nov 8, 2022

If the library pack containing the AST query does not have a lock file, it is likely to be in the package cache, not
a checkout of the CodeQL repo.
In this case, use codeql pack resolve-dependencies to create a temporary lock file, and codeql pack install
to install the dependencies of this library pack.

This allows the CLI to resolve the library path and dependencies for the AST query before running it, in the use case where the standard library packs are installed in the package cache rather than present in the workspace. Otherwise, in the absence of a lock file, codeql resolve library-path finds the standard library pack that contains the AST query, but not the codeql/ssa shared pack that this library pack depends on.

Reviewer notes

  • I've tried to keep this change minimal so that it doesn't affect the usual use case where the codeql repo is present as a workspace folder, containing library packs with lockfiles, and the AST queries are obtained from there.
    • Expanded to include find references and jump to definition, not just the AST viewer.
    • Tested in the usual starter workspace, and in a workspace where the library pack is in the package cache and not the workspace.
  • This didn't work consistently when first run in a debug extension, which makes me think there may be race conditions where we attempt to run the query before the lock file is ready on disk. Is it possible to wait for the two pack-related CLI commands to complete? Suggestions welcome.
    • Fixed by review suggestion of clearing the CLI server pack cache.

Checklist

  • [N/A] 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.
  • [N/A] [Maintainers only] If this pull request makes user-facing changes that require documentation changes, open a corresponding docs pull request in the github/codeql repo and add the ready-for-doc-review label there.

…ache

If the library pack containing the AST query does not have
a lock file, it is likely to be in the package cache, not
a checkout of the CodeQL repo.
In this case, use `codeql pack resolve-dependencies`
to create a temporary lock file, and `codeql pack install`
to install the dependencies of this library pack.

This allows the CLI to resolve the library path and
dependencies for the AST query before running it.
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.

You will need to do something similar for find declarations and find references, but that is probably less important.

extensions/ql-vscode/src/cli.ts Outdated Show resolved Hide resolved
const packContents = await this.cli.packPacklist(query, false);
const packFilePath = packContents.find((p) => ['codeql-pack.yml', 'qlpack.yml'].includes(path.basename(p)));
if (packFilePath === undefined) {
// Should not happen; we already resolved this query.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this always true? What happens if you try to run a query in a directory that's outside of a qlpack?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not generally true, but I hope it is always true for our own contextual queries.

extensions/ql-vscode/src/contextual/templateProvider.ts Outdated Show resolved Hide resolved
extensions/ql-vscode/src/contextual/templateProvider.ts Outdated Show resolved Hide resolved
extensions/ql-vscode/src/contextual/templateProvider.ts Outdated Show resolved Hide resolved
Clear the CLI server's pack cache before installing packs,
to avoid race conditions where the new lock file is not
detected during query running.

Adjust some helper methods.
Shared by the AST viewer, jump to def, and find references
contextual queries.

This allows contextual queries to have their dependencies
resolved and be run whether the library pack is in the
workspace or in the package cache.
@adityasharad
Copy link
Contributor Author

Thanks! Addressed comments and refactored to cover AST viewer, find references, and jump to definition.
The CFG viewer runs the query slightly differently, so I've left that as a follow-up since it's internal-only.

@adityasharad adityasharad changed the title AST viewer: Support running when the library pack is in the package cache Contextual queries: Support running when the library pack is in the package cache Nov 9, 2022
@adityasharad adityasharad marked this pull request as ready for review November 9, 2022 00:10
@adityasharad adityasharad requested a review from a team as a code owner November 9, 2022 00:10
extensions/ql-vscode/src/contextual/queryResolver.ts Outdated Show resolved Hide resolved
const tempLockFilePath = path.resolve(packPath, 'codeql-pack.lock.yml');
void logger.log(`Deleting temporary package lock file at ${tempLockFilePath}`);
// It's fine if the file doesn't exist.
await fs.promises.rm(path.resolve(packPath, 'codeql-pack.lock.yml'), { force: true });
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor: this should work.

Suggested change
await fs.promises.rm(path.resolve(packPath, 'codeql-pack.lock.yml'), { force: true });
await fs.rm(path.resolve(packPath, 'codeql-pack.lock.yml'), { force: true });

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The compiler is not happy with this, and for some reason I don't see an async version of rm in the installed type definitions. Going to keep it for now but happy to replace later if we get it working.

@aeisenberg aeisenberg merged commit 473569d into github:main Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants