travis not find my package golang - go

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

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.

AWS CodeBuild Unable to "go get" a package

I have an AWS CodeBuild job, defined with the following buildspec file (apologies if indentation isn't reproduced correctly):
version: 0.2
env:
variables:
PACKAGE: "github.com/user/package"
phases:
install:
commands:
- mkdir -p ${GOPATH}/src/${PACKAGE}
- cp -a ${CODEBUILD_SRC_DIR}/. ${GOPATH}/src/${PACKAGE}
- cd ${GOPATH}/src/${PACKAGE} && go get ./...
build:
commands:
- cd ${GOPATH}/src/${PACKAGE} && go build -o ${CODEBUILD_SRC_DIR}/application
post_build:
commands:
- aws cloudformation package --template-file template.yml --output-template-file serverless.yml --s3-bucket some-bucket
artifacts:
files:
- serverless.yml
This fails in the install phase.
The go application I'm trying to build has several sub-packages and external dependencies. When running "go get ./..." I'm getting
cannot find package github.com/user/package/sub-package in any of:
/usr/local/go/src/github.com/user/package/sub-package(from $GOROOT) /go/src/github.com/user/package/sub-package(from $GOPATH) /codebuild/output/src708017258/src/github.com/user/package/sub-package
When "debugging" (by putting in some echos and listing the contents of the newley created folders) it appears that everything is in the right place and everything should just work.

How to freeze micro version with dependencies?

I want to build a docker image with a fixed version of micro and go dependencies. I plan to do it with dep:
git checkout git#github.com:micro/micro.git
dep ensure
git add Gopkg.toml
git add Gopkg.lock
# Build micro
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o micro ./main.go
# Build docker image
...
So, my question is does it the best solution to build consistent micro docker image?
An example of a Dockerfile can be:
FROM golang:1.9-alpine3.6 as builder
# Install package manager
RUN apk add --no-cache --virtual .go-dependencies git curl \
&& curl https://glide.sh/get | sh
# Copy files from context
WORKDIR /go/src/github.com/foo/bar
COPY . .
# Install project dependencies, test and build
RUN glide install \
&& go test ./... \
&& CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-w' -i -o ./entry ./main.go ./plugins.go
# Build final image with binary
FROM alpine:3.6
RUN apk add --update ca-certificates && \
rm -rf /var/cache/apk/* /tmp/*
WORKDIR /
COPY --from=builder /go/src/github.com/foo/bar/entry .
ENTRYPOINT [ "/entry" ]
And the glide.yaml would look like this:
package: .
import:
- package: github.com/micro/go-micro
version: ^0.3.0
subpackages:
- client
- server
- package: github.com/micro/go-plugins
version: ^0.6.1
subpackages:
- wrapper/trace/opentracing
- broker/nats
- transport/nats
- package: github.com/opentracing/opentracing-go
version: ^1
- package: github.com/openzipkin/zipkin-go-opentracing
version: ^0.3
testImport:
- package: github.com/golang/mock
subpackages:
- gomock
- package: github.com/smartystreets/goconvey
subpackages:
- convey
In my case, dep looks great and fast enough, moreover, it's official dependency manager in go so I think it's a right choice.

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

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/)

Travis CI + Go: creating a specific build flow for different OS

I have a Go project that I want to build with Travis-CI and deploy it to a specific provider.
I familiar with Gimme project that will use a cross-compilation to do so.
But because Travis already support linux and osx I only need this feature for Windows build.
The big motivation is, of course, to avoid cross-compilation run time error as there are plenty of it.
My question is how can I create, in the same .travis.yml file, a different build flow:
Native linux/os build (with "os" section).
Windows compilation using Gimmme
The .travis.yml file for the first option will look something like:
language: go
go:
- 1.5.1
branches:
only:
- master
os:
- osx
- linux
before_script:
- go get -d -v ./...
script:
- go build -v ./...
# - go test -v ./...
before_deploy:
- chmod +x ./before_deploy.sh
- ./before_deploy.sh
The .travis.yml file for the second option will look something like:
language: go
go:
- 1.5.1
branches:
only:
- master
env:
- GIMME_OS=windows GIMME_ARCH=amd64
before_script:
- go get -d -v ./...
script:
- go build -v ./...
# - go test -v ./...
before_deploy:
- chmod +x ./before_deploy.sh
- ./before_deploy.sh
Is there a nice clean way to combine these two (with Environment variables or any other crazy idea)?
It might be simple, but matrix environement can not be done for a specific OS ...
Then just select with local environement variable:
language: go
go:
- 1.5.1
branches:
only:
- master
os:
- osx
- linux
install:
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then
export GIMME_OS=windows;
export GIMME_ARCH=amd64;
fi
before_script:
- go get -d -v ./...
script:
- go build -v ./...
after_script:
- go test -v ./...
before_deploy:
- ./before_deploy.sh
An other way:
language: go
go:
- 1.5.1
branches:
only:
- master
matrix:
include:
- os: linux
env: GIMME_OS=windows; GIMME_ARCH=amd64;
- os: osx
before_script:
- go get -d -v ./...
script:
- go build -v ./...
after_script:
- go test -v ./...
before_deploy:
- ./before_deploy.sh
Note: the commande: - chmod +x ./before_deploy.sh can be directly done in your repository and commited on it ...
Note: The environament variable can be accessibe: http://docs.travis-ci.com/user/environment-variables/#Default-Environment-Variables or calling \printenv`

Resources