Skip to content

Commit c645f6b

Browse files
stevenlrcopybara-github
authored andcommitted
Fix absolute file paths checking for Windows
Copybara Import from #477 BEGIN_PUBLIC Fix absolute file paths checking for Windows (#477) This is a fix for #476 and uses `bazel_skylib`'s `is_absolute` function. I looked for other places that would need a similar fix but found only one more, so I may have missed some. Closes #477 END_PUBLIC COPYBARA_INTEGRATE_REVIEW=#477 from stevenlr:fix_absolute_include_paths_check_windows ba54fce PiperOrigin-RevId: 811375155 Change-Id: Ie1626dd16dd9a100f55ffcadfcfc2eedf93e646b
1 parent aaf476a commit c645f6b

File tree

7 files changed

+58
-3
lines changed

7 files changed

+58
-3
lines changed

cc/common/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bzl_library(
1919
srcs = glob(["*.bzl"]),
2020
visibility = ["//visibility:public"],
2121
deps = [
22+
"//cc/private:paths_bzl",
2223
"//cc/private/rules_impl:native_bzl",
2324
],
2425
)
@@ -29,6 +30,7 @@ bzl_library(
2930
visibility = ["//visibility:public"],
3031
deps = [
3132
":visibility_bzl",
33+
"//cc/private:paths_bzl",
3234
"//cc/private/rules_impl:objc_common",
3335
],
3436
)

cc/common/cc_helper.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""Utility functions for C++ rules."""
1515

1616
load("//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE")
17+
load("//cc/private:paths.bzl", "is_path_absolute")
1718
load("//cc/private/rules_impl:objc_common.bzl", "objc_common")
1819
load(":cc_common.bzl", "cc_common")
1920
load(
@@ -808,7 +809,7 @@ def _include_dirs(ctx, additional_make_variable_substitutions):
808809
package_source_root = _package_source_root(ctx.label.workspace_name, package, sibling_repository_layout)
809810
for include in ctx.attr.includes:
810811
includes_attr = _expand(ctx, include, additional_make_variable_substitutions)
811-
if includes_attr.startswith("/"):
812+
if is_path_absolute(includes_attr):
812813
continue
813814
includes_path = get_relative_path(package_exec_path, includes_attr)
814815
if not sibling_repository_layout and path_contains_up_level_references(includes_path):

cc/common/cc_helper_internal.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Only use those within C++ implementation. The others need to go through cc_commo
1919
"""
2020

2121
load("@bazel_skylib//lib:paths.bzl", "paths")
22+
load("//cc/private:paths.bzl", "is_path_absolute")
2223

2324
# LINT.IfChange(forked_exports)
2425

@@ -239,7 +240,7 @@ def is_stamping_enabled(ctx):
239240
# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_helper_internal.bzl:forked_exports)
240241

241242
def get_relative_path(path_a, path_b):
242-
if paths.is_absolute(path_b):
243+
if is_path_absolute(path_b):
243244
return path_b
244245
return paths.normalize(paths.join(path_a, path_b))
245246

cc/private/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
16+
1517
filegroup(
1618
name = "srcs",
1719
srcs = glob([
1820
"**/BUILD",
21+
"*.bzl",
1922
]) + [
2023
"//cc/private/rules_impl:srcs",
2124
"//cc/private/toolchain:srcs",
2225
],
2326
visibility = ["//visibility:public"],
2427
)
28+
29+
bzl_library(
30+
name = "paths_bzl",
31+
srcs = ["paths.bzl"],
32+
visibility = ["//cc:__subpackages__"],
33+
)

cc/private/paths.bzl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2025 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Paths utility functions."""
16+
17+
def is_path_absolute(path):
18+
"""Checks whether a path is absolute or not.
19+
20+
Note that is does not take the execution platform into account.
21+
22+
This was implemented to replace skylib's paths.is_absolute, which
23+
only checks for the presence of a colon character in the second position
24+
of the paths
25+
26+
Args:
27+
path: A path (as a string).
28+
29+
Returns:
30+
`True` if `path` is an absolute path.
31+
"""
32+
33+
if path.startswith("/"):
34+
return True
35+
36+
# Check for DOS-style absolute paths for Windows
37+
return len(path) >= 3 and \
38+
path[0].isalpha() and \
39+
path[1] == ":" and \
40+
path[2] in ("/", "\\")

cc/toolchains/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bzl_library(
2222
"//cc:action_names_bzl",
2323
"//cc:cc_toolchain_config_lib_bzl",
2424
"//cc:find_cc_toolchain_bzl",
25+
"//cc/private:paths_bzl",
2526
"//cc/private/rules_impl:cc_flags_supplier_lib_bzl",
2627
"//cc/private/rules_impl:native_bzl",
2728
"//cc/toolchains/impl:toolchain_impl_rules",

cc/toolchains/args.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""All providers for rule-based bazel toolchain config."""
1515

1616
load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo")
17+
load("//cc/private:paths.bzl", "is_path_absolute")
1718
load("//cc/toolchains/impl:args_utils.bzl", "validate_env_variables", "validate_nested_args")
1819
load(
1920
"//cc/toolchains/impl:collect.bzl",
@@ -50,7 +51,7 @@ def _cc_args_impl(ctx):
5051
)
5152

5253
for path in ctx.attr.allowlist_absolute_include_directories:
53-
if not path.startswith("/"):
54+
if not is_path_absolute(path):
5455
fail("`{}` is not an absolute paths".format(path))
5556

5657
nested = None

0 commit comments

Comments
 (0)