Skip to content

Commit 44e5c89

Browse files
iQQBotona-agent
andcommitted
feat: gate weak deps feature behind --go-library-weak-deps flag
The weak dependency feature is now opt-in via CLI flag. When not enabled, Go library dependencies behave as regular hard dependencies. Co-authored-by: Ona <no-reply@ona.com>
1 parent 5d855ea commit 44e5c89

2 files changed

Lines changed: 43 additions & 12 deletions

File tree

cmd/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ func addBuildFlags(cmd *cobra.Command) {
241241
cmd.Flags().Bool("report-github", os.Getenv("GITHUB_OUTPUT") != "", "Report package build success/failure to GitHub Actions using the GITHUB_OUTPUT environment variable")
242242
cmd.Flags().Bool("fixed-build-dir", true, "Use a fixed build directory for each package, instead of based on the package version, to better utilize caches based on absolute paths (defaults to true)")
243243
cmd.Flags().Bool("docker-export-to-cache", false, "Export Docker images to cache instead of pushing directly (enables SLSA L3 compliance)")
244+
cmd.Flags().Bool("go-library-weak-deps", false, "Treat Go library dependencies as weak dependencies (copy source instead of built artifacts)")
244245
}
245246

246247
func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
@@ -412,6 +413,11 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
412413
dockerExportSet = true
413414
}
414415

416+
goLibraryWeakDeps, err := cmd.Flags().GetBool("go-library-weak-deps")
417+
if err != nil {
418+
log.Fatal(err)
419+
}
420+
415421
return []leeway.BuildOption{
416422
leeway.WithLocalCache(localCache),
417423
leeway.WithRemoteCache(remoteCache),
@@ -430,6 +436,7 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
430436
leeway.WithInFlightChecksums(inFlightChecksums),
431437
leeway.WithDockerExportToCache(dockerExportToCache, dockerExportSet),
432438
leeway.WithDockerExportEnv(dockerExportEnvValue, dockerExportEnvSet),
439+
leeway.WithGoLibraryWeakDeps(goLibraryWeakDeps),
433440
}, localCache
434441
}
435442

pkg/leeway/build.go

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,11 @@ type buildOptions struct {
586586
DockerExportEnvValue bool // Value from explicit user env var
587587
DockerExportEnvSet bool // Whether user explicitly set env var (before workspace)
588588

589+
// GoLibraryWeakDeps enables treating Go library dependencies as weak dependencies.
590+
// When enabled, Go library deps copy source code instead of built artifacts,
591+
// allowing parallel builds while still running tests.
592+
GoLibraryWeakDeps bool
593+
589594
context *buildContext
590595
}
591596

@@ -734,6 +739,16 @@ func WithDockerExportEnv(value, isSet bool) BuildOption {
734739
}
735740
}
736741

742+
// WithGoLibraryWeakDeps enables treating Go library dependencies as weak dependencies.
743+
// When enabled, Go library deps copy source code instead of built artifacts,
744+
// allowing parallel builds while still running tests.
745+
func WithGoLibraryWeakDeps(enabled bool) BuildOption {
746+
return func(opts *buildOptions) error {
747+
opts.GoLibraryWeakDeps = enabled
748+
return nil
749+
}
750+
}
751+
737752
func withBuildContext(ctx *buildContext) BuildOption {
738753
return func(opts *buildOptions) error {
739754
opts.context = ctx
@@ -781,18 +796,22 @@ func Build(pkg *Package, opts ...BuildOption) (err error) {
781796
requirements := pkg.GetTransitiveDependencies()
782797
allpkg := append(requirements, pkg)
783798

784-
weakDeps := collectWeakDependencies(pkg)
785-
for _, wd := range weakDeps {
786-
// Only add if not already in allpkg (avoid duplicates)
787-
found := false
788-
for _, p := range allpkg {
789-
if p.FullName() == wd.FullName() {
790-
found = true
791-
break
799+
// Only collect weak deps if the feature is enabled
800+
var weakDeps []*Package
801+
if ctx.GoLibraryWeakDeps {
802+
weakDeps = collectWeakDependencies(pkg)
803+
for _, wd := range weakDeps {
804+
// Only add if not already in allpkg (avoid duplicates)
805+
found := false
806+
for _, p := range allpkg {
807+
if p.FullName() == wd.FullName() {
808+
found = true
809+
break
810+
}
811+
}
812+
if !found {
813+
allpkg = append(allpkg, wd)
792814
}
793-
}
794-
if !found {
795-
allpkg = append(allpkg, wd)
796815
}
797816
}
798817

@@ -2293,7 +2312,12 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (res *packa
22932312
}
22942313

22952314
transdep := p.GetTransitiveDependencies()
2296-
weakdeps := p.GetTransitiveWeakDependencies()
2315+
2316+
// Only use weak deps if the feature is enabled via CLI flag
2317+
var weakdeps []*Package
2318+
if buildctx.GoLibraryWeakDeps {
2319+
weakdeps = p.GetTransitiveWeakDependencies()
2320+
}
22972321
needsDepsDir := len(transdep) > 0 || len(weakdeps) > 0
22982322

22992323
if needsDepsDir {

0 commit comments

Comments
 (0)