Skip to content

Rust: Remove external locations in tests using post-processing #19669

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

Merged

Conversation

hvitved
Copy link
Contributor

@hvitved hvitved commented Jun 4, 2025

This PR introduces (shared) .qlref post-processing logic for converting external locations to special {EXTERNAL LOCATION} markers instead.

By changing these locations via post-processing instead of in the individual .ql test files, we avoid replicating logic and also keeps the ability to run the .ql files in VS Code using the real external locations.

For Rust this means we get rid of all the annoying Location is outside of test directory warnings.

@github-actions github-actions bot added the Rust Pull requests that update Rust code label Jun 4, 2025
@hvitved hvitved force-pushed the rust/external-location-post-processing branch from 73d132d to d384e78 Compare June 4, 2025 10:03
@hvitved hvitved force-pushed the rust/external-location-post-processing branch from d384e78 to aa0fc05 Compare June 4, 2025 11:08
@hvitved hvitved marked this pull request as ready for review June 4, 2025 11:29
@Copilot Copilot AI review requested due to automatic review settings June 4, 2025 11:29
@hvitved hvitved requested a review from a team as a code owner June 4, 2025 11:29
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds a shared post‐processing step to normalize external file locations in CodeQL tests and updates Rust tests to use it instead of inline location handling.

  • Introduce ExternalLocationPostProcessing.qll with logic to map external locations to {EXTERNAL LOCATION}
  • Make existing test predicates in InlineExpectationsTest.qll private
  • Remove per-test “hasLocationInfo” classes in Rust library tests and wire in the new postprocessor via updated .qlref files

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
shared/util/codeql/util/test/InlineExpectationsTest.qll Changed external predicates to external private
shared/util/codeql/util/test/ExternalLocationPostProcessing.qll New module that replaces external locations with a marker
rust/ql/test/library-tests//.qlref Added postprocess: utils/test/ExternalLocationPostProcessing.ql
rust/ql/test/library-tests//.ql Removed inline location helper classes and updated predicate sigs
rust/ql/lib/utils/test/ExternalLocationPostProcessing.ql QL pack wrapper importing shared postprocessor and prefix logic

Comment on lines +21 to +23
not s = "file://" + any(string suffix) or
s = "file://:0:0:0:0" or
s = getSourceLocationPrefix() + any(string suffix)
Copy link
Preview

Copilot AI Jun 4, 2025

Choose a reason for hiding this comment

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

Operator precedence is ambiguous here: the not only negates the first equality. Wrap the entire set of prefix and exact-match checks in parentheses so you get if not (s = "file://" + … or s = "file://:0:0:0:0" or s = getSourceLocationPrefix() + …) then as intended.

Suggested change
not s = "file://" + any(string suffix) or
s = "file://:0:0:0:0" or
s = getSourceLocationPrefix() + any(string suffix)
not (s = "file://" + any(string suffix) or
s = "file://:0:0:0:0" or
s = getSourceLocationPrefix() + any(string suffix))

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe the way it is written is the way it's intended, and this change would break correctness.

query predicate results(string relation, int row, int column, string data) {
exists(string s | queryResults(relation, row, column, s) |
if
not s = "file://" + any(string suffix) or
Copy link
Preview

Copilot AI Jun 4, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider using the startsWith predicate (e.g., s.startsWith("file://")) instead of s = "file://" + any(string suffix) for clearer intent and potentially better performance.

Suggested change
not s = "file://" + any(string suffix) or
not s.startsWith("file://") or

Copilot uses AI. Check for mistakes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we have a built-in startsWith predicate. You could do .matches("file://%") and that might perform better (or worse).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think .matches("file://%") actually performs worse; the current solution compiles down to inverse string append, which I think uses startsWith or similar under-the-hood.

@hvitved hvitved added the no-change-note-required This PR does not need a change note label Jun 4, 2025
Copy link
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

LGTM. Less code, more re-usable than the previous ad-hoc solutions.

@hvitved hvitved merged commit 76c6d71 into github:main Jun 4, 2025
40 of 42 checks passed
@hvitved hvitved deleted the rust/external-location-post-processing branch June 4, 2025 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-change-note-required This PR does not need a change note Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants