Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
idavydov opened this issue Sep 13, 2021 · 1 comment
Open

Comments

@idavydov
Copy link

@idavydov idavydov commented Sep 13, 2021

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

$ go version
go version go1.17.1 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users//Library/Caches/go-build"
GOENV="/Users//Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users//go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users//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//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"

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.
@idavydov
Copy link
Author

@idavydov idavydov commented Sep 13, 2021

Another work-around is to call:
CGO_FFLAGS="-J $TMPDIR -I $TMPDIR" CC=gcc-11 go run -x github.com/idavydov/go_fortran_test_run2@latest

But I think the proper fix is for cgo to pass -J $WORK/b002/ automatically.

@seankhliao seankhliao changed the title cgo fortran compilation: Fatal Error: Cannot open module file .mod0 for writing at (1): Permission denied cmd/go: fortran modules in go mod cache fail to compile (permission denied) Sep 15, 2021
@bcmills bcmills added this to the Backlog milestone Sep 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants