golang/go

cmd/go: fortran modules in go mod cache fail to compile (permission denied)

Open

#48,349 opened on Sep 13, 2021

View on GitHub
 (1 comment) (0 reactions) (0 assignees)Go (19,008 forks)batch import
GoCommandNeedsFixhelp wantedmodules

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

What version of Go are you using (go version)?

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

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.

Fortran library

mylib.f90

Note: This only fails if using fortran module.

module hi

contains
    subroutine hello() bind(C)
        print *, "Hello from Fortran"
    end subroutine

end module hi
mymod.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
package main

import "github.com/idavydov/go_fortran_test2"

func main() {
	go_fortran_test2.Hello()
}

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.

Contributor guide