Resolving 'cannot find package' error with vendor in go 1.7 - go

I have a project structure which looks like below:-
session-service
_libs //Contains all the external dependencies
api
constants
exceptions
idgen
jsonDecoder
log
model
monitor
persistence
redis
routes
src/bddtest/servicetest
util
Content of _libs looks like below:-
github.com
golang.org
x
net
gopkg.in
My Makefile looks like below:-
.PHONY: deploy
LOGLEVEL ?= 1
CONFIGFILE ?= 2
GOFLAGS ?= $(GOFLAGS:)
PWD = $(shell pwd)
export GOPATH = $(shell echo $$GOPATH):$(PWD)/_libs:$(PWD)
export GOBIN = $(PWD)/bin
export GOROOT = $(shell echo $$GOROOT)
deploy: clean build install
build:
#rm -rf pkg/ 2>/dev/null
#rm -rf _libs/pkg/ 2>/dev/null
#go build $(GOFLAGS) ./...
install:
#go install ./...
clean:
#go clean $(GOFLAGS) -i ./...
## EOF
Everything is working fine. Now I am thinking of moving to vendor. So I renamed my _libs to vendor and modified my Makefile like below:-
export GOPATH = $(shell echo $$GOPATH):$(PWD)
But after this I started getting the following error:-
vendor/golang.org/x/net/html/charset/charset.go:20:2: cannot find package "golang.org/x/text/encoding" in any of:
/Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/encoding (vendor tree)
/usr/local/go/src/golang.org/x/text/encoding (from $GOROOT)
/Users/debraj/golang/src/golang.org/x/text/encoding (from $GOPATH)
/Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/encoding
vendor/golang.org/x/net/html/charset/charset.go:21:2: cannot find package "golang.org/x/text/encoding/charmap" in any of:
/Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/encoding/charmap (vendor tree)
/usr/local/go/src/golang.org/x/text/encoding/charmap (from $GOROOT)
/Users/debraj/golang/src/golang.org/x/text/encoding/charmap (from $GOPATH)
/Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/encoding/charmap
vendor/golang.org/x/net/html/charset/charset.go:22:2: cannot find package "golang.org/x/text/encoding/htmlindex" in any of:
/Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/encoding/htmlindex (vendor tree)
/usr/local/go/src/golang.org/x/text/encoding/htmlindex (from $GOROOT)
/Users/debraj/golang/src/golang.org/x/text/encoding/htmlindex (from $GOPATH)
/Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/encoding/htmlindex
vendor/golang.org/x/net/html/charset/charset.go:23:2: cannot find package "golang.org/x/text/transform" in any of:
/Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/text/transform (vendor tree)
/usr/local/go/src/golang.org/x/text/transform (from $GOROOT)
/Users/debraj/golang/src/golang.org/x/text/transform (from $GOPATH)
/Users/debraj/golang/src/b/m/session-service/src/golang.org/x/text/transform
vendor/golang.org/x/net/http2/h2i/h2i.go:38:2: cannot find package "golang.org/x/crypto/ssh/terminal" in any of:
/Users/debraj/golang/src/b/m/session-service/vendor/golang.org/x/crypto/ssh/terminal (vendor tree)
/usr/local/go/src/golang.org/x/crypto/ssh/terminal (from $GOROOT)
/Users/debraj/golang/src/golang.org/x/crypto/ssh/terminal (from $GOPATH)
/Users/debraj/golang/src/b/m/session-service/src/golang.org/x/crypto/ssh/terminal
Environment:-
go version go1.7.3 darwin/amd64
Mac OS X 10.11.6
Can someone let me know why I am getting the above errors with vendor but everything works fine with _libs?
UPDATE
In my local the newlines in the output of $(go list ./... | grep -v /vendor/) was causing some problem. So to resolve this I had to modify the jimb 's solution a bit. I introduced a variable in Makefile PKG = $(shell go list ./... | grep -v /vendor/ | tr "\n" " ") and then used that variable in go install & go build like #go build $(GOFLAGS) $(PKG)

