bazel 使用 gtest/gmock 报错 Constraints from @bazel_tools//platforms have been removed

问题描述

运行 bazel test 命令,遇到错误:“Constraints from @bazel_tools//platforms have been removed. Please use constraints from @platforms repository embedded in Bazel, or preferably declare dependency on https://github.com/bazelbuild/platforms. See https://github.com/bazelbuild/bazel/issues/8622 for details”

ERROR: /.../bazel_tools/platforms/BUILD:89:6: in alias rule @bazel_tools//platforms:windows: Constraints from @bazel_tools//platforms have been removed. Please use constraints from @platforms repository embedded in Bazel, or preferably declare dependency on https://github.com/bazelbuild/platforms. See https://github.com/bazelbuild/bazel/issues/8622 for details.
ERROR: /.../bazel_tools/platforms/BUILD:89:6: Analysis of target '@bazel_tools//platforms:windows' failed
ERROR: /.../google/BUILD.bazel:113:11: errors encountered resolving select() keys for @google//:gtest_main

问题分析

该错误是项目使用的 gtest 版本太老,里面用了 @bazel_tools//platforms 这种限制形式,而这一用法在新版的 bazel 中已经废弃,改为内置的 @platforms

解决方案

解决办法是更新 gtest 的版本。不同项目可能会有差异,无法直接照搬,这里给出的示例仅供参考。修改 WORKSPACE 文件中引入 gtest 的地方,把 url 换成一个新版 gtest 的地址,同时修改 strip_prefix 字段:

http_archive(
    name = "google",
    strip_prefix = "googletest-5ab508a01f9eb089207ee87fd547d290da39d015",
    url = "https://github.com/google/googletest/archive/5ab508a01f9eb089207ee87fd547d290da39d015.zip",
)
posted @ 2023-08-06 17:21  Zijian/TENG  阅读(406)  评论(0编辑  收藏  举报