Skip to content

Go: improve output of check formatting in makefile#14364

Merged
owen-mc merged 4 commits intogithub:mainfrom
owen-mc:go/improve-output-of-check-formatting-in-makefile
Oct 4, 2023
Merged

Go: improve output of check formatting in makefile#14364
owen-mc merged 4 commits intogithub:mainfrom
owen-mc:go/improve-output-of-check-formatting-in-makefile

Conversation

@owen-mc
Copy link
Copy Markdown
Contributor

@owen-mc owen-mc commented Oct 3, 2023

I tried to do this once before but my bash-scripting skills weren't up to it. This time I had copilot to do it for me 😄 .

@smowton
Copy link
Copy Markdown
Contributor

smowton commented Oct 3, 2023

Accidentally based on another PR?

@owen-mc owen-mc force-pushed the go/improve-output-of-check-formatting-in-makefile branch from 487c520 to 186146a Compare October 3, 2023 11:39
@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented Oct 3, 2023

@smowton Deliberately, to check what is printed when there are mis-formatted files. Hence why it's still a draft.

@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented Oct 3, 2023

The output when based on a PR with files that need to be reformatted is like this (taken from CI):

The following files need to be reformatted using gofmt:
./ql/test/library-tests/semmle/go/frameworks/Beego/test.go ./ql/test/library-tests/semmle/go/frameworks/Echo/test.go ./ql/test/library-tests/semmle/go/frameworks/Afero/main.go ./ql/test/library-tests/semmle/go/frameworks/Fiber/test.go ./ql/test/library-tests/semmle/go/frameworks/Iris/test.go
Error: make: *** [Makefile:35: check-formatting] Error 1

It would be slightly nicer if they weren't all on one line, but I think this is acceptable. When there is also a file that can't be parsed then the error is included in the list, like this (taken from my local machine):

The following files need to be reformatted using gofmt:
./ql/test/library-tests/semmle/go/frameworks/Afero/main.go ./ql/test/library-tests/semmle/go/frameworks/Echo/test.go ./ql/test/library-tests/semmle/go/frameworks/Iris/test.go ./ql/test/library-tests/semmle/go/frameworks/Beego/test.go ./ql/test/library-tests/semmle/go/frameworks/Fiber/test.go ./ql/src/experimental/CWE-203/timingGood.go:8:60: missing ',' in parameter list ./ql/src/experimental/CWE-203/timingBad.go:8:59: missing ',' in parameter list ./ql/src/experimental/CWE-74/DsnBad.go:2:1: expected 'package', found 'func' ./ql/src/experimental/CWE-74/DsnGood.go:1:1: expected 'package', found 'func'
make: *** [check-formatting] Error 1

Again, I think this is confusing but acceptable.

The list of files that would change when reformatted is now printed.
Also, parsing errors now make the check fail.
@owen-mc owen-mc force-pushed the go/improve-output-of-check-formatting-in-makefile branch from 186146a to 5433636 Compare October 3, 2023 11:48
@owen-mc owen-mc marked this pull request as ready for review October 3, 2023 11:48
@owen-mc owen-mc requested a review from a team as a code owner October 3, 2023 11:48
@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented Oct 3, 2023

@smowton I've rebased on main now that I've checked it gives the output I expected in CI.

Copy link
Copy Markdown
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.

Not familiar with an @ variable, but I'll take your word for it that this does what it ought to.

I don't think go files serving as qhelp examples should gain package declarations and imports; it's better that they are abbreviated snippets.

@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented Oct 4, 2023

I don't know what the @ means either, so I asked Copilot.

The @ symbol in a Makefile is used to suppress the echoing of a command. By default, when a Makefile runs a command, it first prints the command to the standard output (usually the terminal), and then runs the command. If you prefix a command with @, Make will run the command without printing it first.

Not sure if we want that, actually.

@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented Oct 4, 2023

I don't think go files serving as qhelp examples should gain package declarations and imports; it's better that they are abbreviated snippets.

I think that should be dealt with separately. All but four qhelp examples already have package declarations and imports, so I think it's fine for this PR to fix the four that don't.

@owen-mc
Copy link
Copy Markdown
Contributor Author

owen-mc commented Oct 4, 2023

With Copilot's help again I have figured out how to preserve the line breaks when printing the output of gofmt, so the list of files that need to be reformatted is now more readable. I believe it will look like this:

The following files need to be reformatted using gofmt:
./ql/test/query-tests/AlertSuppression/tst.go
./ql/test/query-tests/AlertSuppression/tstWindows.go
./ql/test/query-tests/Diagnostics/bad.go:3:1: expected 'package', found avvu
./ql/test/query-tests/Diagnostics/bad.go:5:1: expected ';', found wnvwun
./ql/test/query-tests/InconsistentCode/WhitespaceContradictsPrecedence/WhitespaceContradictsPrecedenceGood.go
./ql/test/query-tests/InconsistentCode/WhitespaceContradictsPrecedence/WhitespaceContradictsPrecedence.go
./ql/test/query-tests/InconsistentCode/WhitespaceContradictsPrecedence/main.go
./ql/test/library-tests/semmle/go/Expr/literals.go
./ql/test/library-tests/semmle/go/PrintAst/input.go
./ql/test/library-tests/semmle/go/controlflow/ControlFlowGraph/stmts.go
./ql/test/extractor-tests/robustness/tst.go
./ql/test/extractor-tests/go1.13/tst.go
./ql/test/extractor-tests/diagnostics/broken/test.go:7:1: expected declaration, found This
./ql/test/extractor-tests/diagnostics/broken2/test.go:3:1: expected 'package', found pac
./ql/test/extractor-tests/empty-interface/tst.go
./ql/test/consistency/test.go:7:1: expected declaration, found This
make: *** [check-formatting] Error 1

smowton
smowton previously approved these changes Oct 4, 2023
Copy link
Copy Markdown
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.

Change message to note that a file can be flagged due to compile errors as well as due to formatting problems; otherwise lgtm

@owen-mc owen-mc merged commit 3703c56 into github:main Oct 4, 2023
@owen-mc owen-mc deleted the go/improve-output-of-check-formatting-in-makefile branch October 4, 2023 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants