Search code examples
graphbazelbazel-query

Exclude @npm// Dependencies from Bazel Dependency Graph (bazel query)


I use the following commands to generate a graph for my Bazel project.

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

The following graph is generated:

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"
}

As you can see this graph is ridiculously huge, because of all those dependencies from @npm//<something> ra

What I actually want is something like this:

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"
}

good graph


Is it possible to remove all those npm-dependencies in the graph without manually removing them from the graph.in file?


Solution

  • When you describe the targets you're interested in (e.g. with deps()), you should be able to ask for more or (in this case less):

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

    or:

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