-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade original targets #2942
Upgrade original targets #2942
Changes from 5 commits
878a312
4a96e39
8bcf6f2
0ee7324
56ed27a
809836b
27fcfe9
5a544e4
ce90345
2f19586
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,8 +286,7 @@ type stateProgress struct { | |
// The set of known states | ||
allStates []*BuildState | ||
// Targets that we were originally requested to build | ||
originalTargets []BuildLabel | ||
originalTargetMutex sync.Mutex | ||
originalTargets *TargetSet | ||
// True if something about the build has failed. | ||
failed atomicBool | ||
// True if >= 1 target has failed to build | ||
|
@@ -423,14 +422,10 @@ func (state *BuildState) IsOriginalTarget(target *BuildTarget) bool { | |
} | ||
|
||
func (state *BuildState) isOriginalTarget(target *BuildTarget, exact bool) bool { | ||
state.progress.originalTargetMutex.Lock() | ||
defer state.progress.originalTargetMutex.Unlock() | ||
for _, original := range state.progress.originalTargets { | ||
if original == target.Label || (!exact && original.IsAllTargets() && original.PackageName == target.Label.PackageName && state.ShouldInclude(target)) { | ||
return true | ||
} | ||
if exact { | ||
return state.progress.originalTargets.MatchExact(target.Label) | ||
} | ||
return false | ||
return state.progress.originalTargets.Match(target.Label) && state.ShouldInclude(target) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic isn't quite right. I think what we want is:
Currently if I have a test with |
||
} | ||
|
||
// IsOriginalTargetOrParent is like IsOriginalTarget but checks the target's parent too (if it has one) | ||
|
@@ -480,9 +475,7 @@ func (state *BuildState) AddOriginalTarget(label BuildLabel, addToList bool) { | |
} | ||
} | ||
if addToList { | ||
state.progress.originalTargetMutex.Lock() | ||
state.progress.originalTargets = append(state.progress.originalTargets, label) | ||
state.progress.originalTargetMutex.Unlock() | ||
state.progress.originalTargets.Add(label) | ||
} | ||
state.addPendingParse(label, OriginalTarget, ParseModeNormal) | ||
} | ||
|
@@ -702,18 +695,12 @@ func (state *BuildState) NumDone() int { | |
// ExpandOriginalLabels expands any pseudo-labels (ie. :all, ... has already been resolved to a bunch :all targets) | ||
// from the set of original labels. This will exclude non-test targets when we're building for test. | ||
func (state *BuildState) ExpandOriginalLabels() BuildLabels { | ||
state.progress.originalTargetMutex.Lock() | ||
targets := state.progress.originalTargets[:] | ||
state.progress.originalTargetMutex.Unlock() | ||
return state.ExpandLabels(targets) | ||
return state.ExpandLabels(state.progress.originalTargets.AllTargets()) | ||
} | ||
|
||
// ExpandAllOriginalLabels is the same as ExpandOriginalLabels except it always includes non-test targets | ||
func (state *BuildState) ExpandAllOriginalLabels() BuildLabels { | ||
state.progress.originalTargetMutex.Lock() | ||
targets := state.progress.originalTargets[:] | ||
state.progress.originalTargetMutex.Unlock() | ||
return state.expandLabels(targets, false) | ||
return state.expandLabels(state.progress.originalTargets.AllTargets(), false) | ||
} | ||
|
||
func AnnotateLabels(labels []BuildLabel) []AnnotatedOutputLabel { | ||
|
@@ -1394,6 +1381,7 @@ func NewBuildState(config *Configuration) *BuildState { | |
packageWaits: cmap.New[packageKey, chan struct{}](cmap.DefaultShardCount, hashPackageKey), | ||
internalResults: make(chan *BuildResult, 1000), | ||
cycleDetector: cycleDetector{graph: graph}, | ||
originalTargets: NewTargetSet(), | ||
}, | ||
initOnce: new(sync.Once), | ||
preloadDownloadOnce: new(sync.Once), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,7 +59,8 @@ func TestExpandVisibleOriginalTargets(t *testing.T) { | |
|
||
func TestExpandOriginalSubLabels(t *testing.T) { | ||
state := NewDefaultBuildState() | ||
state.AddOriginalTarget(BuildLabel{PackageName: "src/core", Name: "..."}, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we ever actually needed to support this. The new implementation just panics but things like |
||
state.AddOriginalTarget(BuildLabel{PackageName: "src/core", Name: "all"}, true) | ||
state.AddOriginalTarget(BuildLabel{PackageName: "src/core/tests", Name: "all"}, true) | ||
state.Include = []string{"go"} | ||
state.Exclude = []string{"py"} | ||
addTarget(state, "//src/core:target1", "go") | ||
|
@@ -76,17 +77,18 @@ func TestExpandOriginalSubLabels(t *testing.T) { | |
func TestExpandOriginalLabelsOrdering(t *testing.T) { | ||
state := NewDefaultBuildState() | ||
state.AddOriginalTarget(BuildLabel{PackageName: "src/parse", Name: "parse"}, true) | ||
state.AddOriginalTarget(BuildLabel{PackageName: "src/core", Name: "..."}, true) | ||
state.AddOriginalTarget(BuildLabel{PackageName: "src/core", Name: "all"}, true) | ||
state.AddOriginalTarget(BuildLabel{PackageName: "src/core/tests", Name: "all"}, true) | ||
state.AddOriginalTarget(BuildLabel{PackageName: "src/build", Name: "build"}, true) | ||
addTarget(state, "//src/core:target1", "go") | ||
addTarget(state, "//src/core:target2", "py") | ||
addTarget(state, "//src/core/tests:target3", "go") | ||
expected := BuildLabels{ | ||
{PackageName: "src/parse", Name: "parse"}, | ||
{PackageName: "src/build", Name: "build"}, | ||
{PackageName: "src/core", Name: "target1"}, | ||
{PackageName: "src/core", Name: "target2"}, | ||
{PackageName: "src/core/tests", Name: "target3"}, | ||
{PackageName: "src/build", Name: "build"}, | ||
{PackageName: "src/parse", Name: "parse"}, | ||
} | ||
assert.Equal(t, expected, state.ExpandOriginalLabels()) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package core | ||
|
||
import ( | ||
"slices" | ||
"sync" | ||
) | ||
|
||
// A TargetSet contains a series of targets and supports efficiently checking for membership | ||
// The zero value is not safe for use. | ||
type TargetSet struct { | ||
targets map[BuildLabel]struct{} | ||
packages map[packageKey]struct{} | ||
mutex sync.RWMutex | ||
} | ||
|
||
// NewTargetSet returns a new TargetSet. | ||
func NewTargetSet() *TargetSet { | ||
return &TargetSet{ | ||
targets: map[BuildLabel]struct{}{}, | ||
packages: map[packageKey]struct{}{}, | ||
} | ||
} | ||
|
||
// Add adds a new target to this set. | ||
func (ts *TargetSet) Add(label BuildLabel) { | ||
ts.mutex.Lock() | ||
defer ts.mutex.Unlock() | ||
if label.IsAllSubpackages() { | ||
panic("TargetSet doesn't support ... labels") | ||
} else if label.IsAllTargets() { | ||
ts.packages[label.packageKey()] = struct{}{} | ||
} else { | ||
ts.targets[label] = struct{}{} | ||
} | ||
} | ||
|
||
// Match checks if this label is covered by the set (either explicitly or via :all) | ||
func (ts *TargetSet) Match(label BuildLabel) bool { | ||
ts.mutex.RLock() | ||
defer ts.mutex.RUnlock() | ||
if _, present := ts.targets[label]; present { | ||
return true | ||
} | ||
_, present := ts.packages[label.packageKey()] | ||
return present | ||
} | ||
|
||
// MatchExact checks if this label was explicitly added to the set (i.e. :all doesn't count) | ||
func (ts *TargetSet) MatchExact(label BuildLabel) bool { | ||
ts.mutex.RLock() | ||
defer ts.mutex.RUnlock() | ||
_, present := ts.targets[label] | ||
return present | ||
} | ||
|
||
// AllTargets returns a copy of the set of targets | ||
func (ts *TargetSet) AllTargets() []BuildLabel { | ||
ts.mutex.RLock() | ||
defer ts.mutex.RUnlock() | ||
ret := make([]BuildLabel, 0, len(ts.targets)+len(ts.packages)) | ||
for target := range ts.targets { | ||
ret = append(ret, target) | ||
} | ||
for pkg := range ts.packages { | ||
ret = append(ret, pkg.BuildLabel()) | ||
} | ||
slices.SortFunc(ret, BuildLabel.Compare) | ||
return ret | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package core | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTargetSetMatch(t *testing.T) { | ||
ts := NewTargetSet() | ||
ts.Add(ParseBuildLabel("//src/core:core", "")) | ||
ts.Add(ParseBuildLabel("//src/parse:all", "")) | ||
assert.True(t, ts.Match(ParseBuildLabel("//src/core:core", ""))) | ||
assert.False(t, ts.Match(ParseBuildLabel("//src/core:core_test", ""))) | ||
assert.True(t, ts.Match(ParseBuildLabel("//src/parse:parse", ""))) | ||
assert.True(t, ts.Match(ParseBuildLabel("//src/parse:parse_test", ""))) | ||
assert.False(t, ts.Match(ParseBuildLabel("//src/build", ""))) | ||
} | ||
|
||
func TestTargetSetMatchExact(t *testing.T) { | ||
ts := NewTargetSet() | ||
ts.Add(ParseBuildLabel("//src/core:core", "")) | ||
ts.Add(ParseBuildLabel("//src/parse:all", "")) | ||
assert.True(t, ts.MatchExact(ParseBuildLabel("//src/core:core", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/core:core_test", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/parse:parse", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/parse:parse_test", ""))) | ||
assert.False(t, ts.MatchExact(ParseBuildLabel("//src/build", ""))) | ||
} | ||
|
||
func TestAllTargets(t *testing.T) { | ||
ts := NewTargetSet() | ||
labels := []BuildLabel{ | ||
ParseBuildLabel("//src/core:core", ""), | ||
ParseBuildLabel("//src/core:core_test", ""), | ||
ParseBuildLabel("//src/parse:all", ""), | ||
ParseBuildLabel("//src/parse:parse_test", ""), | ||
} | ||
for _, label := range labels { | ||
ts.Add(label) | ||
} | ||
assert.Equal(t, labels, ts.AllTargets()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm restructuring this a bit but also it was wrong for ages because it didn't look at subrepos.