`go run: cannot run non-main package` as Unprivileged User - go

I'm encountering an error with a recently-compiled golang compiler, but only as an unprivileged user. I'm hoping someone could provide advice on further troubleshooting ideas.
As root:
$ cd $HOME && pwd
/root
$ mkdir -pv $HOME/go/src/hello
/bin/mkdir: created directory 'go'
/bin/mkdir: created directory 'go/src'
/bin/mkdir: created directory 'go/src/hello'
$ cd go/src/hello
$ cat > hello.go << EOF
> package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello, world\n")
> }
> EOF
$ go build
$ ls
hello hello.go
$ ./hello
hello, world
$ go run hello.go
hello, world
As an unprivileged user:
$ cd $HOME && pwd
/usr/src/libcap
$ mkdir -pv $HOME/go/src/hello
/bin/mkdir: created directory 'go'
/bin/mkdir: created directory 'go/src'
/bin/mkdir: created directory 'go/src/hello'
$ cd go/src/hello
$ cat > hello.go << EOF
> package main
>
> import "fmt"
>
> func main() {
> fmt.Printf("hello, world\n")
> }
> EOF
$ go build
can't load package: package libcap/go/src/hello: cannot find package "libcap/go/src/hello" in any of:
/usr/src/libcap/go/src/hello (from $GOROOT)
/usr/src/libcap/go/src/libcap/go/src/hello (from $GOPATH)
$ ls
hello.go
$ go run hello.go
go run: cannot run non-main package
System information:
$ go version
go version go1.12.2 gccgo (GCC) 9.2.0 linux/amd64
I'm running in an LFS chroot environment and first discovered the issue while compiling the libcap package, which contains a go module, in chapter 6.28 of the 20200119-systemd book. The host is a Debian live cd:
$ uname -a
Linux debian 4.19.0-6-amd64 #1 SMP Debian 4.19.67-2+deb10u1 (2019-09-20) x86_64 GNU/Linux
The go tests appear to be fairly nominal, with very few unexpected failures (command executed from gcc build directory):
$ ../contrib/test_summary | grep -A7 Summ > log
$ less log
...
=== go Summary ===
# of expected passes 7349
# of unexpected failures 1
# of expected failures 1
# of untested testcases 6
# of unsupported tests 1
/usr/src/gcc/gcc-9.2.0/build/gcc/gccgo version 9.2.0 (GCC)
--
...
=== gotools Summary ===
# of expected passes 216
# of unexpected failures 4
# of untested testcases 159
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
...
=== libgo Summary ===
# of expected passes 178
# of unexpected failures 6
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
=== libgomp Summary ===
# of expected passes 6458
# of expected failures 2
# of unsupported tests 349
/usr/src/gcc/gcc-9.2.0/build/./gcc/gccgo version 9.2.0 (GCC)
--
...

Related

gccgo build can not find custom pakage

