Avoid unnecessary go list
command execution when running tasks
#355
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 (e.g., the automatically generated
github.com/arduino/arduino-lint/internal/rule/schema/schemadata
package).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 oftask
. 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 theGO_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 (example).The
go list
command ran on alltask
executionsMany 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 usegit diff
to detect problems, this would cause spurious workflow failures. For example:https://github.com/arduino/arduino-lint/runs/5561403607?check_suite_focus=true#step:5:35
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 atask
execution, and when it does occur multiple times it is in order to run the task for each module, in which case thego list
command needs to run again anyway.