Skip to content

Commit d084344

Browse files
gregestrencopybara-github
authored andcommitted
New incompatible flags disable ctx.fragments.py, ctx.fragments.bazel_py
`--incompatible_remove_ctx_py_fragment` removes `ctx.fragments.py`. `--incompatible_remove_ctx_bazel_py_fragment` removes `ctx.fragments.bazel_py`. `rules_python` can use this to determine if it should read Python flags from Starlark definitions or Bazel-native: ```py if not hasattr(ctx.fragments, "py"): print("I should read Python flags from Starlark.") ```` For bazel-contrib/rules_python#3252. **Caveats:** This approach has the downside that it's all-or-nothing: removing an entire fragment doesn't permit partial Starlarkification. Which is hypothetically an issue if we have to keep certain flags native because of strange inner Bazel dependencies. I think for Python this isn't a huge risk. Worst-case we can redefine the flags to restrict fragment access instead of completely removing it. Closes #27056. PiperOrigin-RevId: 811388173 Change-Id: I8e148e65fce8d007f0724ebac6752319e0c86c54
1 parent 054536e commit d084344

File tree

4 files changed

+43
-8
lines changed

4 files changed

+43
-8
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/python/BazelPythonConfiguration.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,18 @@ public static final class Options extends FragmentOptions {
6464
+ "`import myreponame.mytoplevelpackage.package.module` is valid. The latter form "
6565
+ "is less likely to experience import name collisions.")
6666
public boolean experimentalPythonImportAllRepositories;
67+
68+
@Option(
69+
name = "incompatible_remove_ctx_bazel_py_fragment",
70+
defaultValue = "false",
71+
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
72+
effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
73+
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
74+
help =
75+
"When true, Python build flags are defined with Python rules (in BUIILD files) and the"
76+
+ " ctx.fragments.bazel_py attribute is not present. This is a migration flag to"
77+
+ " move all Python flags from core Bazel to Python rules.")
78+
public boolean disablePyFragment;
6779
}
6880

6981
private final Options options;
@@ -77,6 +89,11 @@ public BazelPythonConfiguration(BuildOptions buildOptions) throws InvalidConfigu
7789
}
7890
}
7991

92+
@Override
93+
public boolean shouldInclude() {
94+
return !options.disablePyFragment;
95+
}
96+
8097
@StarlarkConfigurationField(
8198
name = "python_top",
8299
doc = "Deprecated. Always returns None. Use toolchain resolution instead.")

src/main/java/com/google/devtools/build/lib/rules/python/PythonConfiguration.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class PythonConfiguration extends Fragment implements StarlarkValue {
5555
private final boolean defaultToExplicitInitPy;
5656
@Nullable private final Label nativeRulesAllowlist;
5757
private final boolean disallowNativeRules;
58+
private final boolean disablePyFragment;
5859

5960
public PythonConfiguration(BuildOptions buildOptions) {
6061
PythonOptions pythonOptions = buildOptions.get(PythonOptions.class);
@@ -68,6 +69,12 @@ public PythonConfiguration(BuildOptions buildOptions) {
6869
this.nativeRulesAllowlist = pythonOptions.nativeRulesAllowlist;
6970
this.disallowNativeRules = pythonOptions.disallowNativeRules;
7071
this.includeLabelInLinkstamp = pythonOptions.includeLabelInPyBinariesLinkstamp;
72+
this.disablePyFragment = pythonOptions.disablePyFragment;
73+
}
74+
75+
@Override
76+
public boolean shouldInclude() {
77+
return !disablePyFragment;
7178
}
7279

7380
@Override
@@ -140,14 +147,11 @@ public void processForOutputPathMnemonic(Fragment.OutputDirectoriesContext ctx)
140147
structField = true,
141148
doc = "The effective value of --build_python_zip")
142149
public boolean buildPythonZip() {
143-
switch (buildPythonZip) {
144-
case YES:
145-
return true;
146-
case NO:
147-
return false;
148-
default:
149-
return OS.getCurrent() == OS.WINDOWS;
150-
}
150+
return switch (buildPythonZip) {
151+
case YES -> true;
152+
case NO -> false;
153+
default -> OS.getCurrent() == OS.WINDOWS;
154+
};
151155
}
152156

153157
/**

src/main/java/com/google/devtools/build/lib/rules/python/PythonOptions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,18 @@ public String getTypeDescription() {
174174
// Helper field to store hostForcePython in exec configuration
175175
private PythonVersion defaultPythonVersion = null;
176176

177+
@Option(
178+
name = "incompatible_remove_ctx_py_fragment",
179+
defaultValue = "false",
180+
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
181+
effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
182+
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
183+
help =
184+
"When true, Python build flags are defined with Python rules (in BUIILD files) and"
185+
+ " ctx.fragments.py is undefined. This is a migration flag to move all Python flags "
186+
+ " from core Bazel to Python rules.")
187+
public boolean disablePyFragment;
188+
177189
/**
178190
* Returns the Python major version ({@code PY2} or {@code PY3}) that targets that do not specify
179191
* a version should be built for.

src/main/starlark/builtins_bzl/common/builtin_exec_platforms.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ bazel_fragments["BazelPythonConfiguration$Options"] = fragment(
195195
propagate = [
196196
"//command_line_option:python_path",
197197
"//command_line_option:experimental_python_import_all_repositories",
198+
"//command_line_option:incompatible_remove_ctx_bazel_py_fragment",
198199
],
199200
)
200201

@@ -445,6 +446,7 @@ bazel_fragments["PythonOptions"] = fragment(
445446
"//command_line_option:incompatible_py3_is_default",
446447
"//command_line_option:incompatible_python_disallow_native_rules",
447448
"//command_line_option:python_native_rules_allowlist",
449+
"//command_line_option:incompatible_remove_ctx_py_fragment",
448450
],
449451
outputs = [
450452
"//command_line_option:python_version",

0 commit comments

Comments
 (0)