Vendor-isolated buld of Golang program

Asked by Andrey Vukolov

Hi there! I am trying to package the program built with Golang. I included the vendor code package inside the source archive, and added the following lines to my rules file:

 $(eval PATH := /usr/lib/go-1.21/bin:$(PATH))
 export GOPATH="./gopath"
 go env -w GOPATH='./gopath'
 go env -w GOFLAGS='-mod=vendor'
 go env -w GOBIN=''
 go env -w GOCACHE='./gocache'

to prevent the Go compiler from trying to access the network. However, I see that the build fails due to insufficient permissions (the log is available here - https://launchpadlibrarian.net/741542608/buildlog_ubuntu-jammy-amd64.ipfs-kubo_0.27.5_BUILDING.txt.gz):

go env -w GOFLAGS='-mod=vendor'
go: go: could not create module cache: mkdir /sbuild-nonexistent: permission denied
make[1]: *** [debian/rules:7: build] Error 1

Could you point me to any possible workaround for this error?

Question information

Language:
English Edit question
Status:
Solved
For:
Launchpad itself Edit question
Assignee:
No assignee Edit question
Solved by:
Andrey Vukolov
Solved:
Last query:
Last reply:
Revision history for this message
Simone Pelosi (pelpsi) said (last edit ):
#1

Hey! Did you try locally the code? Are you able to build it without errors ?

Revision history for this message
Andrey Vukolov (twdragon) said :
#2

In the end, I revealed the root of the problem. Go compiler requires the HOME environment variable to be set and contain an absolute path to a writable directory. I have solved this issue by running my recursive build with the following rules.mk:

build:
 $(eval PATH := /usr/lib/go-1.21/bin:$(PATH))
 $(eval HOME := $(CURDIR))
 export HOME="$(CURDIR)"
 @echo "HOME = $(HOME)"
 export GOENV='off'
 export GO111MODULE='off'
 export GOPATH="./gopath"
 export GOMODCACHE='./gomod'
 export GOTMPDIR="./gotemp"
 export GOTOOLCHAIN="local"
 export GOFLAGS="-mod=vendor"
 make build

Surprisingly, without 'export' command, the setup does neither work, nor without the 'eval' command.