【问题标题】:Bazel map directory located outside of `src` to `build`位于`src`之外的Bazel映射目录到`build`
【发布时间】:2021-10-27 08:52:39
【问题描述】:

我不知道 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 bazel-rules-nodejs


    【解决方案1】:

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

    然后将此类文件组目标添加到目标的deps 属性中。(参见示例here)。

    在你的情况下,它会是这样的

    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 属性,但是,将其设置为文件组使其可重复使用...并使您看起来像专业人士。 :)

    【讨论】:

    • 不适合我。 private-keys 仍然不在构建中。
    猜你喜欢
    • 2021-06-02
    • 2019-02-20
    • 2021-07-23
    • 1970-01-01
    • 2017-10-29
    • 2021-05-16
    • 2020-03-02
    • 1970-01-01
    相关资源
    最近更新 更多