Skip to content

feat(tree): json representation - #4261

Merged
Hofer-Julian merged 4 commits into
prefix-dev:mainfrom
abkfenris:tree-json
Nov 11, 2026
Merged

Hofer-Julian merged 4 commits into
prefix-dev:mainfrom
abkfenris:tree-json

Conversation

@abkfenris

@abkfenris abkfenris commented Aug 2, 2025 •

Copy link
Copy Markdown
Contributor

Description

Fixes #4130

Add pixi tree --json and pixi global tree --json, so tools like marimo don't have to parse the box-drawing output anymore.
Builds on the initial version by @abkfenris, adapted to the current tree code.

  • Output is a JSON array of root nodes, nested and deduplicated like the text output
  • Each node has name, version, kind (conda/pypi), is_explicit, via_extras, is_deduplicated and dependencies
  • Works with the regex filter and --invert, the children are then listed under required_by
  • Every direct dependency is now listed as a root, marked with (*) if it was already shown below another one
  • Inverted trees are sorted instead of being in hash map order
❯ pixi tree --json --manifest-path examples/editable-with-extras
[
  {
    "name": "package_with_extras",
    "version": null,
    "kind": "pypi",
    "is_explicit": true,
    "via_extras": [],
    "is_deduplicated": false,
    "dependencies": [
      {
        "name": "boltons",
        "version": "24.0.0",
        "kind": "pypi",
        "is_explicit": false,
        "via_extras": [],
        "is_deduplicated": false,
        "dependencies": []
      },
      {
        "name": "rich",
        "version": "13.7.1",
        "kind": "pypi",
        "is_explicit": false,
        "via_extras": ["color"],
        "is_deduplicated": false,
        "dependencies": [
...

For marimo, via_extras maps to the extra tags and is_deduplicated to the dedupe tag of their DependencyTreeNode.
Filtering on kind == "pypi" replaces the cross-check with pixi list --json.

AI-generated details (Claude Opus 5.5)
  • crates/pixi_cli/src/shared/tree.rs now builds a Tree of TreeNodes once, print_tree and print_tree_json render it. This keeps filtering, deduplication and ordering identical between both outputs.
  • Package::version is now Option<String>, so locked packages without a version serialize as null. Text shows <unknown> for them.
  • The text output only changes in three places: duplicate roots are shown with (*), the tree no longer ends in a dangling ├── when the last direct dependency was already shown, and -i output is sorted.
  • Tested with insta snapshots on synthetic graphs (cycles, unlocked dependencies, extras, regex fallback, invert) and on examples/editable-with-extras.
  • A throwaway end-to-end suite (26 tests against the debug binary) checked that stdout is pure JSON, text and JSON agree, and covered --environment, --script, PyPI extras and pixi global tree. All passed; it isn't part of this PR.

Checklist:

  • I've discussed the proposed changes with a maintainer.
  • I understand the purpose of my changes and how they interact with the greater system.
  • User-facing changes are documented and prose in the documentation has been written/edited/reviewed by hand.

@abkfenris abkfenris mentioned this pull request Aug 2, 2025
@abkfenris
abkfenris force-pushed the tree-json branch 2 times, most recently from c391e7f to 40dbb1a Compare August 2, 2025 01:35
Adds a JSON output allowing `pixi tree —json` to hopefully be easily 
consumable by tools like marimo.

Closes prefix-dev#4130
@abkfenris
abkfenris marked this pull request as ready for review August 2, 2025 01:48
@lucascolley

Copy link
Copy Markdown
Member

cc @manzt

@ruben-arts ruben-arts left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @abkfenris, I really want this feature!

@MantZ would you be able to give this a swirl?

Comment thread src/cli/tree.rs Outdated
name: String,
version: String,
tags: Vec<String>,
dependencies: Vec<DependencyTreeNode>,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to jam a little more info in here, we've had people ask for the size and I would say it misses the type (conda or pypi)

Comment thread src/cli/tree.rs Outdated
Comment on lines +175 to +182
.map_err(|e| {
if e.kind() == std::io::ErrorKind::BrokenPipe {
// Exit gracefully
std::process::exit(0);
} else {
e
}
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We dont do this anywhere else, can you explain why this is needed?

@lucascolley lucascolley changed the title feat: Tree json representation feat(tree): json representation Sep 8, 2025
@pavelzw

pavelzw commented Nov 5, 2025

Copy link
Copy Markdown
Collaborator

What is the status of this PR?

@abkfenris

Copy link
Copy Markdown
Contributor Author

I was waiting for feedback from the Marimo folks before doing another pass.

Build the tree once and render it as text or JSON, so both share filtering and
deduplication. Works for `pixi tree` and `pixi global tree`, including the
regex filter and `--invert`.

While I was at it:
- List every direct dependency as a root, marked with `(*)` if it was already shown
- Sort the roots and dependents of the inverted tree, they were in hash map order
- Show `<unknown>` for locked packages without version, also for conda

Closes prefix-dev#4130
@Hofer-Julian
Hofer-Julian self-requested a review November 11, 2026 13:18

@Hofer-Julian Hofer-Julian left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I pushed a few improvements to your branch.

@Hofer-Julian
Hofer-Julian merged commit 8763a7c into prefix-dev:main Nov 11, 2026
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add pixi tree --json

6 participants