RELNOTES: throw an error if attributes contain an absolute path instead of silently ignoring them. Addresses https://github.com/bazelbuild/rules_cc/issues/490 PiperOrigin-RevId: 955278429 Change-Id: I92486e4f35bb355b22da2c9aedeec2d7f4213606
diff --git a/cc/common/cc_helper.bzl b/cc/common/cc_helper.bzl index 6a17c68..dc660a7 100644 --- a/cc/common/cc_helper.bzl +++ b/cc/common/cc_helper.bzl
@@ -866,7 +866,7 @@ for include in getattr(ctx.attr, attr): includes_attr = _expand(ctx, include, additional_make_variable_substitutions) if is_path_absolute(includes_attr): - continue + fail("The path '" + includes_attr + "' is absolute, but only relative paths are allowed.", attr = attr) includes_path = get_relative_path(package_exec_path, includes_attr) if not sibling_repository_layout and path_contains_up_level_references(includes_path): fail("Path references a path above the execution root.", attr = "includes")
diff --git a/tests/cc/common/cc_library_configured_target_tests.bzl b/tests/cc/common/cc_library_configured_target_tests.bzl index 95ad741..4b583bf 100644 --- a/tests/cc/common/cc_library_configured_target_tests.bzl +++ b/tests/cc/common/cc_library_configured_target_tests.bzl
@@ -80,10 +80,58 @@ target.runfiles().not_contains_predicate(matching.str_endswith(".a")) target.data_runfiles().not_contains_predicate(matching.str_endswith(".a")) +def _test_absolute_includes_fail(name): + util.helper_target( + cc_library, + name = name + "_lib", + hdrs = ["header.h"], + includes = ["/absolute/path"], + ) + cc_analysis_test( + name = name, + impl = _test_absolute_includes_fail_impl, + target = name + "_lib", + expect_failure = True, + ) + +def _test_absolute_includes_fail_impl(env, target): + expected_msg = "The path '/absolute/path' is absolute, but only relative paths are allowed." + env.expect.that_target(target).failures().contains_predicate( + matching.custom( + "contains '{}'".format(expected_msg), + lambda s: expected_msg in s, + ), + ) + +def _test_absolute_includes_windows_fail(name): + util.helper_target( + cc_library, + name = name + "_lib", + hdrs = ["header.h"], + includes = ["C:\\absolute\\path"], + ) + cc_analysis_test( + name = name, + impl = _test_absolute_includes_windows_fail_impl, + target = name + "_lib", + expect_failure = True, + ) + +def _test_absolute_includes_windows_fail_impl(env, target): + expected_msg = "The path 'C:\\absolute\\path' is absolute, but only relative paths are allowed." + env.expect.that_target(target).failures().contains_predicate( + matching.custom( + "contains '{}'".format(expected_msg), + lambda s: expected_msg in s, + ), + ) + def cc_library_configured_target_tests(name): test_suite( name = name, tests = [ _test_cc_library_data_in_runfiles, + _test_absolute_includes_fail, + _test_absolute_includes_windows_fail, ] if bazel_features.cc.cc_common_is_in_rules_cc else [], )