Skip to content

Commit 84fd6e3

Browse files
authored
feat: Rework turbo ls to use query internals and add turbo query ls shorthand (#12424)
## Summary - **Refactors `turbo ls`** to execute GraphQL queries through the `QueryServer` instead of directly querying the package graph. This keeps `ls` in sync with `turbo query` semantics and eliminates duplicate querying logic. - **Adds `turbo query ls`** as a new shorthand subcommand with the same flags as `turbo ls` (`--filter`, `--affected`, `--output`, positional packages). - **Documents `turbo query ls`** in the query reference page. ## How it works `ls::run()` now accepts a `&dyn QueryServer` and uses it to execute two types of GraphQL queries: - `{ packages { items { name path } length } }` for listing packages - `{ package(name: "...") { ... tasks, allDependencies, allDependents ... } }` for package details Filter/affected flags still flow through `RunBuilder` via `Opts` — the filtered package set is captured before the `Run` is wrapped as `Arc<dyn QueryRun>`, then used to filter the GraphQL results. `turbo query ls` is handled early in `query::run()` (before the general-purpose query Run is constructed) since it needs its own Run with filter opts. Closes #11144 Closes #10869
1 parent 3ebf536 commit 84fd6e3

5 files changed

Lines changed: 365 additions & 233 deletions

File tree

apps/docs/content/docs/reference/query.mdx

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,66 @@ turbo query query.gql
4141

4242
Shorthands generate GraphQL queries for common operations so you don't need to write them by hand. The JSON output is identical to what you'd get from a raw query.
4343

44+
### `ls`
45+
46+
List packages in your monorepo. This is a shorthand for a `packages` query, equivalent to `turbo ls`.
47+
48+
```bash title="Terminal"
49+
turbo query ls [packages] [flags]
50+
```
51+
52+
With no arguments, lists all packages:
53+
54+
```bash title="Terminal"
55+
turbo query ls
56+
```
57+
58+
```json title="Output"
59+
{
60+
"packageManager": "npm",
61+
"packages": {
62+
"count": 3,
63+
"items": [
64+
{ "name": "docs", "path": "apps/docs" },
65+
{ "name": "ui", "path": "packages/ui" },
66+
{ "name": "web", "path": "apps/web" }
67+
]
68+
}
69+
}
70+
```
71+
72+
When passed package names, returns detailed information including tasks, dependencies, and dependents:
73+
74+
```bash title="Terminal"
75+
turbo query ls web
76+
```
77+
78+
#### `--filter` (`-F`)
79+
80+
Use pnpm-style package selectors to narrow the package list. Same syntax as [`turbo run --filter`](/docs/reference/command-line-reference/run#--filter).
81+
82+
```bash title="Terminal"
83+
turbo query ls --filter=web...
84+
turbo query ls -F my-app...
85+
```
86+
87+
#### `--affected`
88+
89+
Show only packages affected by changes between the current branch and `main`. Cannot be combined with `--filter`.
90+
91+
```bash title="Terminal"
92+
turbo query ls --affected
93+
```
94+
95+
#### `--output`
96+
97+
Control the output format. Defaults to `pretty` (human-readable).
98+
99+
```bash title="Terminal"
100+
turbo query ls --output json
101+
turbo query ls --output pretty
102+
```
103+
44104
### `affected`
45105

46106
Check which packages or tasks are affected by changes between two git refs.

crates/turborepo-lib/src/cli/mod.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,28 @@ pub enum QuerySubcommand {
886886
/// Check which packages or tasks are affected by changes between two git
887887
/// refs
888888
Affected(AffectedArgs),
889+
/// List packages in your monorepo (shorthand for a packages query)
890+
Ls(LsArgs),
891+
}
892+
893+
#[derive(clap::Args, Clone, Debug, PartialEq)]
894+
pub struct LsArgs {
895+
/// Show only packages that are affected by changes between
896+
/// the current branch and `main`
897+
#[clap(long, group = "scope-filter-group")]
898+
pub affected: bool,
899+
/// Use the given selector to specify package(s) to act as
900+
/// entry points. The syntax mirrors pnpm's syntax, and
901+
/// additional documentation and examples can be found in
902+
/// turbo's documentation https://turborepo.dev/docs/reference/command-line-reference/run#--filter
903+
#[clap(short = 'F', long, group = "scope-filter-group")]
904+
pub filter: Vec<String>,
905+
/// Get insight into a specific package, such as
906+
/// its dependencies and tasks
907+
pub packages: Vec<String>,
908+
/// Output format
909+
#[clap(long, value_enum)]
910+
pub output: Option<OutputFormat>,
889911
}
890912

891913
#[derive(clap::Args, Clone, Debug, PartialEq)]
@@ -1636,14 +1658,17 @@ async fn run_main(
16361658
Command::Ls {
16371659
packages, output, ..
16381660
} => {
1661+
let Some(ref query_server) = query_server else {
1662+
return Err(error::Error::QueryNotAvailable);
1663+
};
16391664
let event = CommandEventBuilder::new("info").with_parent(&root_telemetry);
16401665

16411666
event.track_call();
16421667
let output = *output;
16431668
let packages = packages.clone();
16441669
let base = CommandBase::new(cli_args, repo_root, version, color_config)?;
16451670
event.track_ui_mode(base.opts.run_opts.ui_mode);
1646-
ls::run(base, packages, event, output).await?;
1671+
ls::run(base, packages, event, output, query_server.as_ref()).await?;
16471672

16481673
Ok(0)
16491674
}

0 commit comments

Comments
 (0)