Skip to content

Commit 15d7c84

Browse files
authored
Rewrote filtering workspaces docs (#1879)
Improved organisation of filter docs, added a reference list at the top, simplified language and pulled out examples into different sections. NOTE - I've added imagery to this PR but it can be dropped if we don't think it's high enough quality. ![image](https://user-images.githubusercontent.com/28293365/188836995-c946ce06-e583-405f-8e74-e87b2ff175c3.png)
1 parent 5ba6ac1 commit 15d7c84

File tree

3 files changed

+98
-43
lines changed

3 files changed

+98
-43
lines changed

docs/pages/docs/core-concepts/filtering.mdx

Lines changed: 98 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,73 @@ import HeartIcon from "@heroicons/react/solid/HeartIcon";
88

99
# Filtering Workspaces
1010

11-
Turborepo supports a `pnpm`-like `--filter` flag that allows you to select the workspaces
12-
that will act as "entry points" into your monorepo's workspace/task graph.
13-
You can filter your project by workspace name, workspace directory, whether workspaces include dependents/dependencies, and by changes
14-
in git history.
11+
A monorepo can contain hundreds, or thousands, of workspaces. By default, running `turbo run test` will execute the `test` task in **all available workspaces**.
1512

16-
`turbo` will run each task against each matched workspace, ensuring that any dependencies
17-
are run first, according to the `pipeline` specification in [`turbo.json`](/docs/reference/configuration#pipeline).
13+
![Without using a filter, test will be run across all packages](../../../public/images/docs/no-filter.png)
1814

19-
## Filter Syntax
15+
Turborepo supports a `--filter` flag that lets you **select the workspaces you'd like to execute your task in**.
16+
17+
![With a filter on shared, only the shared package runs test](../../../public/images/docs/with-filter.png)
2018

21-
Filters specify combinations of workspaces, directories, and git commits to act as entrypoints
22-
for execution.
19+
You can use it to:
2320

24-
Multiple filters can be combined to select distinct sets of targets. Additionally, filters
25-
can also exclude targets. A target that matches any filter and is not explicitly excluded will
26-
be in the final entrypoint selection.
21+
- Filter by [workspace name](#filter-by-workspace-name)
22+
- Filter by [workspace directory](#filter-by-directory)
23+
- Include [dependents](#include-dependents-of-matched-workspaces) and [dependencies](#include-dependencies-of-matched-workspaces) of matched workspaces
24+
- Execute tasks from the [workspace root](#the-workspace-root)
25+
- Filter by [changes in git history](#filter-by-changed-workspaces)
26+
- [Exclude workspaces](#excluding-workspaces) from selection
2727

28-
Turborepo's filter syntax is based on pnpm's filter syntax, so it should feel familiar to pnpm users.
28+
Turborepo will run each task against each matched workspace, ensuring that any tasks which depend on it are run first, according to the `pipeline` specification in [`turbo.json`](/docs/reference/configuration#pipeline).
2929

30-
### Filter by workspace
30+
## Filter Syntax
3131

32-
Supports exact matches (`--filter=my-pkg`), and globs (`--filter=*my-pkg*`) are supported.
33-
Scoped workspaces (`@foo/bar`) can be abbreviated to just the workspace name (`bar`) as long as the workspace name
34-
is unique within the monorepo (e.g. `@foo/bar` exists, but `@baz/bar` does not).
32+
### Multiple filters
3533

36-
The monorepo's root can be selected using the token `//`.
34+
You can specify more than one filter by passing multiple `--filter` flags to the command:
35+
36+
```sh
37+
turbo run build --filter=my-pkg --filter=my-app
38+
```
39+
40+
### Filter by workspace name
41+
42+
When you want to run a script in only one workspace, you can use a single filter: `--filter=my-pkg`.
3743

3844
```sh
3945
# Build 'my-pkg', letting `turbo` infer task dependencies
4046
# from the pipeline defined in turbo.json
4147
turbo run build --filter=my-pkg
4248

43-
# Build '@foo/bar', letting `turbo` infer task dependencies
49+
# Build '@acme/bar', letting `turbo` infer task dependencies
4450
# from the pipeline defined in turbo.json
45-
turbo run build --filter=@foo/bar
51+
turbo run build --filter=@acme/bar
52+
```
53+
54+
If you want to run tasks inside several workspaces with similar names, you can use glob syntax: `--filter=*my-pkg*`.
4655

56+
```sh
4757
# Build all workspaces that start with 'admin-', letting turbo infer task
4858
# dependencies from the pipeline defined in turbo.json
4959
turbo run build --filter=admin-*
60+
```
5061

51-
# Run the format script from the root "package.json" file:
52-
turbo run format --filter=//
62+
#### Scopes
63+
64+
Some monorepos prepend their workspace names with a scope, such as `@acme/ui` and `@acme/app`. As long as the scope (`@acme`) is unique across the codebase, you may omit it from filters.
65+
66+
```diff
67+
- turbo run build --filter=@acme/ui
68+
+ turbo run build --filter=ui
5369
```
5470

5571
### Include dependents of matched workspaces
5672

57-
Prepend `...` to the filter. If `my-app` depends on `my-lib`, `...my-lib` will select `my-app` and `my-lib`.
58-
Optionally including a `^` (`...^my-lib`) will select all of `my-lib`'s dependents, but not `my-lib` itself. In this case, just `my-app` will be
59-
selected.
73+
Sometimes, you'll want to ensure that your shared package isn't affecting any downstream dependencies. For that, you can use `--filter=...my-lib`.
74+
75+
If `my-app` depends on `my-lib`, `...my-lib` will select `my-app` and `my-lib`.
76+
77+
Including a `^` (`...^my-lib`) will select all of `my-lib`'s dependents, but not `my-lib` itself.
6078

6179
```sh
6280
# Test 'my-lib' and everything that depends on 'my-lib'
@@ -68,8 +86,11 @@ turbo run test --filter=...^my-lib
6886

6987
### Include dependencies of matched workspaces
7088

71-
Append `...` to the end of the filter. If `my-app` depends on `my-lib`, `my-app...` will select `my-app` and `my-lib`.
72-
Optionally including a `^` (`my-app^...`) will select all of `my-app`'s dependencies, but not `my-app` itself. In this case, just `my-lib` will be selected.
89+
Sometimes, you'll want to make sure that `build` is run in all of the dependencies of the lib you're targeting. For that, you can use `--filter=my-app...`.
90+
91+
If `my-app` depends on `my-lib`, `my-app...` will select `my-app` and `my-lib`.
92+
93+
Including a `^` (`my-app^...`) will select all of `my-app`'s dependencies, but not `my-app` itself.
7394

7495
```sh
7596
# Build 'my-app' and its dependencies
@@ -79,49 +100,83 @@ turbo run build --filter=my-app...
79100
turbo run build --filter=my-app^...
80101
```
81102

82-
### Filter by directory or directory tree
103+
### Filter by directory
83104

84-
Supports exact matches (`--filter=./apps/docs`) and globs (`--filter=./apps/*`).
85-
When combined with other components, enclose in `{}`. For example, `--filter=...{./libs/*}`.
105+
Useful for when you want to target a specific directory, not a workspace name. It supports:
106+
107+
- Exact matches: `--filter=./apps/docs`
108+
- Globs: `--filter=./apps/*`
86109

87110
```sh
88111
# Build all of the workspaces in the 'apps' directory
89112
turbo run build --filter=./apps/*
113+
```
114+
115+
#### Combining with other syntaxes
116+
117+
When combining directory filters with other syntaxes, enclose in `{}`. For example:
90118

119+
```sh
91120
# Build all of the workspaces in the 'libs' directory,
92121
# and all the workspaces that depends on them
93122
turbo run build --filter=...{./libs/*}
94123
```
95124

96125
### Filter by changed workspaces
97126

98-
Use the set of files changed since a specified commit to calculate workspaces. Enclose references in
99-
`[]`. For example, `--filter=[HEAD^1]` will select all workspaces that have changed in the most recent
100-
commit. If you need to check a specific range of commits, rather than comparing to `HEAD`, you can set
101-
both ends of the comparison via `[<from commit>...<to commit>]`.
127+
You can run tasks on any workspaces which have changed since a certain commit. These need to be wrapped in `[]`.
128+
129+
For example, `--filter=[HEAD^1]` will select all workspaces that have changed in the most recent commit:
130+
131+
```sh
132+
# Test everything that changed in the last commit
133+
turbo run test --filter=[HEAD^1]
134+
```
135+
136+
#### Check a range of commits
137+
138+
If you need to check a specific range of commits, rather than comparing to `HEAD`, you can set both ends of the comparison via `[<from commit>...<to commit>]`.
139+
140+
```sh
141+
# Test each workspace that changed between 'main' and 'my-feature'
142+
turbo run test --filter=[main...my-feature]
143+
```
144+
145+
#### Ignoring changed files
102146

103147
You can use [`--ignore`](/docs/reference/command-line-reference#--ignore) to specify changed files to be ignored in the calculation of which workspaces have changed.
104148

149+
#### Combining with other syntaxes
150+
105151
You can additionally prepend the commit reference with `...` to match the dependencies of other components
106152
against the changed workspaces. For instance, to select `foo` if any of `foo`'s dependencies have changed in the last commit,
107-
you can pass `--filter=foo...[HEAD^1]`. Note that this feature is different from `pnpm`'s syntax.
153+
you can pass `--filter=foo...[HEAD^1]`.
108154

109155
```sh
110-
# Test everything that changed in the last commit
111-
turbo run test --filter=[HEAD^1]
112-
113156
# Build everything that depends on changes in branch 'my-feature'
114157
turbo run build --filter=...[origin/my-feature]
115158

116-
# Build '@foo/bar' if it or any of its dependencies changed in the last commit
159+
# Build '@foo/bar' if it or any of its dependencies
160+
# changed in the last commit
117161
turbo run build --filter=@foo/bar...[HEAD^1]
162+
```
163+
164+
You can even combine `[]` and `{}` syntax together:
118165

119-
# Test each workspace in the '@scope' scope that is in the 'packages' directory,
120-
# if it has changed in the last commit
166+
```sh
167+
# Test each workspace in the '@scope' scope that
168+
# is in the 'packages' directory, if it has
169+
# changed in the last commit
121170
turbo run test --filter=@scope/*{./packages/*}[HEAD^1]
171+
```
122172

123-
# Test each workspace that changed between 'main' and 'my-feature'
124-
turbo run test --filter=[main...my-feature]
173+
### The workspace root
174+
175+
The monorepo's root can be selected using the token `//`.
176+
177+
```sh
178+
# Run the format script from the root "package.json" file:
179+
turbo run format --filter=//
125180
```
126181

127182
### Excluding workspaces
1020 KB
Loading
1.02 MB
Loading

0 commit comments

Comments
 (0)