From dfb958389ac481a39edddb94bc51e9e50ff9ffb2 Mon Sep 17 00:00:00 2001 From: cemeceme <26171877+cemeceme@users.noreply.github.com> Date: Wed, 22 May 2024 15:39:46 +0200 Subject: [PATCH 1/5] Added dependency check after post_build step. --- src/build/build_step.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/build/build_step.go b/src/build/build_step.go index 1c274c2ab..b87b878df 100644 --- a/src/build/build_step.go +++ b/src/build/build_step.go @@ -78,6 +78,23 @@ func Build(state *core.BuildState, target *core.BuildTarget, remote bool) { } else { successfulLocalTargetBuildDuration.Observe(float64(time.Since(start).Milliseconds())) } + + // Check if the target had a post build step and make sure to handle any dependencies added within it. + if target.PostBuildFunction != nil { + // Use declared dependencies and get the targets from their labels, because addDep creates a depInfo struct with the deps field unpopulated. + for _, label := range target.DeclaredDependencies() { + depTarget := state.Graph.Target(label) + depTarget.WaitForBuild() + if depTarget.State() >= core.DependencyFailed { // Either the target failed or its dependencies failed + // Give up and set the original target as dependency failed + target.SetState(core.DependencyFailed) + state.LogBuildResult(target, core.TargetBuilt, "Dependency failed") + target.FinishBuild() + return + } + } + } + // Mark the target as having finished building. target.FinishBuild() if target.IsTest() && state.NeedTests && state.IsOriginalTarget(target) { From 89ff26dd510320f7c34d5fc93f3771f018b7daa8 Mon Sep 17 00:00:00 2001 From: cemeceme <26171877+cemeceme@users.noreply.github.com> Date: Fri, 21 Jun 2024 13:05:32 +0200 Subject: [PATCH 2/5] Refactored post build dependencies and fixed exported dependencies. --- src/build/build_step.go | 26 ++++++++++---------------- src/core/build_target.go | 18 ++++++++++++++++++ src/parse/asp/builtins.go | 1 + 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/build/build_step.go b/src/build/build_step.go index b87b878df..c74b1e0ac 100644 --- a/src/build/build_step.go +++ b/src/build/build_step.go @@ -79,22 +79,6 @@ func Build(state *core.BuildState, target *core.BuildTarget, remote bool) { successfulLocalTargetBuildDuration.Observe(float64(time.Since(start).Milliseconds())) } - // Check if the target had a post build step and make sure to handle any dependencies added within it. - if target.PostBuildFunction != nil { - // Use declared dependencies and get the targets from their labels, because addDep creates a depInfo struct with the deps field unpopulated. - for _, label := range target.DeclaredDependencies() { - depTarget := state.Graph.Target(label) - depTarget.WaitForBuild() - if depTarget.State() >= core.DependencyFailed { // Either the target failed or its dependencies failed - // Give up and set the original target as dependency failed - target.SetState(core.DependencyFailed) - state.LogBuildResult(target, core.TargetBuilt, "Dependency failed") - target.FinishBuild() - return - } - } - } - // Mark the target as having finished building. target.FinishBuild() if target.IsTest() && state.NeedTests && state.IsOriginalTarget(target) { @@ -360,6 +344,16 @@ func buildTarget(state *core.BuildState, target *core.BuildTarget, runRemotely b return err } + // Wait for any new dependencies added by post-build commands before continuing. + for _, dep := range target.Dependencies() { + dep.WaitForBuild() + if dep.State() >= core.DependencyFailed { // Either the target failed or its dependencies failed + // Give up and set the original target as dependency failed + target.SetState(core.DependencyFailed) + return fmt.Errorf("error in post-rule dependency for %s: %s", target.Label, dep.Label) + } + } + if runRemotely && len(outs) != len(target.Outputs()) { // postBuildFunction has changed the target - must rebuild it log.Info("Rebuilding %s after post-build function", target) diff --git a/src/core/build_target.go b/src/core/build_target.go index 166dbb515..b9f06f7da 100644 --- a/src/core/build_target.go +++ b/src/core/build_target.go @@ -677,6 +677,15 @@ func (target *BuildTarget) ExportedDependencies() []BuildLabel { defer target.mutex.RUnlock() ret := make(BuildLabels, 0, len(target.dependencies)) for _, info := range target.dependencies { + + logString := "" + for i, dep := range info.deps { + logString += dep.Label.Name + if i+1 < len(info.deps) { + logString += ", " + } + } + log.Info("Exported deps call for target %s: %s , exported: %t", target.Label, logString, info.exported) if info.exported { ret = append(ret, *info.declared) } @@ -1653,6 +1662,15 @@ func (target *BuildTarget) AddMaybeExportedDependency(dep BuildLabel, exported, } } +// RegisterDependencyTarget registers a build target to be used for a dependency label on the given target. +func (target *BuildTarget) RegisterDependencyTarget(dep BuildLabel, deptarget *BuildTarget) { + info := target.dependencyInfo(dep) + if info == nil { + log.Fatalf("Target %s doesn't contain dependency %s.\n", target.Label, dep) + } + info.deps = append(info.deps, deptarget) +} + // IsTool returns true if the given build label is a tool used by this target. func (target *BuildTarget) IsTool(tool BuildLabel) bool { for _, t := range target.Tools { diff --git a/src/parse/asp/builtins.go b/src/parse/asp/builtins.go index e3a04b53a..cc82f74b6 100644 --- a/src/parse/asp/builtins.go +++ b/src/parse/asp/builtins.go @@ -1125,6 +1125,7 @@ func addDep(s *scope, args []pyObject) pyObject { dep := s.parseLabelInPackage(string(args[1].(pyString)), s.pkg) exported := args[2].IsTruthy() target.AddMaybeExportedDependency(dep, exported, false, false) + target.RegisterDependencyTarget(dep, s.state.Graph.Target(dep)) // Queue this dependency if it'll be needed. if target.State() > core.Inactive { err := s.state.QueueTarget(dep, target.Label, false, core.ParseModeNormal) From cefd2c25a37ed61f06df5d40e9bfdcc7d37610ec Mon Sep 17 00:00:00 2001 From: cemeceme <26171877+cemeceme@users.noreply.github.com> Date: Tue, 9 Jul 2024 17:06:52 +0200 Subject: [PATCH 3/5] Removed leading newline. --- src/core/build_target.go | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/build_target.go b/src/core/build_target.go index b9f06f7da..98beb6f58 100644 --- a/src/core/build_target.go +++ b/src/core/build_target.go @@ -677,7 +677,6 @@ func (target *BuildTarget) ExportedDependencies() []BuildLabel { defer target.mutex.RUnlock() ret := make(BuildLabels, 0, len(target.dependencies)) for _, info := range target.dependencies { - logString := "" for i, dep := range info.deps { logString += dep.Label.Name From 9a94e313c9264f91de8da5d884467bcd5f74dbb3 Mon Sep 17 00:00:00 2001 From: cemeceme <26171877+cemeceme@users.noreply.github.com> Date: Sat, 10 Aug 2024 18:58:59 +0200 Subject: [PATCH 4/5] Linter fixes. --- src/core/build_target.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core/build_target.go b/src/core/build_target.go index 98beb6f58..9c6586940 100644 --- a/src/core/build_target.go +++ b/src/core/build_target.go @@ -1666,8 +1666,9 @@ func (target *BuildTarget) RegisterDependencyTarget(dep BuildLabel, deptarget *B info := target.dependencyInfo(dep) if info == nil { log.Fatalf("Target %s doesn't contain dependency %s.\n", target.Label, dep) + } else { + info.deps = append(info.deps, deptarget) } - info.deps = append(info.deps, deptarget) } // IsTool returns true if the given build label is a tool used by this target. From 3261eec309c180f8e0db9fb6dc318718145651b5 Mon Sep 17 00:00:00 2001 From: cemeceme <26171877+cemeceme@users.noreply.github.com> Date: Tue, 13 Aug 2024 15:24:33 +0300 Subject: [PATCH 5/5] Removed debug print. --- src/build/build_step.go | 1 - src/core/build_target.go | 8 -------- 2 files changed, 9 deletions(-) diff --git a/src/build/build_step.go b/src/build/build_step.go index c74b1e0ac..d028eb67d 100644 --- a/src/build/build_step.go +++ b/src/build/build_step.go @@ -78,7 +78,6 @@ func Build(state *core.BuildState, target *core.BuildTarget, remote bool) { } else { successfulLocalTargetBuildDuration.Observe(float64(time.Since(start).Milliseconds())) } - // Mark the target as having finished building. target.FinishBuild() if target.IsTest() && state.NeedTests && state.IsOriginalTarget(target) { diff --git a/src/core/build_target.go b/src/core/build_target.go index 9c6586940..fb91a935b 100644 --- a/src/core/build_target.go +++ b/src/core/build_target.go @@ -677,14 +677,6 @@ func (target *BuildTarget) ExportedDependencies() []BuildLabel { defer target.mutex.RUnlock() ret := make(BuildLabels, 0, len(target.dependencies)) for _, info := range target.dependencies { - logString := "" - for i, dep := range info.deps { - logString += dep.Label.Name - if i+1 < len(info.deps) { - logString += ", " - } - } - log.Info("Exported deps call for target %s: %s , exported: %t", target.Label, logString, info.exported) if info.exported { ret = append(ret, *info.declared) }