Testing Go project with gomodules in Gitlab CI fails with "package is not in GOROOT"

1k Views Asked by At

I'm trying to build and test a Go project on Gitlab CI using the following .gitlab-ci.yml:

image: golang:latest

stages:
  - prepare
  - test
  - build
  - package

format:
  stage: prepare
  script:
    - make gofmt    

test:
  stage: test
  script:
    - make test

test race condition:
  stage: test
  script:
    - make race

coverage-html:
  stage: test
  script:
    - make cov

build win binary:
  stage: build
  script:    
    - make win

build linux binary:
  stage: build
  script:    
    - make linux
    
build mac binary:
  stage: build
  script:    
    - make mac

and here is the Makefile used in the pipeline:

# Go parameters
GOCMD=go
GOBUILD=$(GOCMD) build
GOCLEAN=$(GOCMD) clean
GOTOOL=$(GOCMD) tool
GOTEST=$(GOCMD) test
GOTESTRACE=$(GOTEST) -race
GOGET=$(GOCMD) get
GOFMT=$(GOCMD)fmt

BINARY_NAME=app
BINARY_DARWIN=$(BINARY_NAME)_darwin
BINARY_LINUX=$(BINARY_NAME)_linux
BINARY_WIN=$(BINARY_NAME)_win
    
all: build linux win

build: ## Download dependecies and Build the default binary      
        $(GOBUILD) -o $(BINARY_NAME) -v $(CURDIR)/cmd/app

linux: ## Build linux binary
        CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) cmd/app -o $(BINARY_LINUX) -v $(CURDIR)/cmd/app

win: ## Build windows binary
        CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BINARY_WIN).exe -v  $(CURDIR)/cmd/app

mac: ## Build macOs binary
        CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BINARY_DARWIN) -v    $(CURDIR)/cmd/app

test: ## Run tests for the project
        $(GOTEST) -count=1 -coverprofile=cover.out -short -cover -failfast ./... -v

race: ## Run tests for the project (while detecting race conditions)
        $(GOTESTRACE) -coverprofile=cover.out -short -cover -failfast ./...

cov: test ## Run tests with HTML for the project
        $(GOTOOL) cover -html=cover.out

gofmt: ## gofmt code formating
    @echo Running go formating with the following command:
    $(GOFMT) -e -s -w .

I'm using gomodule to manage dependencies. What I'm struggling to understand is that if I run jobs in build stage first, the pipeline is able to download dependencies and build the binaries successfuly. But when jobs in the test stage are run, they all fail with the following error:

main.go:3:8: package app/command is not in GOROOT (/usr/local/go/src/app/command)
make: *** [Makefile:31: test] Error 1
Cleaning up file based variables
ERROR: Job failed: command terminated with exit code 1

Is there a different setting for testing gomodule based projects on Gitlab CI that I need to configure?

0

There are 0 best solutions below