Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: export dependency graph as adjacency list #20566

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions docs/docs/using-pants/project-introspection.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,72 @@ To include the original target itself, use `--closed`:
helloworld/main.py:lib
```

## Export dependency graph

Both `dependencies` and `dependents` goals have the `--format` option allowing you to export data in multiple formats.
Exporting information about the dependencies and dependents in JSON format will produce the
[adjacency list](https://en.wikipedia.org/wiki/Adjacency_list) of your dependency graph:

```bash
$ pants dependencies --format=json \
helloworld/greet/greeting.py \
helloworld/translator/translator_test.py

{
"helloworld/greet/greeting.py:lib": [
"//:reqs#setuptools",
"//:reqs#types-setuptools",
"helloworld/greet:translations",
"helloworld/translator/translator.py:lib"
],
"helloworld/translator/translator_test.py:tests": [
"//:reqs#pytest",
"helloworld/translator/translator.py:lib"
]
}
```

This has various applications, and you could analyze, visualize, and process the data further. Sometimes, a fairly
straightforward `jq` query would suffice, but for anything more complex, it may make sense to write a small program
to process the exported graph. For instance, you could:

* find tests with most transitive dependencies

```bash
$ pants dependencies --filter-target-type=python_test --format=json :: \
| jq -r 'to_entries[] | "\(.key)\t\(.value | length)"' \
| sort -k2 -n
```

* find resources that only a few other targets depend on

```bash
$ pants dependents --filter-target-type=resource --format=json :: \
| jq -r 'to_entries[] | select(.value | length < 2)'
```

* find files within the `src/` directory that transitively lead to the most tests

```python
# depgraph.py
import json

with open("data.json") as fh:
data = json.load(fh)

for source, dependents in data.items():
print(source, len([d for d in dependents if d.startswith("tests/")]))
```

```bash
$ pants dependents --transitive --format=json src:: > data.json
$ python3 depgraph.py | sort -k2 -n
```

For more sophisticated graph querying, you may want to look into graph libraries such as [`networkx`](https://networkx.org/).
In a larger repository, it may make sense to track the health of the dependency graph and use the output
of the graph export to identify parts of your codebase that would benefit from refactoring.

## `filedeps` - find which files a target owns

`filedeps` outputs all of the files belonging to a target, based on its `sources` field.
Expand Down
Loading