Skip to content

Commit

Permalink
Add incompatible_stop_exporting_build_file_path
Browse files Browse the repository at this point in the history
to guard removing `ctx.build_file_path`

PiperOrigin-RevId: 608766817
Change-Id: Ib3a636a30aa5fbb0c369f9eabef56f8dfb869049
  • Loading branch information
mai93 authored and copybara-github committed Feb 20, 2024
1 parent 2a1521d commit 5867dbf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -994,10 +994,23 @@ public Artifact getVolatileWorkspaceStatus() throws InterruptedException, EvalEx
@Override
public String getBuildFileRelativePath() throws EvalException {
checkMutable("build_file_path");
checkDeprecated("ctx.label.package + '/BUILD'", "ctx.build_file_path", getStarlarkSemantics());

Package pkg = ruleContext.getRule().getPackage();
return pkg.getSourceRoot().get().relativize(pkg.getBuildFile().getPath()).getPathString();
}

private static void checkDeprecated(String newApi, String oldApi, StarlarkSemantics semantics)
throws EvalException {
if (semantics.getBool(BuildLanguageOptions.INCOMPATIBLE_STOP_EXPORTING_BUILD_FILE_PATH)) {
throw Starlark.errorf(
"Use %s instead of %s.\n"
+ "Use --incompatible_stop_exporting_build_file_path=false to temporarily disable"
+ " this check.",
newApi, oldApi);
}
}

@Override
public String expandLocation(
String input, Sequence<?> targets, boolean shortPaths, StarlarkThread thread)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ public final class BuildLanguageOptions extends OptionsBase {
+ " immutable view objects instead of mutable dicts.")
public boolean incompatibleExistingRulesImmutableView;

@Option(
name = "incompatible_stop_exporting_build_file_path",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
help =
"If set to true, deprecated ctx.build_file_path will not be available. ctx.label.package"
+ " + '/BUILD' can be used instead.")
public boolean incompatibleStopExportingBuildFilePath;

@Option(
name = "experimental_google_legacy_api",
defaultValue = "false",
Expand Down Expand Up @@ -829,6 +840,8 @@ public StarlarkSemantics toStarlarkSemantics() {
.setBool(EXPERIMENTAL_RULE_EXTENSION_API, experimentalRuleExtensionApi)
.setBool(SEPARATE_ASPECT_DEPS, separateAspectDeps)
.setBool(INCOMPATIBLE_ENABLE_DEPRECATED_LABEL_APIS, enableDeprecatedLabelApis)
.setBool(
INCOMPATIBLE_STOP_EXPORTING_BUILD_FILE_PATH, incompatibleStopExportingBuildFilePath)
.build();
return INTERNER.intern(semantics);
}
Expand Down Expand Up @@ -925,7 +938,8 @@ public StarlarkSemantics toStarlarkSemantics() {
public static final String SEPARATE_ASPECT_DEPS = "+separate_aspect_deps";
public static final String INCOMPATIBLE_ENABLE_DEPRECATED_LABEL_APIS =
"+incompatible_enable_deprecated_label_apis";

public static final String INCOMPATIBLE_STOP_EXPORTING_BUILD_FILE_PATH =
"-incompatible_stop_exporting_build_file_path";
// non-booleans
public static final StarlarkSemantics.Key<String> EXPERIMENTAL_BUILTINS_BZL_PATH =
new StarlarkSemantics.Key<>("experimental_builtins_bzl_path", "%bundled%");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3528,6 +3528,33 @@ public void testBuildFilePath() throws Exception {
assertThat(result).isEqualTo("bar/BUILD");
}

@Test
public void testStopExportingBuildFilePath() throws Exception {
scratch.file("/foo/WORKSPACE");
scratch.file("/foo/bar/BUILD", "genrule(name = 'baz', cmd = 'dummy_cmd', outs = ['a.txt'])");

scratch.overwriteFile(
"WORKSPACE",
new ImmutableList.Builder<String>()
.addAll(analysisMock.getWorkspaceContents(mockToolsConfig))
.add("local_repository(name='foo', path='/foo')")
.build());

invalidatePackages(false);
setBuildLanguageOptions("--incompatible_stop_exporting_build_file_path");

setRuleContext(createRuleContext("@foo//bar:baz"));

EvalException evalException =
assertThrows(EvalException.class, () -> ev.eval("ruleContext.build_file_path"));
assertThat(evalException)
.hasMessageThat()
.isEqualTo(
"Use ctx.label.package + '/BUILD' instead of ctx.build_file_path.\nUse"
+ " --incompatible_stop_exporting_build_file_path=false to temporarily disable this"
+ " check.");
}

@Test
public void testNoToolchainContext() throws Exception {
// Build setting rules do not have a toolchain context, as they are part of the configuration.
Expand Down

0 comments on commit 5867dbf

Please sign in to comment.