The _libs directory starts with _, and is ignored by the go tool. When you move the packages to vendor/, the ./... wildcard now includes all packages in the vendor directory.
You should explicitly list the package you want to install, rather than rely on the ./... wildcard. If you still want the wildcard behavior, you can use go list ./... and filter any package containing a vendor/ directory in their path. Depending on your specific needs, this could be as simple as:
go install $(go list ./... | grep -v vendor/)

Related

local build successful while CircleCI build failing

I am trying to experiment a bit with imports and now stuck on the station where simple import of package is failing in CircleCI while it is buildable locally successfully. Could anybody please share what is wrong or what I am doing wrong that is not letting code to be built in CircleCI?
Repo structure:
- project
--- main.go
--- graphic
----- graphics.go
main.go import definition:
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
graphic "../project/graphic"
)
graphics.go import definition:
package graphic
import (
"fmt"
"io/ioutil"
"log"
"github.com/fogleman/gg"
)
CircleCI config.yml:
version: 2
jobs:
build:
docker:
- image: circleci/golang:1.9
working_directory: /go/src/github.com/<my-account>/project
steps:
- checkout
# specify any bash command here prefixed with `run: `
# - run: go get -v -t -d ./...
- run:
name: run filesystem path configuration
command: |
pwd
pwd -P
echo $GOROOT
echo $GOPATH
export GOBIN=$GOPATH/bin
echo $GOBIN
export PATH=$PATH:$GOBIN
ls -latr $GOROOT
ls -latr $GOPATH
ls -latr $GOBIN
go env
- run:
name: run dependecy managament
command: |
go get -v -u github.com/golang/dep/cmd/dep
go get -v -u github.com/fogleman/gg
go get -v -u github.com/aws/aws-sdk-go/aws
go get -v -u github.com/aws/aws-sdk-go/aws/session
go get -v -u github.com/aws/aws-sdk-go/service/cloudwatch
- run:
name: run documentation
command: |
godoc -v -index -timestamps main
- run:
name: run tests
command: |
go vet -v ./...
- run:
name: run build and deploy
command: |
dep init
mkdir build
dep ensure
go build -v -o build/main.exe main.go
- run:
name: run qa
command: go test -v main.go
- store_artifacts:
path: ./build
Failing error message:
#!/bin/bash -eo pipefail
dep init
mkdir build
dep ensure
go build -v -o build/main.exe main.go
Using ^1.3.0 as constraint for direct dep github.com/fogleman/gg
Locking in v1.3.0 (0403632) for direct dep github.com/fogleman/gg
Locking in master (cff245a) for transitive dep golang.org/x/image
Using ^1.23.3 as constraint for direct dep github.com/aws/aws-sdk-go
Locking in v1.23.3 (fbdf1bd) for direct dep github.com/aws/aws-sdk-go
Locking in master (e2365df) for transitive dep github.com/golang/freetype
Locking in (c2b33e84) for transitive dep github.com/jmespath/go-jmespath
graphic/graphics.go:8:2: cannot find package "_/go/src/github.com/<my-account>/project/vendor/github.com/fogleman/gg" in any of:
/usr/local/go/src/_/go/src/github.com/<my-account>/project/vendor/github.com/fogleman/gg (from $GOROOT)
/go/src/_/go/src/github.com/<my-account>/project/vendor/github.com/fogleman/gg (from $GOPATH)
Exited with code 1
i think for your case will solve if you use package manager, like DEP or other.

cannot find package "bufio" in any of

