Allow multiple calls to initStarlarkRuleContext.
This fixes when you have more than one call (from a native rule) into builtins that require StarlarkRuleContext.
Before it was impossible to obtain StarlarkRuleContext without catching an exception (thrown either from initStarlarkRuleContext or from getStarlarkRuleContext).
The calls into builtins do know they are invoked from a native rule or a native aspect. They don't know about each other.
PiperOrigin-RevId: 413076657
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
index b71c73a..6f8d8ad 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/RuleContext.java
@@ -1141,15 +1141,16 @@
}
/**
- * Initializes the StarlarkRuleContext for use and returns it.
+ * Initializes the StarlarkRuleContext for use and returns it. No-op if already initialized.
*
* <p>Throws RuleErrorException on failure.
*/
public StarlarkRuleContext initStarlarkRuleContext() throws RuleErrorException {
- Preconditions.checkState(starlarkRuleContext == null);
- AspectDescriptor descriptor =
- aspects.isEmpty() ? null : Iterables.getLast(aspects).getDescriptor();
- this.starlarkRuleContext = new StarlarkRuleContext(this, descriptor);
+ if (starlarkRuleContext == null) {
+ AspectDescriptor descriptor =
+ aspects.isEmpty() ? null : Iterables.getLast(aspects).getDescriptor();
+ this.starlarkRuleContext = new StarlarkRuleContext(this, descriptor);
+ }
return starlarkRuleContext;
}