我正在经历一些奇怪的不允许错误,我不知道从哪里来。
$ go run .
Hello from go
$ make run
go run .
make: go: Permission denied
make: *** [Makefile:2: run] Error 127
$ make run2
echo "Make says hello" ; go run .
Make says hello
Hello from go
$ cat Makefile
run:
go run .
run2:
echo "Make says hello" ; go run .
$ cat main.go
package main
import "fmt"
func main() {
fmt.Println("Hello from go")
}
我的终端正在Ubuntu22.04上运行。
我的run
目标和直接运行的go之间有什么区别,可以导致权限被拒绝的错误?
run
和run2
之间有什么不同,允许它在一种而不是在另一种情况下工作?
编辑:使用-d
/ --trace
运行make
$ make -d run
<...snip...>
No need to remake target 'Makefile'.
Updating goal targets....
Considering target file 'run'.
File 'run' does not exist.
Finished prerequisites of target file 'run'.
Must remake target 'run'.
go run .
make: go: Permission denied
make: *** [Makefile:2: run] Error 127
$ make --trace run
Makefile:2: target 'run' does not exist
go run .
make: go: Permission denied
make: *** [Makefile:2: run] Error 127
$ make --trace run2
Makefile:5: target 'run2' does not exist
echo "Make says hello"; go run .
Make says hello
Hello from go
发布于 2022-09-30 17:20:29
这是由于gnulib中的一个bug (实际上它是gnulib中的一个bug )。这意味着您在go
上的某个目录中有一个名为go
的目录(在包含go
可执行文件的实际目录之前)。
因此,如果您有一个目录/usr/bin/go/.
,并且在您的PATH
上有/usr/bin
,您将看到这个问题。
您应该检查您的PATH
并确保删除包含此类子目录的所有目录。如果您无法从PATH
中删除该目录(在PATH
上需要包含子目录的目录是不常见的,但我想这是可能的),而且您不能将go
目录重命名为其他目录,您必须通过添加一个特殊字符来确保GNU调用一个shell。仅仅是;
就足够了:
run:
go run . ;
发布于 2022-09-30 13:53:35
您所遇到的问题很可能是由于Makefile
执行的shell和shell环境不同造成的。例如,如果您有一个用于go
的shell别名,这个别名在Makefile
中是不可见的,或者如果您的shell rc文件中有一个自定义路径,那么它对Makefile
是不可见的。很难猜出区别在哪里。
您可能希望尝试通过在Makefile
中执行以下操作来调试此问题
echo $(PATH)
command -v go
并在shell中运行相同的命令并比较结果。
注意,Makefile
的默认外壳是/bin/sh
,而可能有bash
或zsh
。
下面是配置Makefile
构建的一些方便的默认设置:
LANG=en_US.UTF-8
SHELL=/bin/bash
.SHELLFLAGS=--norc --noprofile -e -u -o pipefail -c
https://stackoverflow.com/questions/73908737
复制相似问题