Repository: go
Issue: cmd/go: fortran modules in go mod cache fail to compile (permission denied)
Labels: help wanted,NeedsFix,modules

<!--
Please answer these questions before submitting your issue. Thanks!
For questions please use one of our forums: https://github.com/golang/go/wiki/Questions
-->

### What version of Go are you using (`go version`)?

<pre>
$ go version
go version go1.17.1 darwin/amd64
</pre>

### Does this issue reproduce with the latest release?
Yes


### What operating system and processor architecture are you using (`go env`)?

<details><summary><code>go env</code> Output</summary><br><pre>
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/<username>/Library/Caches/go-build"
GOENV="/Users/<username>/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/<username>/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/<username>/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.1"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/<username>/tmp/mygotests/calltest/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/5b/15pp65tn35gb5h3d9jqhf1c00000gp/T/go-build805641474=/tmp/go-build -gno-record-gcc-switches -fno-common"

</pre></details>

### What did you do?
I am trying to use a module which incorporates some fortran code via cgo. If this fortran code uses fortran modules the compilation fails.

#### Reproducible example
```
$ CC=gcc-11 go run -x github.com/idavydov/go_fortran_test_run2@latest
<...>
cd /Users/<username>/go/pkg/mod/github.com/idavydov/go_fortran_test2@v0.0.0-20210913063354-750b058b70dc
TERM='dumb' gfortran -I . -fPIC -arch x86_64 -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=$WORK/b002=/tmp/go-build -gno-record-gcc-switches -fno-common -I $WORK/b002/ -g -O2 -o $WORK/b002/_x003.o -c mylib.f90
# github.com/idavydov/go_fortran_test2
mylib.f90:8:13:

    8 | end module hi
      |             1
Fatal Error: Cannot open module file 'hi.mod0' for writing at (1): Permission denied
compilation terminated.
```

As far as I understand, the cause of the problem is that `/Users/<username>/go/pkg/mod/github.com/idavydov/go_fortran_test2@v0.0.0-20210913063354-750b058b70dc` is a read-only directory (`555`). And `gfortran` is trying to create a `hi.mod0`.

It is possible to overcome this issue by making a local directory with `go_fortran_test2` and providing a `replace` directive in `go.mod`. In this case gfortran is creating `hi.mod0` in a local directory which is writable. 


- [the module](https://github.com/idavydov/go_fortran_test2)
- [the executable](https://github.com/idavydov/go_fortran_test_run2)

#### Fortran library
##### mylib.f90
**Note**: This only fails if using fortran module.
```fortran
module hi

contains
    subroutine hello() bind(C)
        print *, "Hello from Fortran"
    end subroutine

end module hi
```

##### mymod.go
```go
package go_fortran_test2

// based on https://stackoverflow.com/a/53360659/1292467

// #cgo LDFLAGS: -lgfortran
// void hello();
import "C"

func Hello() {
	C.hello()
}
```
##### executable
```go
package main

import "github.com/idavydov/go_fortran_test2"

func main() {
	go_fortran_test2.Hello()
}
```

<!--
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
-->



### What did you expect to see?
Successful compilation.

### What did you see instead?
```
mylib.f90:8:13:

    8 | end module hi
      |             1
Fatal Error: Cannot open module file 'hi.mod0' for writing at (1): Permission denied
compilation terminated.
```