4

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

我重构了代码,因此首先尝试从目录中读取某个键 (jwk) private-keys。运行时总是找不到文件。我认为 bazel 没有将private-keys目录(srcbuild.

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",
    ],
)
4

1 回答 1

1

构建目标的命令始终在单独的目录中运行。要告诉 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属性中,但是,将其设置为文件组可以使其可重用......并使您看起来像专业人士。:)

于 2021-08-31T19:53:08.380 回答