Skip to content

Commit

Permalink
Only allow files created by the same rule as executable outputs of a …
Browse files Browse the repository at this point in the history
…rule.

Fixes bazelbuild#4170.

Change-Id: I308ee17eb769dcc6a94b90b1dd6cc2ccbe14e968
PiperOrigin-RevId: 182807196
  • Loading branch information
dslomov authored and Copybara-Service committed Jan 22, 2018
1 parent 32d359f commit 2f80e58
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,14 @@ private static void parseDefaultProviderFields(
defaultRunfiles = cast("default_runfiles", provider, Runfiles.class, loc);
} else if (field.equals("executable")) {
executable = cast("executable", provider, Artifact.class, loc);
if (!executable.getArtifactOwner().equals(context.getRuleContext().getOwner())) {
throw new EvalException(
loc,
String.format(
"'executable' provided by an executable rule '%s' should be created "
+ "by the same rule.",
context.getRuleContext().getRule().getRuleClass()));
}
} else if (provider
.getProvider()
.getKey()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,36 @@ public void testExecutableRuleWithNoExecutableReportsError() throws Exception {
+ "parameter to the DefaultInfo it returns.");
}

@Test
public void testExecutableFromDifferentRuleIsForbidden() throws Exception {
scratch.file(
"pkg/BUILD",
"sh_binary(name = 'tryme',",
" srcs = [':tryme.sh'],",
" visibility = ['//visibility:public'],",
")");

scratch.file(
"src/rulez.bzl",
"def _impl(ctx):",
" return [DefaultInfo(executable = ctx.executable.runme,",
" files = depset([ctx.executable.runme]),",
" )]",
"r = rule(_impl,",
" executable = True,",
" attrs = {",
" 'runme' : attr.label(executable = True, mandatory = True, cfg = 'host'),",
" }",
")");

scratch.file(
"src/BUILD", "load(':rulez.bzl', 'r')", "r(name = 'r_tools', runme = '//pkg:tryme')");
reporter.removeHandler(failFastHandler);
getConfiguredTarget("//src:r_tools");
assertContainsEvent(
"/workspace/src/rulez.bzl:2:12: 'executable' provided by an executable"
+ " rule 'r' should be created by the same rule.");
}

/**
* Skylark integration test that forces inlining.
Expand Down

0 comments on commit 2f80e58

Please sign in to comment.