Skip to content

Commit fe644be

Browse files
gregestrencopybara-github
authored andcommitted
Fix cache leak when applying transitions when only a rule's attributes change.
Fixes bazelbuild#13997 PiperOrigin-RevId: 406886706
1 parent 216b2b6 commit fe644be

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleTransitionProvider.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.devtools.build.lib.analysis.config.StarlarkDefinedConfigTransition;
2626
import com.google.devtools.build.lib.analysis.config.transitions.PatchTransition;
2727
import com.google.devtools.build.lib.analysis.config.transitions.TransitionFactory;
28-
import com.google.devtools.build.lib.cmdline.Label;
2928
import com.google.devtools.build.lib.events.Event;
3029
import com.google.devtools.build.lib.events.EventHandler;
3130
import com.google.devtools.build.lib.packages.Attribute;
@@ -71,13 +70,13 @@ public StarlarkDefinedConfigTransition getStarlarkDefinedConfigTransitionForTest
7170
*/
7271
private static class CacheKey {
7372
private final StarlarkDefinedConfigTransition starlarkDefinedConfigTransition;
74-
private final Label ruleLabel;
73+
private final Rule rule;
7574
private final int hashCode;
7675

7776
CacheKey(StarlarkDefinedConfigTransition starlarkDefinedConfigTransition, Rule rule) {
7877
this.starlarkDefinedConfigTransition = starlarkDefinedConfigTransition;
79-
this.ruleLabel = rule.getLabel();
80-
this.hashCode = Objects.hash(starlarkDefinedConfigTransition, ruleLabel);
78+
this.rule = rule;
79+
this.hashCode = Objects.hash(starlarkDefinedConfigTransition, rule);
8180
}
8281

8382
@Override
@@ -90,7 +89,7 @@ public boolean equals(Object other) {
9089
}
9190
return (this.starlarkDefinedConfigTransition.equals(
9291
((CacheKey) other).starlarkDefinedConfigTransition)
93-
&& this.ruleLabel.equals(((CacheKey) other).ruleLabel));
92+
&& this.rule.equals(((CacheKey) other).rule));
9493
}
9594

9695
@Override

src/test/java/com/google/devtools/build/lib/analysis/StarlarkRuleTransitionProviderTest.java

+66
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import com.google.devtools.build.lib.skyframe.ConfiguredTargetAndData;
3131
import com.google.devtools.build.lib.testutil.TestConstants;
3232
import com.google.devtools.build.lib.testutil.TestRuleClassProvider;
33+
import com.google.devtools.build.lib.vfs.ModifiedFileSet;
34+
import com.google.devtools.build.lib.vfs.PathFragment;
35+
import com.google.devtools.build.lib.vfs.Root;
3336
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
3437
import com.google.testing.junit.testparameterinjector.TestParameters;
3538
import java.util.List;
@@ -1692,4 +1695,67 @@ public void testNoPlatformChange() throws Exception {
16921695
.platforms)
16931696
.containsExactly(Label.parseAbsoluteUnchecked("//platforms:my_platform"));
16941697
}
1698+
1699+
@Test
1700+
public void testTransitionsStillTriggerWhenOnlyRuleAttributesChange() throws Exception {
1701+
scratch.file(
1702+
"test/defs.bzl",
1703+
"def _transition_impl(settings, attr):",
1704+
" return {",
1705+
" '//command_line_option:foo': attr.my_attr,",
1706+
" }",
1707+
"_my_transition = transition(",
1708+
" implementation = _transition_impl,",
1709+
" inputs = [],",
1710+
" outputs = [",
1711+
" '//command_line_option:foo',",
1712+
" ]",
1713+
")",
1714+
"def _rule_impl(ctx):",
1715+
" return []",
1716+
"my_rule = rule(",
1717+
" implementation = _rule_impl,",
1718+
" cfg = _my_transition,",
1719+
" attrs = {",
1720+
" 'my_attr': attr.string(),",
1721+
" '_allowlist_function_transition': attr.label(",
1722+
" default = '//tools/allowlists/function_transition_allowlist',",
1723+
" ),",
1724+
" },",
1725+
")");
1726+
writeAllowlistFile();
1727+
1728+
scratch.file(
1729+
"test/BUILD",
1730+
"load('//test:defs.bzl', 'my_rule')",
1731+
"my_rule(",
1732+
" name = 'buildme',",
1733+
" my_attr = 'first build',",
1734+
")");
1735+
assertThat(
1736+
getConfiguration(getConfiguredTarget("//test:buildme"))
1737+
.getOptions()
1738+
.get(DummyTestOptions.class)
1739+
.foo)
1740+
.isEqualTo("first build");
1741+
1742+
scratch.overwriteFile(
1743+
"test/BUILD",
1744+
"load('//test:defs.bzl', 'my_rule')",
1745+
"my_rule(",
1746+
" name = 'buildme',",
1747+
" my_attr = 'second build',",
1748+
")");
1749+
skyframeExecutor.invalidateFilesUnderPathForTesting(
1750+
reporter,
1751+
ModifiedFileSet.builder().modify(PathFragment.create("test/BUILD")).build(),
1752+
Root.fromPath(rootDirectory));
1753+
1754+
assertThat(
1755+
getConfiguration(getConfiguredTarget("//test:buildme"))
1756+
.getOptions()
1757+
.get(DummyTestOptions.class)
1758+
.foo)
1759+
.isEqualTo("second build");
1760+
}
16951761
}

0 commit comments

Comments
 (0)