I have get go1.11.5 installed by downloading precompiled binary package directly on my ubuntu18.04. Now i want to install go1.12 by building from source, so i follow with Installing Go from source.
I set GOROOT_BOOTSTRAP=~/goroot_bootstrap
$ mkdir -p ~/goroot_bootstrap/bin
$ cp /usr/local/go/bin/go ~/goroot_bootstrap/bin/
$ echo "export GOROOT_BOOTSTRAP=~/goroot_bootstrap" >> ~/.bashrc
$ source ~/.bashrc
Then, get source
$ git clone -b release-branch.go1.12 https://github.com/golang/go.git ~/github.com/golang/go
Build
$ cd ~/github.com/golang/go/src
$ ./all.bash
It failed, and tips:
Building Go cmd/dist using /home/pi/goroot_bootstrap.
cmd/dist/imports.go:12.2: cannot find package "bufio" in any of:
/home/pi/goroot_bootstrap/src/bufio (from $GOROOT)
/home/pi/go/src/bufio (from $GOPATH)
...
But if i set GOROOT_BOOTSTRAP=/usr/local/go, it will success.
Then, i read source code at src/make.bash.
166 rm -f cmd/dist/dist
167 GOROOT="$GOROOT_BOOTSTRAP" GOOS="" GOARCH="" GO111MODULE=off "$GOROOT_BOOTSTRAP/bin/go" build -o cmd/dist/dist ./cmd/dist
The bootstrap toolchains build cmd/dist with GOROOT="$GOROOT_BOOTSTRAP" at line 167.
If i have GOROOT_BOOTSTRAP=/usr/local/go set and success, which means building still depending old package such as bufio.
So, why it failed when setting GOROOT_BOOTSTRAP=~/goroot_bootstrap?
Do building depends old package if setting GOROOT_BOOTSTRAP=/usr/local/go?
Which one is the right method to install from source?
Any help will be grateful.

go test foo - cannot find package foo

I have this directory structure:
https://github.com/netjet-chrome-extension/netjet-mono/tree/master/examples/projects/golang
I try to run test.sh, which consists of:
#!/usr/bin/env bash
cd "$(dirname "$BASH_SOURCE")"
export GOPATH="$PWD"
go test sourcegraph_go_selenium
but I get this error:
can't load package: package sourcegraph_go_selenium: cannot find
package "sourcegraph_go_selenium" in any of:
/usr/lib/go-1.10/src/sourcegraph_go_selenium (from $GOROOT)
/home/oleg/codes/netjet/netjet-mono/examples/projects/golang/src/sourcegraph_go_selenium
(from $GOPATH)
GOPATH is set correctly, so why can't it find the sourcegraph_go_selenium package? this package is right there, in src/sourcegraph_go_selenium...?
Try this:
go test sourcegraph_go_selenium/...

travis not find my package golang

my structure project
currency-quote-api -
|
|- scraping/ file.go
|- api/ file.go
|- tests/ test.go
|- main.go
When I run the tests in travis CI i get the following error
tests/scraping_test.go:4:2: cannot find package "currency-quote-api/scraping" in any of:
/home/travis/.gimme/versions/go1.10.2.linux.amd64/src/currency-quote-api/scraping (from $GOROOT)
/home/travis/gopath/src/github.com/matheussilva97/currency-quote-api/Godeps/_workspace/src/currency-quote-api/scraping (from $GOPATH)
/home/travis/gopath/src/currency-quote-api/scraping
what I doing wrong?
my .travis.yml
sudo: false
language: go
go:
- 1.10.2
notifications:
email: false
before_script:
- go get github.com/gorilla/mux
- go get github.com/PuerkitoBio/goquery
script:
- go test -v ./tests/
Why not implicitly get all packages:
before_script:
- go get -t -v ./...
For test, since each test can have its own dependency (depending on which package you put your test in), you should have a script like this:
for d in $(go list ./... | grep -v vendor); do
go test -race -coverprofile=profile.out -covermode=atomic $d
done

Go install: “Can't load package” (even though GOPATH is set)

I'm just getting started with the Go programming language and installed Go using the Windows installer from the website. I tested installation by using go run hello.go and that works. The problem comes when I try to build my first program:
$ echo $GOROOT
C:\Go\
$ echo $GOPATH
/cygdrive/c/Users/Paul/Documents/Home/go
mkdir -p $GOPATH/src/hello
Inside that directory I have a simple hello.go program:
package main
import "fmt"
func main() {
fmt.Printf("Hello, world.\n")
}
The problem comes when I try to build and install:
$ go install hello
can't load package: package hello: cannot find package "hello" in any of:
C:\Go\src\hello (from $GOROOT)
\cygdrive\c\Users\Paul\Documents\Home\go\src\hello (from $GOPATH)
GOPATH environment variable must contain valid path.
\cygdrive\c\Users\Paul\Documents\Home\go\src\hello is not a valid path on Windows.
Try setting GOPATH=c:\Users\Paul\Documents\Home\go instead.

Resources