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

Add Generate Qlhelp preview command #988

Merged
merged 1 commit into from
Nov 18, 2021
Merged

Conversation

mgsium
Copy link
Contributor

@mgsium mgsium commented Oct 31, 2021

Adds a "Generate Query Help" command which generates a .md file from a selected .qhelp file and opens it for preview in vscode. Implements #481

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.

Copy link
Contributor

@shati-patel shati-patel left a comment

Choose a reason for hiding this comment

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

This new command looks exciting! (Not a real review yet, just a small comment about terminology)

extensions/ql-vscode/src/cli.ts Outdated Show resolved Hide resolved
@mgsium mgsium marked this pull request as ready for review November 1, 2021 21:05
@mgsium mgsium requested a review from a team as a code owner November 1, 2021 21:05
Copy link
Contributor

@adityasharad adityasharad left a comment

Choose a reason for hiding this comment

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

Thanks! This is a good start to a much-awaited feature.

Most comments are about security (this command should only be run in a trusted workspace) and file/path handling. There is one longer suggestion about whether we should share the temporary file mechanism used for DIL and results files, on which I'd like opinions from the others on the team.

extensions/ql-vscode/src/cli.ts Outdated Show resolved Hide resolved
extensions/ql-vscode/package.json Outdated Show resolved Hide resolved
async function previewQueryHelp(
selectedQuery: Uri
): Promise<void> {
const path = selectedQuery ? selectedQuery.path : window.activeTextEditor!.document.uri.fsPath;
Copy link
Contributor

Choose a reason for hiding this comment

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

If this command is called from the context menu on the editor, I would expect selectedQuery to be exactly window.activeTextEditor.document.uri. Is that what you observed while testing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, as far as I've observed they are equal. JSON.stringify(selectedQuery) === JSON.stringify(window.activeTextEditor.document.uri) also evaluates to true in the cases I've tested

Copy link
Contributor

Choose a reason for hiding this comment

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

In that case I think it should be enough to use selectedQuery.fsPath.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As in path = selectedQuery ? selectedQuery.fsPath : window.activeTextEditor.document.uri or just path = selectedQuery.fsPath?

Copy link
Contributor

Choose a reason for hiding this comment

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

Just path = selectedQuery.fsPath

Copy link
Contributor Author

Choose a reason for hiding this comment

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

path = selectedQuery.fsPath fails for me when I run the command from the palette

const path = selectedQuery ? selectedQuery.path : window.activeTextEditor!.document.uri.fsPath;
if(path) {
// Create temporary directory
const tmpDir = tmp.dirSync();
Copy link
Contributor

Choose a reason for hiding this comment

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

Using a temporary directory is sensible. However we must be extra careful to clean it up properly, especially on Windows.

Have you seen the other uses of temporary directories in run-queries.ts? I suggest you follow that pattern as closely as possible. In particular, I recommend passing similar options to dirSync, and creating a dispose callback that can be registered with the extension via ctx.subscriptions.push(callback), so that the extension cleans up the temporary directories when it shuts down.


More detailed discussion: We could achieve more elegant code by adding an extra field to the QueryInfo class in run-queries.ts, which currently tracks a dilPath and resultsPath for each completed query. What do others think?

  • Advantages: It handles all the temporary directories already, associates each query with a number of temporary file paths, and doesn't repeat work if the file has already been generated.
  • Disadvantages: It only works for queries that have been run, which seems unreasonable for previewing query help. We would also have to track whether the .qhelp file had been modified.

Comment on lines 512 to 515
const errorMessage = err.message.includes('Generating qhelp in markdown') ? (
`Could not generate markdown from ${path}: Bad formatting in .qhelp file.`
) : `Could not open a preview of the generated file (${absolutePathToMd}).`;
void helpers.showAndLogErrorMessage(errorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will suppress the error message from the CLI, which is hopefully useful. Could we include err in the message we construct here?

extensions/ql-vscode/package.json Outdated Show resolved Hide resolved
extensions/ql-vscode/package.json Outdated Show resolved Hide resolved
extensions/ql-vscode/package.json Outdated Show resolved Hide resolved
extensions/ql-vscode/src/extension.ts Outdated Show resolved Hide resolved
extensions/ql-vscode/CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Contributor

@shati-patel shati-patel left a comment

Choose a reason for hiding this comment

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

Just tried this locally and found one more thing! Looks good otherwise 🎇

extensions/ql-vscode/src/extension.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@shati-patel shati-patel left a comment

Choose a reason for hiding this comment

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

Sorry to leave this hanging! I think this is mostly good to merge, though I've highlighted one of the earlier review comments which looks worth doing 😃

const errorMessage = err.message.includes('Generating qhelp in markdown') ? (
`Could not generate markdown from ${pathToQhelp}: Bad formatting in .qhelp file.`
) : `Could not open a preview of the generated file (${absolutePathToMd}).`;
void helpers.showAndLogErrorMessage(errorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

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

As @adityasharad mentioned above (in #988 (comment)), it would be nice to print err itself somewhere (perhaps using logger.log(...)?) 📝

Copy link
Contributor

@adityasharad adityasharad left a comment

Choose a reason for hiding this comment

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

Looking good, thank you. Final round of minor suggestions :)

extensions/ql-vscode/CHANGELOG.md Outdated Show resolved Hide resolved
extensions/ql-vscode/src/extension.ts Outdated Show resolved Hide resolved
@@ -493,6 +494,32 @@ async function activateWithInstalledDistribution(
}
}

const tmpDir = tmp.dirSync({ prefix: 'qhelp_', keep: false, unsafeCleanup: 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: Rename this to qhelpTmpDir (or something similarly meaningful) so we can remember what it's for.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I think this pattern does the right thing on Windows (since we use it elsewhere in the extension), but I would not object to a second look from @shati-patel.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also I think this pattern does the right thing on Windows (since we use it elsewhere in the extension), but I would not object to a second look from @shati-patel.

Looks good to me too! 😊

Copy link
Contributor

@shati-patel shati-patel left a comment

Choose a reason for hiding this comment

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

This looks good to me! Once you've tidied up the changelog this should be ready to merge 🥳

Comment on lines 10 to 13
- Add a _CodeQL: Preview Query Help_ command to generate Markdown previews of `.qhelp` query help files. This command should only be run in trusted workspaces. See https://codeql.github.com/docs/codeql-cli/testing-query-help-files for more information about query help. [#988](https://github.com/github/vscode-codeql/pull/988)
- Make "Open Referenced File" command accessible from the active editor menu. [#989](https://github.com/github/vscode-codeql/pull/989)
- Add a _CodeQL: Preview Query Help File_ command to generate and preview query documentation in markdown format from a .qhelp file. [#988](https://github.com/github/vscode-codeql/pull/988)
- Add a _CodeQL: Preview Query Help_ command to generate Markdown previews of `.qhelp` query help files. See https://codeql.github.com/docs/codeql-cli/testing-query-help-files for more information about query help. [#988](https://github.com/github/vscode-codeql/pull/988)
Copy link
Contributor

Choose a reason for hiding this comment

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

There are some outdated copies of the changelog entry here! The top one looks most correct, so you can delete the others :)

@shati-patel
Copy link
Contributor

Nice work! Thank you 🎈 🥳

@shati-patel shati-patel merged commit 3743895 into github:main Nov 18, 2021
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.

3 participants