Skip to content

Commit fea6313

Browse files
docs: provide options to refer to a group of targets (#20522)
I believe the docs would benefit from having `cli` subsystem mentioned when explaining how to address targets. Also, the generic `target` is not referred anywhere, but is a powerful technique to provide a proxy to address multiple targets.
1 parent e144c1c commit fea6313

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

docs/docs/using-pants/advanced-target-selection.mdx

+25
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,28 @@ For other goals, you can leverage shell piping to partition the input targets in
193193
```bash
194194
pants list :: | awk 'NR % 5 == 0' | xargs pants package
195195
```
196+
197+
## Using CLI aliases
198+
199+
If setting tags on individual targets is not feasible, there are a few other options available to refer to multiple targets.
200+
201+
If you have an operation that you perform often on a certain group of targets, you can use the
202+
[cli](../../reference/subsystems/cli) subsystem options to create shortcuts. For instance, this alias
203+
would let you run `pants publish-libraries` to publish all Python distributions declared in the `src/libA` and `src/libB`
204+
directories.
205+
206+
```toml title="pants.toml"
207+
[cli.alias]
208+
publish-libraries = "--filter-target-type=python_distribution --filter-address-regex=\"['^src/libA/,^src/libB/']\" publish src::"
209+
```
210+
211+
You can use any argument or goal, and the alias doesn't need to be a "full" invocation of Pants.
212+
For instance, you could combine filtering arguments along with `--changed-since` flag and a tag to refer to long-running
213+
integration tests that have been recently modified:
214+
215+
```toml title="pants.toml"
216+
[cli.alias]
217+
--integration-long = "--changed-since --filter-target-type=python_test --tag=long"
218+
```
219+
220+
You can now invoke `pants --integration-long test tests::` to run the relevant tests.

docs/docs/using-pants/key-concepts/targets-and-build-files.mdx

+44
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,50 @@ You can use the prefix `!!` to transitively exclude a dependency, meaning that e
166166
Transitive excludes can only be used in target types that conventionally are not depended upon by other targets, such as `pex_binary`, `python_distribution`, and `python_test` / `python_tests`. This is meant to limit confusion, as using `!!` in something like a `python_source` / `python_sources` target could result in surprising behavior for everything that depends on it. (Pants will print a helpful error when using `!!` when it's not legal.)
167167
:::
168168

169+
## Using the generic `target`
170+
171+
[`target`](../../..reference/targets/target) is a generic target with no specific type.
172+
It can be used as a generic collection of targets to group related, but distinct targets into one single target.
173+
174+
### Referring to a group of targets
175+
176+
You could use the generic `target` when you need to group multiple targets to refer to them as a unit
177+
(a single dependency) to reduce repetition:
178+
179+
```python title="BUILD"
180+
target(
181+
name="python-libs",
182+
dependencies=["src/python/libraries/libA", "src/python/libraries/libB"],
183+
)
184+
```
185+
186+
If declared in the root of your workspace, you can now address the Python libraries by `//:python-libs`:
187+
188+
```bash
189+
$ pants dependencies //:python-libs
190+
````
191+
192+
### Creating aliases for targets
193+
194+
If you have some targets declared in BUILD files that are stored deep within the directory structure of your workspace,
195+
you can make it easier to refer to that target when listing that target among dependencies of other targets.
196+
197+
For example, you can simplify accessing a target by creating another target that will serve as an alias definition in
198+
a BUILD file stored in a more convenient location in the workspace, for instance, in the build root directory:
199+
200+
```python title="BUILD"
201+
target(
202+
name="deployment-bins",
203+
dependencies=["src/golang/production/cloud/deployment/binaries:tools"]
204+
)
205+
```
206+
207+
You can now refer to that target more concisely in BUILD files:
208+
209+
```python title="BUILD"
210+
python_sources(dependencies=["//:deployment-bins"])
211+
```
212+
169213
## Field default values
170214

171215
As mentioned above in [BUILD files](./targets-and-build-files.mdx#build-files), most target fields have sensible defaults. And it's easy to override those values on a specific target. But applying the same non-default value on many targets can get unwieldy, error-prone and hard to maintain. Enter `__defaults__`.

0 commit comments

Comments
 (0)