-1

When running go install on my cmd folder I get the error:

go install cmd/go: copying 
/var/folders/wh/9y99138n2w1bvcwxz3tbb8zw0000g
n/T/go-build195667123/b150/exe/a.out: open 
/usr/local/go/bin/go: permission denied

It looks like my env variables are set correctly:

GOCACHE="/Users/<USER>/Library/Caches/go-build"
GOENV="/Users/<USER>/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/<USER>/src/unbias"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"

Here's my directory of my current project: which is inside my go path: /Users/<USER>/src/unbias

enter image description here

Since it looks like the permission error is not in my project but in my go install path, I am not sure if I should change ownership of such files.

total 37032
drwxr-xr-x   4 root  wheel   128B Jul 16 15:53 .
drwxr-xr-x  20 root  wheel   640B Jul 16 15:29 ..
-rwxr-xr-x   1 root  wheel    15M Jul 16 15:52 go
-rwxr-xr-x   1 root  wheel   3.4M Jul 16 15:53 gofmt

My go.mod is also:

module cmd

go 1.14
Jacob B
  • 694
  • 10
  • 21
  • 3
    The relevant part of the error is `open /usr/local/go/bin/go: permission denied`. There are two questions: 1) what is "my `cmd` folder"?; 2) what you're actually trying to do? I mean what you're trying to `go install`? – kostix Jul 26 '20 at 11:04
  • @kostix my cmd folder refers to the cmd folder under my current workspace. I am trying to go install just a simple hello world under my current directory which is my current go path I updated the post with relevant info. – Jacob B Jul 26 '20 at 18:34
  • You haven’t shown what GOBIN is set to. – JimB Jul 26 '20 at 18:56
  • @JimB GOBIN is not set as per the documentation it should not need to be set for package level `go install` as long as GOPATH is set. – Jacob B Jul 26 '20 at 18:58
  • I understand that, but `go env` will still list it and it appears you are trying to install somewhere you aren’t expecting to – JimB Jul 26 '20 at 19:53
  • I see, in `go env` it is `GOBIN=""` – Jacob B Jul 26 '20 at 19:59
  • You also appear to be trying to use GOPATH but it’s set incorrectly. I would suggest bypassing GOPATH and following “How to Write Go Code” – JimB Jul 26 '20 at 19:59
  • I have already read through and followed "How to Write Go Code" in which I had it set to `$HOME/go` like they do I have changed it for a specific project to save to the bin under the namespace. – Jacob B Jul 26 '20 at 20:05
  • But you’re not in a valid GOPATH path, and you don’t appear to have a go.mod. – JimB Jul 26 '20 at 20:10
  • Okay I see what you are saying I set GOPATH=$HOME and set go.mod with `go init` in the current path. I will update the post with the `cat` of go.mod. But the issue persists if I `go install cmd/` if I first cd into `cmd` and then `go install .` it does install to `$HOME/go` without any issues but it is named `cmd` not `main` it is also not the correct location as I want to install to the bin of my project. – Jacob B Jul 26 '20 at 20:21
  • If I set `GOBIN` to my dir it will save in the bin file but this is not the expected behavior. I am also not able to go install out side of the package directory. – Jacob B Jul 26 '20 at 20:28
  • I think I found the error `go install cmd` was installing something else called `cmd` not my local directory `cmd` in order to solve this I have to cd into the directory and `go install .` – Jacob B Jul 26 '20 at 21:43

1 Answers1

2

It means that either <PATH_TO>/cmd permission is not writable or $GOPATH/pkg/cmd/go is not readable for your User.

You may:

  1. use sudo chmod -R 777 <PATH_TO>/cmd
  2. using govendor or go mod if it's outside of GOPATH.
  3. try to create inside $GOPATH instead (you haven't stated if <PATH_TO>/cmd inside $GOPATH
  4. If <PATH_TO>/cmd is writable, use sudo for now and change the ownership to <User> again by chown

Check the permission on <PATH_TO>/cmd or $GOPATH/pkg/cmd/go again by ls -lah <PATH_TO>/cmd, it should be readable and writable.

Fahim Bagar
  • 798
  • 7
  • 17
  • That would not make much sense as this is a folder inside of my $HOME directory which is under my $GOPATH. CHMOD 777 on my current path did not work but I do have full permissions. – Jacob B Jul 26 '20 at 18:37
  • what do you mean by chmod 777 on your current path not working? can you share the permission of your GOPATH by ls -lah ? – Fahim Bagar Jul 26 '20 at 18:49
  • Oh by that I mean that after using chmod 777 and having full permissions in my directory it still throws the same error. – Jacob B Jul 26 '20 at 18:51
  • Have you tried recursively with -R? I suspect one of your library inside main.go have wrong permission or you haven't set PATH=$PATH:$GOROOT/bin or you haven't set $GOTOOLDIR=/usr/local/go/pkg/tool/darwin_amd64" (check it on this post: https://stackoverflow.com/questions/27273053/permission-denied-error-for-go-tools/27273294#27273294) – Fahim Bagar Jul 26 '20 at 18:59
  • My GOTOOLDIR is `GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"` I did set the path recursively with -R. I think the issue is with permissions on my root folders `/usr/local` – Jacob B Jul 26 '20 at 19:02
  • Try it, and if it works then write your answer and mark it solved so others can relate to this question – Fahim Bagar Jul 26 '20 at 19:06