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

ci-test: Fix syntax/formatting check so it actually emits an exit code #237

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ottok
Copy link
Contributor

@ottok ottok commented Jan 5, 2025

I noticed this bug while working on #225 and I saw this:

https://github.com/Debian/dh-make-golang/actions/runs/12617186618/job/35159206853

image

The old format didn't emit any exit codes and thus did not cause CI to fail:

$ diff -u <(echo -n) <(gofmt -d -s .)
make.go:513:46: missing ',' before newline in composite literal
$ echo $?
0

The smallest possible fix to this is to ensure gofmt output actually goes to stdout as the original test expected:

$ diff -u <(echo -n) <(gofmt -d -s . 2>&1)
--- /dev/fd/63	2025-01-05 07:46:28.550632333 +0000
+++ /dev/fd/62	2025-01-05 07:46:28.550632333 +0000
@@ -0,0 +1 @@
+make.go:513:46: missing ',' before newline in composite literal
$ echo $?
1

@ottok
Copy link
Contributor Author

ottok commented Jan 7, 2025

Thanks @tianon and @zhsj for commenting my other PRs. Could you review/approve this one too? Thanks

@ottok
Copy link
Contributor Author

ottok commented Jan 14, 2025

@Maytha8 or @n-peugnet Can you review/approve this?

@ottok ottok force-pushed the fix-ci-go-fmt-check branch from f8f6193 to 4a27042 Compare January 14, 2025 03:43
@n-peugnet
Copy link
Contributor

Looks OK, but gofmt is more about code formatting/prettify than syntax errors. In this case the syntax errors are caught in the next step, so this is more or less the same thing as before.

@ottok
Copy link
Contributor Author

ottok commented Jan 14, 2025

Looks OK, but gofmt is more about code formatting/prettify than syntax errors. In this case the syntax errors are caught in the next step, so this is more or less the same thing as before.

If the step is useless, should we remove it instead of fixing it?

@n-peugnet
Copy link
Contributor

If the step is useless, should we remove it instead of fixing it?

This step is not useless, it checks that the code is formatted with gofmt.

@ottok
Copy link
Contributor Author

ottok commented Jan 14, 2025

But it is part of a CI pipeline, so if the job can and will never fail, nobody will ever look at it.

@n-peugnet
Copy link
Contributor

n-peugnet commented Jan 14, 2025

gofmt -d sends the diff to stdout:

$ diff -u <(echo -n) <(gofmt -d -s .)
--- /dev/fd/63	2025-01-14 19:01:56.993862536 +0100
+++ /dev/fd/62	2025-01-14 19:01:57.005862508 +0100
@@ -0,0 +1,12 @@
+diff main.go.orig main.go
+--- main.go.orig
++++ main.go
+@@ -15,7 +15,7 @@
+ )
+ 
+ func usage() {
+-fmt.Fprintf(os.Stderr, "%s\n", buildVersionString())
++	fmt.Fprintf(os.Stderr, "%s\n", buildVersionString())
+ 	fmt.Fprintf(os.Stderr, "\n")
+ 	fmt.Fprintf(os.Stderr, "%s is a tool that converts Go packages into Debian package source.\n", program)
+ 	fmt.Fprintf(os.Stderr, "\n")
$ echo $?
1

But I agree that the result is not very beautiful with the double diff. In my own projects I usually use go fmt ./... && git diff --exit-code --color, but it does not check for simplifications (-s), for example: https://salsa.debian.org/lintian/lintian-ssg/-/jobs/6488774#L22

Instead we could use gofmt -w -s . && git diff --exit-code --color. I should start using it for my own projects in fact 🤔

@n-peugnet
Copy link
Contributor

Here is an exemple of what it does that just happened to me 😅: https://github.com/Debian/dh-make-golang/actions/runs/12845745463/job/35820145357?pr=243

The old format didn't emit any exit codes and thus did not cause CI to
fail:

    $ diff -u <(echo -n) <(gofmt -d -s .)
    make.go:513:46: missing ',' before newline in composite literal
    $ echo $?
    0

The old code would have an failure only if `gofmt` ran successfully AND
outputted changes. With the new structure both `gofmt` and `git` needs
to pass, thus capturing exit codes and error of any kind of failures.
@ottok ottok force-pushed the fix-ci-go-fmt-check branch from 4a27042 to efb6240 Compare January 19, 2025 22:05
@ottok
Copy link
Contributor Author

ottok commented Jan 19, 2025

Updated latest version to use the double command you suggested. It seems like the most solid option. Thanks!

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