首页 > 解决方案 > 从 Bazel 依赖关系图中排除 @npm// 依赖关系(bazel 查询)

问题描述

我使用以下命令为我的 Bazel 项目生成图表。

bazel query 'deps(//services/gateway:lib)' --output graph --nohost_deps --noimplicit_deps > graph.in
dot -Tpng < graph.in > graph.png

生成以下图表:

digraph mygraph {
  node [shape=box];
  "//services/gateway:lib"
  "//services/gateway:lib" -> "//services/gateway:controllers/auth.controller.ts\n//services/gateway:index.ts\n//services/gateway:controllers/index.controller.ts\n//:tsconfig.json\n//services/gateway:controllers/index.ts"
  "//services/gateway:lib" -> "@npm//@types/node:node"
  "//services/gateway:lib" -> "@npm//inversify-express-utils:inversify-express-utils"
  "//services/gateway:lib" -> "@npm//helmet:helmet"
  "//services/gateway:lib" -> "@npm//inversify:inversify"
  "@npm//inversify:inversify"
  "@npm//inversify:inversify" -> "@npm//inversify:inversify__contents"
  "@npm//inversify:inversify" -> "@npm//inversify:inversify__files"
  "@npm//inversify:inversify" -> "@bazel_tools//src/conditions:host_windows"
  
   MANY MORE LINES

 "@npm//:node_modules/bytes/index.js\n@npm//:node_modules/bytes/History.md\n@npm//:node_modules/bytes/Readme.md\n@npm//:node_modules/bytes/LICENSE\n@npm//:node_modules/bytes/package.json"
  "@npm//methods:methods__nested_node_modules"
  "@npm//array-flatten:array-flatten__files"
  "@npm//array-flatten:array-flatten__files" -> "@npm//:node_modules/array-flatten/LICENSE\n@npm//:node_modules/array-flatten/array-flatten.js\n@npm//:node_modules/array-flatten/README.md\n@npm//:node_modules/array-flatten/package.json"
  "@npm//:node_modules/array-flatten/LICENSE\n@npm//:node_modules/array-flatten/array-flatten.js\n@npm//:node_modules/array-flatten/README.md\n@npm//:node_modules/array-flatten/package.json"
}

正如你所看到的,这个图非常大,因为所有这些依赖于@npm//<something> 拉

我真正想要的是这样的:

digraph mygraph {
  node [shape=box];
  "//services/gateway:lib"
  "//services/gateway:lib" -> "//services/gateway:controllers/auth.controller.ts\n//services/gateway:index.ts\n//services/gateway:controllers/index.controller.ts\n//:tsconfig.json\n//services/gateway:controllers/index.ts"
  "//services/gateway:lib" -> "@npm//@types/node:node"
  "//services/gateway:lib" -> "@npm//inversify-express-utils:inversify-express-utils"
  "//services/gateway:lib" -> "@npm//helmet:helmet"
  "//services/gateway:lib" -> "@npm//inversify:inversify"
}

好图


是否可以在不手动从 graph.in 文件中删除它们的情况下删除图中的所有这些 npm 依赖项?

标签: graphbazelbazel-query

解决方案


当您描述您感兴趣的目标时(例如用deps()),您应该能够要求更多或(在这种情况下为更少):

deps(//services/gateway:lib) except @npm//...:*

或者:

deps(//services/gateway:lib) - @npm//...:*

推荐阅读