Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.3.0] Handle exception instead of crashing #18895

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,15 @@ public String getName() {
public Object call(StarlarkThread thread, Tuple args, Dict<String, Object> kwargs)
throws EvalException, InterruptedException {
if (!args.isEmpty()) {
throw new EvalException("unexpected positional arguments");
throw new EvalException("Unexpected positional arguments");
}
try {
BazelStarlarkContext.from(thread).checkLoadingPhase(getName());
} catch (IllegalStateException e) {
throw new EvalException(
"A rule can only be instantiated in a BUILD file, or a macro "
+ "invoked from a BUILD file");
}
BazelStarlarkContext.from(thread).checkLoadingPhase(getName());
if (ruleClass == null) {
throw new EvalException("Invalid rule class hasn't been exported by a bzl file");
}
Expand Down
31 changes: 31 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,37 @@ def testWorkspaceToolchainRegistrationWithPlatformsConstraint(self):
with open(self.Path('bazel-bin/my_consumer'), 'r') as f:
self.assertEqual(f.read().strip(), 'my_value = Hello, Bzlmod!')

def testModuleExtensionWithRuleError(self):
self.ScratchFile(
'MODULE.bazel',
[
'ext = use_extension("extensions.bzl", "ext")',
'use_repo(ext, "ext")',
],
)
self.ScratchFile('BUILD')
self.ScratchFile(
'extensions.bzl',
[
'def _rule_impl(ctx):',
' print("RULE CALLED")',
'init_rule = rule(_rule_impl)',
'def ext_impl(module_ctx):',
' init_rule()',
'ext = module_extension(implementation = ext_impl,)',
],
)
exit_code, _, stderr = self.RunBazel(
['build', '--nobuild', '@ext//:all'],
allow_failure=True,
)
self.AssertExitCode(exit_code, 48, stderr)
self.assertIn(
'Error in init_rule: A rule can only be instantiated in a BUILD file, '
'or a macro invoked from a BUILD file',
stderr,
)


if __name__ == '__main__':
unittest.main()