Skip to content

Commit f6bc0eb

Browse files
committed
Avoid unnecessary go list command execution when running tasks
Introduction ============ Go commands used during the development of this project may take a list of packages as argument. Generally, we will want to run the command on most of the packages of the targeted module, excluding selected packages which are not of interest (the automatically generated `github.com/arduino/arduino-lint/internal/rule/schema/schemadata` data package for example). The list of packages is generated using the `go list` command. Problem ======= Previously, the list was defined via a "dynamic" taskfile variable. The benefit to that approach was that the `go list` command ran only once per execution of `task`. However, there were some serious disadvantages: **The module path could not be adjusted on the fly** The list would always be for the module path set at the time the `task` command ran (either by setting the `GO_MODULE_PATH` environment variable in advance, or by the default of the repo root). This meant it was not possible to call the Go tasks multiple times from an "umbrella" task to cover the multiple modules in the repository. **The `go list` command ran on all `task` executions** Many of the tasks don't have any need for the information generated by this command, meaning this was inefficient. More significantly, this command can have a side effect of changing the dependency metadata a la `go mod tidy`. Since the CI ensures the project is always in a tidy state, you would not expect this to cause an inappropriate diff. However, `go mod tidy` can have a different result depending on which version of Go is in use. Although we always use the same version of Go for development and for the runs of the Go-related CI workflows, that is not done for the non Go-related workflows. This meant that the `go list` command ran with whatever Go version happened to be pre-installed in the GitHub Actions runner (currently 1.15.15), resulting in an unexpected diff. Since some of those workflows use `git diff` to detect problems, this would cause spurious workflow failures. Solution ======== The solution is to change this taskfile variable so that it is not "dynamic". With this new approach, the variable is only a string, which is interpreted by the shell at the time of the task is ran. Although this does mean that the `go list` command runs each time the variable occurs in the task being ran, in practice that does not usually result in inefficiency since most often the variable only occurs once in the task run via a `task` execution, and when it does occur multiple times it is in order to run the task for each module, in which case the `go list` command needs to run again anyway.
1 parent 4f0fa8b commit f6bc0eb

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

Taskfile.yml

+8-11
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ vars:
1010
DIST_DIR: "dist"
1111
# Path of the project's primary Go module:
1212
DEFAULT_GO_MODULE_PATH: ./
13-
DEFAULT_GO_PACKAGES:
14-
sh: |
15-
echo $( \
16-
cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} \
17-
&& \
18-
go list ./... | \
19-
grep --invert-match 'github.com/arduino/arduino-lint/internal/rule/schema/schemadata' | \
20-
tr '\n' ' ' \
21-
|| \
22-
echo '"ERROR: Unable to discover Go packages"' \
23-
)
13+
DEFAULT_GO_PACKAGES: |
14+
$( \
15+
go list ./... | \
16+
grep --invert-match 'github.com/arduino/arduino-lint/internal/rule/schema/schemadata' | \
17+
tr '\n' ' ' \
18+
|| \
19+
echo '"ERROR: Unable to discover Go packages"' \
20+
)
2421
# build vars
2522
COMMIT:
2623
sh: echo "$(git log --no-show-signature -n 1 --format=%h)"

0 commit comments

Comments
 (0)