You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/pages/docs/core-concepts/filtering.mdx
+98-43Lines changed: 98 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,55 +8,73 @@ import HeartIcon from "@heroicons/react/solid/HeartIcon";
8
8
9
9
# Filtering Workspaces
10
10
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**.
15
12
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
+

18
14
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
+

20
18
21
-
Filters specify combinations of workspaces, directories, and git commits to act as entrypoints
22
-
for execution.
19
+
You can use it to:
23
20
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
27
27
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).
29
29
30
-
###Filter by workspace
30
+
## Filter Syntax
31
31
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
35
33
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`.
If you want to run tasks inside several workspaces with similar names, you can use glob syntax: `--filter=*my-pkg*`.
46
55
56
+
```sh
47
57
# Build all workspaces that start with 'admin-', letting turbo infer task
48
58
# dependencies from the pipeline defined in turbo.json
49
59
turbo run build --filter=admin-*
60
+
```
50
61
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
53
69
```
54
70
55
71
### Include dependents of matched workspaces
56
72
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.
60
78
61
79
```sh
62
80
# Test 'my-lib' and everything that depends on 'my-lib'
@@ -68,8 +86,11 @@ turbo run test --filter=...^my-lib
68
86
69
87
### Include dependencies of matched workspaces
70
88
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.
73
94
74
95
```sh
75
96
# Build 'my-app' and its dependencies
@@ -79,49 +100,83 @@ turbo run build --filter=my-app...
79
100
turbo run build --filter=my-app^...
80
101
```
81
102
82
-
### Filter by directory or directory tree
103
+
### Filter by directory
83
104
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/*`
86
109
87
110
```sh
88
111
# Build all of the workspaces in the 'apps' directory
89
112
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:
90
118
119
+
```sh
91
120
# Build all of the workspaces in the 'libs' directory,
92
121
# and all the workspaces that depends on them
93
122
turbo run build --filter=...{./libs/*}
94
123
```
95
124
96
125
### Filter by changed workspaces
97
126
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
102
146
103
147
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.
104
148
149
+
#### Combining with other syntaxes
150
+
105
151
You can additionally prepend the commit reference with `...` to match the dependencies of other components
106
152
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]`.
108
154
109
155
```sh
110
-
# Test everything that changed in the last commit
111
-
turbo run test --filter=[HEAD^1]
112
-
113
156
# Build everything that depends on changes in branch 'my-feature'
114
157
turbo run build --filter=...[origin/my-feature]
115
158
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
117
161
turbo run build --filter=@foo/bar...[HEAD^1]
162
+
```
163
+
164
+
You can even combine `[]` and `{}` syntax together:
118
165
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
121
170
turbo run test --filter=@scope/*{./packages/*}[HEAD^1]
171
+
```
122
172
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:
0 commit comments