Bazel映射目录位于' src '之外的' build '



我不知道Bazel或它是如何工作的,但我必须解决这个问题,最终归结为Bazel不复制某个目录到构建

我重构了一段代码,因此首先尝试从目录private-keys读取某个键(jwk)。运行时,它总是找不到文件。我认为bazel没有将private-keys目录(与src处于同一级别)复制到build

Project/
|-- private-keys
|-- src/
|   |-- //other directories
|   |-- index.ts
|
|-- package.json
|-- BUILD.bazel

有一个映射对象复制目录在src我尝试使用../private-keys那里,但没有工作。

以下是BUILD.bazel的样子

SOURCES = glob(
["src/**/*.ts"],
exclude = [
"src/**/*.spec.ts",
"src/__mocks__/**/*.ts",
],
)
mappings_dict = {
"api": "build/api",
....
}
ts_project(
name = "compile_ts",
srcs = SOURCES,
incremental = True,
out_dir = "build",
root_dir = "src",
tsc = "@npm//typescript/bin:tsc",
tsconfig = ":ts_config_build",
deps = DEPENDENCIES + TYPE_DEPENDENCIES,
)
ts_project(
name = "compile_ts",
srcs = SOURCES,
incremental = True,
out_dir = "build",
root_dir = "src",
tsc = "@npm//typescript/bin:tsc",
tsconfig = ":ts_config_build",
deps = DEPENDENCIES + TYPE_DEPENDENCIES,
)
_mappings = module_mappings(
name = "mappings",
mappings = mappings_dict,
)
# Application binary and docker imaage
nodejs_image(
name = "my-service",
data = [
":compile_ts",
"@npm//source-map-support",
] + _mappings,
entry_point = "build/index.js",
templated_args = [
"--node_options=--require=source-map-support/register",
"--bazel_patch_module_resolver",
],
)

构建目标的命令总是在单独的目录中运行。要告诉Bazel它需要复制一些额外的文件,您需要将这些文件包装在filegroup目标中(cf . Bazel docs)。

然后将这样的文件组目标添加到目标的deps属性。

在你的例子中应该是

filegroup(
name = "private_keys",
srcs = glob(["private_keys/**"]),
)
ts_project(
name = "compile_ts",
srcs = SOURCES,
data = [ ":private_keys" ], # <-- !!!
incremental = True,
out_dir = "build",
root_dir = "src",
tsc = "@npm//typescript/bin:tsc",
tsconfig = ":ts_config_build",
deps = DEPENDENCIES + TYPE_DEPENDENCIES,
)

您也可以将glob(...)直接插入data属性,但是,使其成为文件组使其可重用…让你看起来像个专业人士。:)

相关内容

最新更新