Travis_install_go_dependencies does not respect GOFLAGS="-mod=readonly"

The go tool will attempt to modify the go.mod file if necessary. In CI contexts, this is sometimes not wanted. There are two ways to avoid this, either pass -mod=readonly to the go command or set an env var GOFLAGS to have -mod=readonly as an element.

In the case that Go versions prior to the existence of Go modules are being tested in the same matrix as versions that are module-aware, the first option is onerous as differential flags must be created depending on the Go version otherwise the non-module-aware versions will fail with an error. I have also found that the instructions at the Go language page either don’t work or I don’t understand and this approach fails anyway with the gobuild_args looking like it’s being constructed as an array.

This leaves the second approach, using an env var GOFLAGS="-mod=readonly". However, the travis_install_go_dependencies bash function that is invoked during install does not respect this as can be seen here.

See discussion at golang/go issue #32147.

Could you explain this in more detail? go env is reporting this value in the build you indicated. Travis CI - Test and Deploy with Confidence Is it wrong?

You can see the definition of this function:

I don’t see how go … command would ignore this environment variable.

I’m confused by this too, but it’s the only possible explanation that makes even the slightest sense.

The reporting of go env's output is from an execution of that command by the travis builder. That is presumably being passed the environment. The execution of travis_install_go_dependencies is started by ruby code in the bowels of the travis builder. I don’t know if it is being passed the same environment; it would be surprising if it weren’t, but the fact remains that go get behaves as if GOFLAGS does not have -mod=readonly. It’s not possible for me to debug this further because all the code here is internal to the travis builder.

The results of the tests explicitly show that -mod=readonly is not respected, go get should fail rather than allowing the go.mod file to be mutated here, noticing that the file is not mutated immediately prior to this here.

This is incorrect. We compile the bash script (with a Ruby app) and execute that. The logic is all in bash.

I’m sorry, but I feel that your analysis is missing something critical. On Travis CI - Test and Deploy with Confidence, you see that within the process you are running (.travis/run-parts https://github.com/gonum/gonum/blob/66b8c0e62f1cd7bc52b01754dad66313d6f6a436/.travis/run-parts#L28-L31) GOFLAGS is set to -mod=readonly; Travis CI - Test and Deploy with Confidence

We are happy to provide you with debug access, if you wish. Please email support@travis-ci.com. Thanks.

That is true for the code that is run by run parts. I don’t have a problem with that. What I can see from the log though is that there is no diff prior to running travis_install_go_dependencies and there is a diff immediately after. This is the only part I don’t have control of and it is where the change is happening. I may be wrong, but all the evidence I can see points to it being the cause.

Again, I think an interactive debugging session will serve you well. Please email us.

Yeah, I have. However, given that everywhere else has been excluded from the cause, I know pretty much exactly where I’ll find the problem.

Thanks for pushing. It does indeed happen where I thought, but it is the go tool that is ignoring the environment.