Push BazelStarlarkContext fields down to BzlInitThreadContext
The fields toolsRepository, networkAllowlistForTests, and fragmentNameToClass, are only relevant to rule definition. This CL moves them from BazelStarlarkContext down to the subclass BzlInitThreadContext, which is used for top-level evaluation of .bzl code. The fields also become non-nullable. BzlInitThreadContext now directly implements RuleDefinitionEnvironment instead of BazelStarlarkContext implementing it.
These fields are also needed at BUILD evaluation time for the niche case of analysis_test (called within a macro). This is now obtained from a separate RuleDefinitionEnvironment threadlocal, set in PackageFactory.
StarlarkRuleClassFunctions#createRule is refactored so that its two call sites (rule() and analysis_test()) can each supply the RuleDefinitionEnvironment and related contextual information in different ways. This results in createRule() having an excessive number of parameters, triggering a lint. I think that's preferable to passing a larger-scoped object than is needed and trying to fake such an object from the call site where it's not applicable.
Other changes:
- `configuration_field()` now fails Starlark evaluation if called outside .bzl initialization. (Previously I believe it would hard-crash.)
- `rule()` and `attr.*` can now only be called in BUILD/.bzl initialization. Previously they could also be called from repo rule implementations and WORKSPACE evaluation, which made no sense. (You could argue we should go the other way and allow users to call `rule()`/`attr.*` wherever they want, but I followed the existing idiom here.)
- BzlInitThreadContext.fromOrFailFunction() is renamed fromOrFail(), and its error message no longer assumes the offending feature is a function. The related, unused from() method is deleted.
- StarlarkCallbackHelper (used for computed defaults and implicit outputs) no longer embeds a BazelStarlarkContext.
- In StarlarkRuleClassFunctions, narrow param type from StarlarkThread to smaller objects for some helper functions.
- Add some refactoring TODOs to StarlarkAttrModule.
- Clarify comment in LabelConverter.
RELNOTES: `rule()` and `attr.*` can no longer be (pointlessly) called during WORKSPACE evaluation and repository rule evaluation.
PiperOrigin-RevId: 555981744
Change-Id: I996a0b97f8ada049a957a3ac5f8d16c9f5bb7da0
diff --git a/src/main/java/com/google/devtools/build/lib/packages/LabelConverter.java b/src/main/java/com/google/devtools/build/lib/packages/LabelConverter.java
index 935f137..64ac5ba 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/LabelConverter.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/LabelConverter.java
@@ -30,9 +30,10 @@
public class LabelConverter {
/**
- * Returns a label converter for the given thread, which MUST be evaluating a .bzl file. It uses
- * the package containing the .bzl file as the base package, and the repo mapping of the repo
- * containing the .bzl file.
+ * Returns a label converter for the given thread, which MUST be currently evaluating Starlark
+ * code in a .bzl file (top-level, macro, rule implementation function, etc.). It uses the package
+ * containing the .bzl file as the base package, and the repo mapping of the repo containing the
+ * .bzl file.
*/
public static LabelConverter forBzlEvaluatingThread(StarlarkThread thread) {
BazelModuleContext moduleContext = BazelModuleContext.ofInnermostBzlOrThrow(thread);