@@ -19,6 +19,9 @@ const (
1919 // GoModCacheKey is the key used to identify the go module cache in the buildkit cache.
2020 // It is exported only for testing purposes.
2121 GomodCacheKey = "dalec-gomod-proxy-cache"
22+ // BuildArgDalecGomodProxy is the frontend build arg used to override GOPROXY
23+ // for gomod dependency generation.
24+ BuildArgDalecGomodProxy = "DALEC_GOMOD_PROXY"
2225)
2326
2427func (g * GeneratorGomod ) processBuildArgs (args map [string ]string , allowArg func (key string ) bool ) error {
@@ -63,16 +66,35 @@ func (s *Spec) HasGomods() bool {
6366 return false
6467}
6568
66- func withGomod (g * SourceGenerator , srcSt , worker llb.State , subPath string , credHelper llb.RunOption , opts ... llb.ConstraintsOpt ) func (llb.State ) llb.State {
69+ type gomodGeneratorOpts struct {
70+ sourceName string
71+ gen * SourceGenerator
72+ sourceState llb.State
73+ worker llb.State
74+ credHelper llb.RunOption
75+ extraEnvs map [string ]string
76+ constraints []llb.ConstraintsOpt
77+ }
78+
79+ func (opts gomodGeneratorOpts ) gomodProxy () string {
80+ if opts .extraEnvs == nil {
81+ return ""
82+ }
83+ return opts .extraEnvs ["GOPROXY" ]
84+ }
85+
86+ func withGomod (gomodOpts gomodGeneratorOpts ) func (llb.State ) llb.State {
6787 return func (in llb.State ) llb.State {
6888 const (
6989 workDir = "/work/src"
7090 scriptMountpoint = "/tmp/dalec/internal/gomod"
7191 gomodDownloadWrapperBasename = "go_mod_download.sh"
7292 )
7393
74- joinedWorkDir := filepath .Join (workDir , subPath , g .Subpath )
75- srcMount := llb .AddMount (workDir , srcSt )
94+ g := gomodOpts .gen
95+ opts := gomodOpts .constraints
96+ joinedWorkDir := filepath .Join (workDir , gomodOpts .sourceName , g .Subpath )
97+ srcMount := llb .AddMount (workDir , gomodOpts .sourceState )
7698
7799 paths := g .Gomod .Paths
78100 if g .Gomod .Paths == nil {
@@ -86,23 +108,30 @@ func withGomod(g *SourceGenerator, srcSt, worker llb.State, subPath string, cred
86108 scriptPath := filepath .Join (scriptMountpoint , gomodDownloadWrapperBasename )
87109
88110 for _ , path := range paths {
89- in = worker . Run (
111+ runOpts := []llb. RunOption {
90112 // First download the go module deps to our persistent cache
91113 // Then set the GOPROXY to the cache dir so that we can extract just the deps we need
92114 // This allows us to persist the module cache across builds and avoid downloading
93115 // the same deps over and over again.
94- ShArgs (`set -e; GOMODCACHE="${TMP_GOMODCACHE}" ` + scriptPath + `; GOPROXY="file://${TMP_GOMODCACHE}/cache/download" ` + scriptPath ),
116+ ShArgs (`set -e; GOMODCACHE="${TMP_GOMODCACHE}" ` + scriptPath + `; GOPROXY="file://${TMP_GOMODCACHE}/cache/download" ` + scriptPath ),
95117 g .withGomodSecretsAndSockets (),
96118 llb .AddMount (scriptMountpoint , script ),
97119 llb .AddEnv ("GOPATH" , "/go" ),
98- credHelper ,
120+ gomodOpts . credHelper ,
99121 llb .AddEnv ("TMP_GOMODCACHE" , proxyPath ),
100122 llb .AddEnv ("GIT_SSH_COMMAND" , "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no" ),
101123 llb .Dir (filepath .Join (joinedWorkDir , path )),
102124 srcMount ,
103125 llb .AddMount (proxyPath , llb .Scratch (), llb .AsPersistentCacheDir (GomodCacheKey , llb .CacheMountShared )),
104126 WithConstraints (opts ... ),
105127 g .Gomod ._sourceMap .GetLocation (in ),
128+ }
129+ if gomodProxy := gomodOpts .gomodProxy (); gomodProxy != "" {
130+ runOpts = append (runOpts , llb .AddEnv ("GOPROXY" , gomodProxy ))
131+ }
132+
133+ in = gomodOpts .worker .Run (
134+ runOpts ... ,
106135 ).AddMount (gomodCacheDir , in )
107136 }
108137
@@ -238,7 +267,15 @@ func (s *Spec) GomodDeps(sOpt SourceOpts, worker llb.State, opts ...llb.Constrai
238267 deps = deps .With (func (in llb.State ) llb.State {
239268 for _ , gen := range src .Generate {
240269 if gen .Gomod != nil {
241- in = in .With (withGomod (gen , patched [key ], worker , key , credHelperRunOpt , opts ... ))
270+ in = in .With (withGomod (gomodGeneratorOpts {
271+ sourceName : key ,
272+ gen : gen ,
273+ sourceState : patched [key ],
274+ worker : worker ,
275+ credHelper : credHelperRunOpt ,
276+ extraEnvs : sOpt .ExtraEnvs ,
277+ constraints : opts ,
278+ }))
242279 }
243280 }
244281 return in
0 commit comments