when use the gccgo build one single go file is ok, but whn I build a large multi custom pacage go mod project, the project have some sub package(such as app/ app/core/server etc) not build success.
how to fix this? anyhelp? I build with -x and see some debug info.
go build -x -gccgoflags -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go
the error is:
WORK=/tmp/go-build1609005358
mkdir -p $WORK/b001/
cd $WORK
/opt/gccgo/bin/gccgo -fgo-importcfg=/dev/null -c -x c - -o /dev/null || true
mkdir -p $WORK/b001/_importcfgroot_/github.com/gin-gonic
ln -s /home/liangqi1/.cache/go-build/8a/8ae3d6bb3097698b7be6547599d7a61c9c5c54cca0be55c007ca1c8386d1188c-d $WORK/b001/_importcfgroot_/github.com/gin-gonic/libgin.a
mkdir -p $WORK/b001/_importcfgroot_/github.com/logrusorgru
ln -s /home/liangqi1/.cache/go-build/6f/6fb602f3c990310188a3e921909d152810f07d50f1ab4c621ab71b87afd4942d-d $WORK/b001/_importcfgroot_/github.com/logrusorgru/libaurora.a
mkdir -p $WORK/b001/_importcfgroot_/go.uber.org
ln -s /home/liangqi1/.cache/go-build/c0/c0ac9f2f0ebb74e0997bfa1d72d21a79ce906ed736197f83402365203094c3a9-d $WORK/b001/_importcfgroot_/go.uber.org/libautomaxprocs.a
/opt/gccgo/bin/gccgo -ffile-prefix-map=a=b -c -x c - -o /dev/null || true
cd /home/liangqi1/gccgo_demo
/opt/gccgo/bin/gccgo -c -g -m64 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -fgo-relative-import-path=_/home/liangqi1/gccgo_demo -o $WORK/b001/_go_.o -I $WORK/b001/_importcfgroot_ -Wl,-R,/opt/gccgo/lib64/ ./main.go
# command-line-arguments
./main.go:7:23: 错误:import file ‘gccgo_demo/app’ not found
7 | "gccgo_demo/app"
| ^
./main.go:8:35: 错误:import file ‘gccgo_demo/app/core/server’ not found
8 | "gccgo_demo/app/core/server"
| ^
./main.go:24:19: 错误:reference to undefined name ‘app’
24 | if err := app.Init(prjHome); err != nil {
| ^
./main.go:29:12: 错误:reference to undefined name ‘app’
29 | if app.IsProd() {
| ^
./main.go:35:9: 错误:reference to undefined name ‘server’
35 | server.Run()
| ^
the gccgo just clone from gcc git:
gccgo (GCC) 12.0.1 20220217
go version is 1.16.4
my project struct like flow:
gccgo_demo
├── app
│   ├── app.go
│   ...
├── go.mod
├── go.sum
├── main.go
... other code
the go mod name is gccgo_demo so , the app/app.go has some import like gccgo_demo/app path.
I don't see your code but I guess the problem that you do not pass the include path.
The cgo tool will always invoke the C compiler with the source file's directory in the include path; i.e. -I${SRCDIR} is always implied. This means that if a header file foo/bar.h exists both in the source directory and also in the system include directory (or some other place specified by a -I flag), then "#include <foo/bar.h>" will always find the local version in preference to any other version.
So just do smth like
go build -x -gccgoflags -I/path/to/your/file -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go
More details https://pkg.go.dev/cmd/cgo

Golang net.LookupIP not working when compiled with GOARCH=amd64?

here is a simple program:
package main
import (
"fmt"
"net"
)
func main() {
nameserver, _ := net.LookupNS("mylab.local")
for _, ns := range nameserver {
addrs, errIP := net.LookupIP(ns.Host)
if errIP == nil {
fmt.Printf("%s\n",addrs[0].String())
} else {
fmt.Printf("%s\n", errIP.Error())
}
}
}
Here is the ouput compiled with GOARCH=amd64:
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$ ./test
?
The output compiled with GOARCH=386:
$ GOOS=linux GOARCH=386 go build -o test test.go
$ ./test
192.168.56.3
I'm working on a "Ubuntu 18.04.6 LTS".
Is there something obvious I missed ?
Thank you
Update 1: I updated the code in order to print the errIP
The output compiled GOARCH=amd64:
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$ ./test
lookup dc01.mylab.local.: no such host
$ ping -c1 dc01.mylab.local
PING dc01.mylab.local (192.168.56.3) 56(84) bytes of data.
64 bytes from dc01.mylab.local (192.168.56.3): icmp_seq=1 ttl=128 time=0.561 ms
Update 2: #hobbs
Here is the output with the environment var:
$ export GODEBUG=netdns=1
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$./test
go package net: dynamic selection of DNS resolver
&{dc01.mylab.local.}
lookup dc01.mylab.local.: no such host
Update 3: Ok solved, thank you hobbs. I found that with arch 386, the resolver used was go. So after that I had a look at the documentation of the net package. I compiled as follow:
$ export GODEBUG=netdns=go
$ GOOS=linux GOARCH=amd64 go build -o test test.go
$ ./test
192.168.56.3

Golang: go build failing when called as part of makefile

I have the following make file:
default: builddocker
setup:
go get github.com/golang/protobuf/proto
go get github.com/golang/protobuf/protoc-gen-go
go get golang.org/x/net/context
go get google.golang.org/grpc
buildgo:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
builddocker:
docker build -t johnwesonga/grpc/customer-server -f ./Dockerfile.build .
docker run -t johnwesonga/grpc/customer-server /bin/true
docker cp `docker ps -q -n=1`:/main .
chmod 755 ./main
docker build --rm=true --tag=johnwesonga/grpc/customer-server -f Dockerfile.static .
My Dockerfile.build has:
FROM golang
ADD Makefile /
WORKDIR /
RUN make setup
RUN make buildgo
CMD ["/bin/bash"]
But every time I run "make builddocker" I get the following error:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-s" -a -installsuffix cgo -o main ./customer-server/
can't load package: package ./customer-server: open /customer-server: no such file or directory
make: *** [buildgo] Error 1
Makefile:10: recipe for target 'buildgo' failed
The command '/bin/sh -c make buildgo' returned a non-zero code: 2
make: *** [builddocker] Error 2
But if I run "Make buildgo" it works just fine, any idea why this is failing?
FYI my source tree as follows:
/go/src/github.com/johnwesonga/grpc/MakeFile
/go/src/github.com/johnwesonga/grpc/customer-server
/go/src/github.com/johnwesonga/grpc/customer-server/main.go

`command -v` seems to only search in /usr/bin

For some shell script I need to store output of command -v foo in variables. I am checking if some binaries exist on the system. When I execute command -v foo in terminal, I get an output but when I execute command -v foo inside a shell script I don't get any output.
➜ tools git:(install-script) ✗ command -v node
/usr/local/bin/node
check_deps() {
declare -A deps
deps=( ['git']=`command -v git`
['gem']=`command -v gem`
['node']=$(command -v node))
# ['redis-server']=command -v redis-server
# ['postgres']=command -v psql
# ['sass']=command -v sass
# ['gulp']=command -v gulp
# ['bower']=command -v bower )
for each in ${!deps[#]};
do
echo $each ${deps[$each]}
done
}
check_deps
Output is:
node
git /usr/bin/git
gem /usr/bin/gem
[Finished in 0.0s]
What's happening? How do I fix this?
I was using Sublime Text which had a plugin called Bash Build system. Basically it got me a file which didn't have /usr/local/bin, this build syntax now returns proper command -v output:
{
"cmd" : ["bash", "$file"],
"selector" : "source.shell",
"osx": {
"path" : "$PATH:/usr/local/bin"
}
}

When I type $ gcc into the mac Terminal I get a "-bash: gcc: command not found"

However, it works when I type:
$ gcc-4.2
Find where gcc-4.2 is installed and go into that directory:
$ cd $(which gcc-4.2)
Check if there is a gcc symlink:
$ ls gcc
If there isn't one, create it:
$ ln -s gcc-4.2 gcc
I got it: all was well after I installed command line tools in Xcode > Preferences > Downloads.

Resources