Skip to content

Commit

Permalink
Update error message when toolchain in ctx.actions.{run,run_shell} is…
Browse files Browse the repository at this point in the history
… not defined

Some rules are using `ctx.attr.tool[DefaultInfo].files_to_run.executable` when setting executable inside ctx.actions.{run,run_shell}. This attribute could be non-executable inside the rule's definition. This creates ambiguity when checking if toolchain parameter should be added or not since "context.getExecutableRunfiles" returns null. In this cases we should add "toolchain = None" to ctx.actions.{run,run_shell}.

I've added tests to cover these cases.

PiperOrigin-RevId: 518252829
Change-Id: I8d7ffd9459c295a3e08344aebfdfc70895283361
  • Loading branch information
kotlaja authored and copybara-github committed Mar 21, 2023
1 parent f48412f commit f8a59d9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private void checkToolchainParameterIsSet(Object toolchainUnchecked) throws Eval
if (toolchainUnchecked == Starlark.UNBOUND) {
throw Starlark.errorf(
"Couldn't identify if tools are from implicit dependencies or a toolchain. Please"
+ " set the toolchain parameter.");
+ " set the toolchain parameter. If you're not using a toolchain, set it to 'None'.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ private void createCustomRule(
" implementation = _impl,",
" attrs = {",
" '_tool': attr.label(default = '//toolchain:a_tool', cfg = 'exec', executable = True),",
" '_nonexecutable_tool': attr.label(default = '//toolchain:b_tool', cfg = 'exec'),",
extraAttributes,
" },",
" exec_groups = {",
Expand Down Expand Up @@ -404,6 +405,43 @@ public void toolInExecutableIdentified_noToolchainParameter_noError() throws Exc
assertNoEvents();
}

@Test
public void toolWithFilesToRunExecutable_noToolchainParameter_reportsError() throws Exception {
createCustomRule(
/* action= */ "ctx.actions.run",
/* actionParameters= */ "executable ="
+ " ctx.attr._nonexecutable_tool[DefaultInfo].files_to_run.executable,",
/* extraAttributes= */ "",
/* toolchains= */ "['//rule:toolchain_type_1', '//rule:toolchain_type_2']",
/* execGroups= */ "",
/* execCompatibleWith= */ "");
useConfiguration("--incompatible_auto_exec_groups");

reporter.removeHandler(failFastHandler);
getConfiguredTarget("//test:custom_rule_name");

assertContainsEvent(
"Couldn't identify if tools are from implicit dependencies or a toolchain. Please set"
+ " the toolchain parameter. If you're not using a toolchain, set it to 'None'.");
}

@Test
public void toolWithFilesToRunExecutable_toolchainParameterSetToNone_noError() throws Exception {
createCustomRule(
/* action= */ "ctx.actions.run",
/* actionParameters= */ "toolchain = None,"
+ " executable = ctx.attr._nonexecutable_tool[DefaultInfo].files_to_run.executable,",
/* extraAttributes= */ "",
/* toolchains= */ "['//rule:toolchain_type_1', '//rule:toolchain_type_2']",
/* execGroups= */ "",
/* execCompatibleWith= */ "");
useConfiguration("--incompatible_auto_exec_groups");

getConfiguredTarget("//test:custom_rule_name");

assertNoEvents();
}

@Test
public void toolInExecutableUnidentified_noToolchainParameter_reportsError() throws Exception {
createCustomRule(
Expand All @@ -420,7 +458,7 @@ public void toolInExecutableUnidentified_noToolchainParameter_reportsError() thr

assertContainsEvent(
"Couldn't identify if tools are from implicit dependencies or a toolchain. Please set"
+ " the toolchain parameter.");
+ " the toolchain parameter. If you're not using a toolchain, set it to 'None'.");
}

@Test
Expand All @@ -441,7 +479,7 @@ public void toolWithFilesToRunInExecutableUnidentified_noToolchainParameter_repo

assertContainsEvent(
"Couldn't identify if tools are from implicit dependencies or a toolchain. Please set"
+ " the toolchain parameter.");
+ " the toolchain parameter. If you're not using a toolchain, set it to 'None'.");
}

@Test
Expand All @@ -465,7 +503,7 @@ public void toolInToolsUnidentified_noToolchainParameter_reportsError(String act

assertContainsEvent(
"Couldn't identify if tools are from implicit dependencies or a toolchain. Please set"
+ " the toolchain parameter.");
+ " the toolchain parameter. If you're not using a toolchain, set it to 'None'.");
}

@Test
Expand All @@ -489,7 +527,7 @@ public void toolWithFilesToRunInToolsUnidentified_noToolchainParameter_reportsEr

assertContainsEvent(
"Couldn't identify if tools are from implicit dependencies or a toolchain. Please set"
+ " the toolchain parameter.");
+ " the toolchain parameter. If you're not using a toolchain, set it to 'None'.");
}

@Test
Expand All @@ -512,7 +550,7 @@ public void depsetInTools_noToolchainParameter_reportsError(String action) throw

assertContainsEvent(
"Couldn't identify if tools are from implicit dependencies or a toolchain. Please set"
+ " the toolchain parameter.");
+ " the toolchain parameter. If you're not using a toolchain, set it to 'None'.");
}

@Test
Expand Down

0 comments on commit f8a59d9

Please sign in to comment.