Skip to content

Commit 7858d75

Browse files
committed
[go] Support "nested" Go main builds
1 parent 346bec2 commit 7858d75

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ config:
132132
buildFlags: []
133133
# Command that's executed to lint the code
134134
lintCommand: ["golangci-lint", "run"]
135+
# GoMod can point to a go.mod file outside the component root. Leeway expects a go.sum alongside the go.mod.
136+
goMod: "../go.mod"
135137
```
136138
137139
### Yarn packages
@@ -173,6 +175,14 @@ config:
173175
- gitpod/leeway:${__pkg_version}
174176
```
175177
178+
The first image name of each Docker dependency which pushed an image will result in a build argument. This mechanism enables a package to build the base image for another one, by using the build argument as `FROM` value.
179+
The name of this build argument is the package name of the dependency, transformed as follows:
180+
- `/` is replaced with `_`
181+
- `:` is replaced with `__`
182+
- all uppercase.
183+
184+
E.g. `component/nested:docker` becomes `COMPONENT_NESTED__DOCKER`.
185+
176186
### Generic packages
177187
```YAML
178188
config:

pkg/leeway/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (res *packa
10781078
return nil, xerrors.Errorf("package should have Go config")
10791079
}
10801080

1081-
if _, err := os.Stat(filepath.Join(p.C.Origin, "go.mod")); os.IsNotExist(err) {
1081+
if _, err := os.Stat(filepath.Join(wd, "go.mod")); os.IsNotExist(err) {
10821082
return nil, xerrors.Errorf("can only build Go modules (missing go.mod file)")
10831083
}
10841084

pkg/leeway/package.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ type GoPkgConfig struct {
475475
BuildCommand []string `yaml:"buildCommand,omitempty"`
476476
LintCommand []string `yaml:"lintCommand,omitempty"`
477477
GoVersion string `yaml:"goVersion,omitempty"`
478+
GoMod string `yaml:"goMod,omitempty"`
478479
}
479480

480481
// Validate ensures this config can be acted upon/is valid
@@ -495,6 +496,15 @@ func (cfg GoPkgConfig) Validate() error {
495496
}
496497
}
497498

499+
if cfg.GoMod != "" {
500+
if filepath.IsAbs(cfg.GoMod) {
501+
return xerrors.Errorf("goMod must be relative to the component root")
502+
}
503+
if !strings.HasSuffix(cfg.GoMod, ".mod") {
504+
return xerrors.Errorf("goMod must end with .mod")
505+
}
506+
}
507+
498508
return nil
499509
}
500510

@@ -510,12 +520,21 @@ const (
510520

511521
// AdditionalSources returns a list of unresolved sources coming in through this configuration
512522
func (cfg GoPkgConfig) AdditionalSources(workspaceOrigin string) []string {
523+
var res []string
524+
513525
fn := filepath.Join(workspaceOrigin, "go.work")
514526
if _, err := os.Stat(fn); err == nil {
515-
return []string{fn}
527+
res = append(res, fn)
516528
}
517529

518-
return []string{}
530+
if cfg.GoMod != "" {
531+
res = append(res,
532+
cfg.GoMod,
533+
strings.TrimSuffix(cfg.GoMod, ".mod")+".sum",
534+
)
535+
}
536+
537+
return res
519538
}
520539

521540
// DockerPkgConfig configures a Docker package

pkg/leeway/workspace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ func loadComponent(ctx context.Context, workspace *Workspace, path string, args
613613
}
614614

615615
if _, err := os.Stat(fn); os.IsNotExist(err) {
616-
return comp, xerrors.Errorf("%s: %w", comp.Name, err)
616+
return comp, xerrors.Errorf("cannot find additional source for %s: %w", comp.Name, err)
617617
}
618618
if _, found := completeSources[fn]; found {
619619
continue

0 commit comments

Comments
 (0)