Skip to content

Go: Check more things while running tests #19491

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
merged 7 commits into from
May 28, 2025

Conversation

owen-mc
Copy link
Contributor

@owen-mc owen-mc commented May 14, 2025

This PR adds the following flags when we run tests: --fail-on-trap-errors, --check-undefined-labels, --check-unused-labels, --check-repeated-labels, --check-redefined-labels, --check-use-before-definition. There are also two extractor fixes to make tests pass with these flags:

  1. Avoid making unused labels in certain situations.
  2. Fix a db inconsistency on Windows by making the Go extractor use "C:/" for the drive root instead of "C:", so it matches up with the XML extractor (which we use for HTML files).

@owen-mc owen-mc added the no-change-note-required This PR does not need a change note label May 14, 2025
@github-actions github-actions bot added the Go label May 14, 2025
@owen-mc
Copy link
Contributor Author

owen-mc commented May 15, 2025

The CI has highlighted a db inconsistency that only happens on windows: the container parent of "C:/a" is recorded as both "C:/" and "C:". This only happens in tests with html files, hence it must be related to the xml extractor.

@owen-mc owen-mc force-pushed the go/add-test-flags branch 3 times, most recently from c42710a to afc4bf3 Compare May 20, 2025 02:24
@owen-mc owen-mc marked this pull request as ready for review May 20, 2025 12:00
@Copilot Copilot AI review requested due to automatic review settings May 20, 2025 12:00
@owen-mc owen-mc requested a review from a team as a code owner May 20, 2025 12:00
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 more validation flags to the CodeQL test command and refactors the Go extractor’s path handling and nil checks.

  • Introduces rawPath/canonicalPath in extractFileInfo to normalize file paths.
  • Simplifies nil checks in extractExpr, extractStmt, and extractDecl by moving them to the top.
  • Updates test runner flags in Makefile and adjusts GitHub Actions Windows runner configuration.
  • Aligns greet.go import alias with updated definitions expected output.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
go/ql/test/query-tests/definitions/greet.go Renamed import to myfmt and updated print call
go/ql/test/query-tests/definitions/definitions.expected Added new definition entry for myfmt
go/extractor/extractor.go Introduced rawPath/canonicalPath, refactored nil checks
go/Makefile Added new --check-* flags to codeql test run
.github/workflows/go-tests-other-os.yml Changed runs-on from windows-latest-xl to windows-latest
Comments suppressed due to low confidence (1)

go/Makefile:57

  • Remove the --check-unused-labels flag, as noted in the PR description, to prevent unintended test failures caused by trap errors and ensure consistency with the intended test configuration.
codeql test run -j0 ql/test --search-path .. --consistency-queries ql/test/consistency --compilation-cache=$(cache) --dynamic-join-order-mode=$(rtjo) --check-databases --fail-on-trap-errors --check-undefined-labels --check-unused-labels --check-repeated-labels --check-redefined-labels --check-use-before-definition

if path != "/" {
parentPath = path
}
parentPath = rawPath
Copy link
Preview

Copilot AI May 20, 2025

Choose a reason for hiding this comment

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

Unconditionally assigning parentPath = rawPath will make the next iteration build rawPath differently when rawPath is empty (root). Consider guarding this assignment for root paths (e.g. only set parentPath when rawPath is non-empty) to preserve correct canonical paths.

Suggested change
parentPath = rawPath
if rawPath != "" {
parentPath = rawPath
}

Copilot uses AI. Check for mistakes.

owen-mc added 6 commits May 20, 2025 13:13
This makes the test slightly more thorough.
In go, an interface with value nil does not compare equal to nil. This
is known as "typed nils". So our existing nil checks weren't working,
which shows why we needed more nil checks inside the type switches. The
solution is to explicitly check for each type we care about.
@owen-mc owen-mc force-pushed the go/add-test-flags branch from 85d5922 to 83cd349 Compare May 20, 2025 12:13
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Path-formatting and test flag changes LGTM. Suggest not making the nil-check changes.

@@ -936,7 +941,16 @@ func emitScopeNodeInfo(tw *trap.Writer, nd ast.Node, lbl trap.Label) {

// extractExpr extracts AST information for the given expression and all its subexpressions
func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int, skipExtractingValue bool) {
if expr == nil {
if expr == nil || expr == (*ast.Ident)(nil) || expr == (*ast.BasicLit)(nil) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

TIL: this makes sense. It does, though! Weak preference for keeping the old code, since as/when there are new AST nodes it is now easier to accidentally not add the nil check.

Copy link
Contributor

Choose a reason for hiding this comment

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

as a Go neophyte, after doing a quick google search, would something like expr == nil || reflect.ValueOf(i).IsNil() work and avoid the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's a definite footgun in the language, which I think they probably should have a better solution for. Btw there is an alternative which I saw suggested when I searched for this online, which is to use reflection to check if the interface type has a nil value. I thought this was too ugly, but it would address the concern that we need to remember to add any new types to the check.

You want to leave in the nil checks in the branches of the type switch? I don't really agree, but I'll do it as it isn't worth debating.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My comment above was a response to @smowton 's comment. I'm not sure about the exact formulation of using reflection. Since we know it will be an interface and not a map or channel or anything like that, it shouldn't be too hard.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the reflection isn't too costly that's fine. After that I'd say a check in each branch is slightly better the a mass of checks up front, because the up-front ones can drift out of sync with the arms of the switch too easily.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a definite footgun in the language

So true! TIL, I'll definitely keep that in mind for next time I might dabble with Go.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another idea occurred to me: we just move the two lines that are above the type switch into every case clause. Then the nil check works as expected. There is duplication, but it isn't too bad, and anyone adding another case in future will be able to copy from other case clauses without any risk of error. I think if reflection works and is performant then that is probably my first choice, followed by this duplication approach.

@@ -936,7 +941,16 @@ func emitScopeNodeInfo(tw *trap.Writer, nd ast.Node, lbl trap.Label) {

// extractExpr extracts AST information for the given expression and all its subexpressions
func extractExpr(tw *trap.Writer, expr ast.Expr, parent trap.Label, idx int, skipExtractingValue bool) {
if expr == nil {
if expr == nil || expr == (*ast.Ident)(nil) || expr == (*ast.BasicLit)(nil) ||
Copy link
Contributor

Choose a reason for hiding this comment

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

as a Go neophyte, after doing a quick google search, would something like expr == nil || reflect.ValueOf(i).IsNil() work and avoid the problem?

@owen-mc
Copy link
Contributor Author

owen-mc commented May 22, 2025

I have switched to using reflection for the nil check on interfaces. DCA showed no significant changes in build time - on the two largest repos it reduces it, if anything. I will do a QA run to be sure.

@owen-mc
Copy link
Contributor Author

owen-mc commented May 22, 2025

QA showed no negative consequences. The build step was took on average 1.68% less time and the analysis step took on average 1% less time. It's tempting to think that this PR caused that, but it may well just be noise.

There was one alert difference - I'll double check it locally.

@owen-mc owen-mc requested a review from smowton May 22, 2025 21:17
@owen-mc
Copy link
Contributor Author

owen-mc commented May 23, 2025

I wasn't able to reproduce the alert difference locally. My best guess was that the repo was updated between the left run and the right run, but actually it hasn't been updated for two years.

@owen-mc owen-mc merged commit 6f71e3b into github:main May 28, 2025
13 checks passed
@owen-mc owen-mc deleted the go/add-test-flags branch May 28, 2025 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Go no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants