bazel: '@bazel/typescript' 不是包
bazel: '@bazel/typescript' is not a package
我尝试加载 ts_project
失败并显示以下错误消息:
ERROR: error loading package '': Label '@npm//@bazel/typescript:index.bzl' is invalid because '@bazel/typescript' is not a package; perhaps you meant to put the colon here: '@npm//:@bazel/typescript/index.bzl'?
WORKSPACE
:
workspace(
name = "nodejs",
managed_directories = {"@npm": ["node_modules"]},
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "cfc289523cf1594598215901154a6c2515e8bf3671fd708264a6f6aefe02bf39",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.4.6/rules_nodejs-4.4.6.tar.gz"],
)
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
npm_install(
name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
)
BUILD
:
load("@npm//@bazel/typescript:index.bzl", "ts_project")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
ts_project(
name = "lib",
srcs = glob(["src/**/*.ts"]),
tsconfig = "tsconfig.json",
deps = [
"@npm//@types/lodash"
],
declaration = True,
source_map = True,
)
nodejs_test(
name = "index_test",
entry_point = ":src/index.ts",
data = [
"@npm//lodash",
":lib"
],
)
怎么了?
假设您有一个最小的 package.json 没有在这里展示给我们,比如
{
"name": "test",
"version": "1.0.0"
}
然后,如果您检查 @npm
中的内容,您会发现其中没有定义包。
❯ bazel query @npm//...
@npm//:node_modules
Loading: 1 packages loaded
您需要将 @bazel/typescript
添加到您的 package.json 才能让 npm_install
可供您使用。所以运行
❯ npm install @bazel/typescript
up to date, audited 25 packages in 644ms
found 0 vulnerabilities
package.json 现在有
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"@bazel/typescript": "^4.4.6"
}
}
我们的 Bazel 查询现在显示了一堆可用的包:
❯ bazel query @npm//...
@npm//tsutils:tsutils__umd
@npm//tsutils:tsutils__typings
@npm//tsutils:tsutils__all_files
[...]
@npm//@bazel/typescript:typescript__nested_node_modules
@npm//@bazel/typescript:typescript__files
Loading: 0 packages loaded
包括 load
语句之前未找到的那个。此时你可以尝试再次构建你的包,确保添加你需要的所有依赖项(比如 lodash)。
我尝试加载 ts_project
失败并显示以下错误消息:
ERROR: error loading package '': Label '@npm//@bazel/typescript:index.bzl' is invalid because '@bazel/typescript' is not a package; perhaps you meant to put the colon here: '@npm//:@bazel/typescript/index.bzl'?
WORKSPACE
:
workspace(
name = "nodejs",
managed_directories = {"@npm": ["node_modules"]},
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "build_bazel_rules_nodejs",
sha256 = "cfc289523cf1594598215901154a6c2515e8bf3671fd708264a6f6aefe02bf39",
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.4.6/rules_nodejs-4.4.6.tar.gz"],
)
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
npm_install(
name = "npm",
package_json = "//:package.json",
package_lock_json = "//:package-lock.json",
)
BUILD
:
load("@npm//@bazel/typescript:index.bzl", "ts_project")
load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
ts_project(
name = "lib",
srcs = glob(["src/**/*.ts"]),
tsconfig = "tsconfig.json",
deps = [
"@npm//@types/lodash"
],
declaration = True,
source_map = True,
)
nodejs_test(
name = "index_test",
entry_point = ":src/index.ts",
data = [
"@npm//lodash",
":lib"
],
)
怎么了?
假设您有一个最小的 package.json 没有在这里展示给我们,比如
{
"name": "test",
"version": "1.0.0"
}
然后,如果您检查 @npm
中的内容,您会发现其中没有定义包。
❯ bazel query @npm//...
@npm//:node_modules
Loading: 1 packages loaded
您需要将 @bazel/typescript
添加到您的 package.json 才能让 npm_install
可供您使用。所以运行
❯ npm install @bazel/typescript
up to date, audited 25 packages in 644ms
found 0 vulnerabilities
package.json 现在有
{
"name": "test",
"version": "1.0.0",
"dependencies": {
"@bazel/typescript": "^4.4.6"
}
}
我们的 Bazel 查询现在显示了一堆可用的包:
❯ bazel query @npm//...
@npm//tsutils:tsutils__umd
@npm//tsutils:tsutils__typings
@npm//tsutils:tsutils__all_files
[...]
@npm//@bazel/typescript:typescript__nested_node_modules
@npm//@bazel/typescript:typescript__files
Loading: 0 packages loaded
包括 load
语句之前未找到的那个。此时你可以尝试再次构建你的包,确保添加你需要的所有依赖项(比如 lodash)。