Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document 'dart pub add' support for adding dependency overrides #4726

Merged
merged 12 commits into from
Apr 4, 2023
58 changes: 39 additions & 19 deletions src/tools/pub/cmd/pub-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ _Add_ is one of the commands of the [pub tool](/tools/pub/cmd).
$ dart pub add [dev:]<package>[:<constraint>] [[dev:]<package>[:<constraint>]... ] [options]
```

This command adds the specified packages to the `pubspec.yaml` as dependencies,
and then retrieves the dependencies to resolve `pubspec.yaml`.
This command adds the specified packages to the `pubspec.yaml` as dependencies,
and then retrieves the dependencies to resolve `pubspec.yaml`.

The following example command is equivalent to
editing `pubspec.yaml` to add the `http` package,
editing `pubspec.yaml` to add the `http` package,
and then calling `dart pub get`:

```terminal
Expand All @@ -39,6 +39,26 @@ $ dart pub add foo:'>2.0.0 <3.0.1'
If `dart pub add <package>:<constraint>` is an existing dependency,
it will update the constraint.

### Adding Dependency Overrides

Starting in Dart 3, `dart pub add` supports adding dependency overrides in a similar fashion to dev dependencies. To add a dependency override, use the following syntax:
`dart pub add override:<package>:<version>`
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved

For example, to add an override for the `foo` package version `1.0.0`, run the following command:
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved

```
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
dart pub add override:foo:1.0.0
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
```

This will add an override to your `pubspec.yaml` file:
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved

```
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
dependency_overrides:
foo: 1.0.0
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
```

Note that dependency overrides can conflict with the normal dependency resolution process and should be used with caution.

shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
## `dev` dependency

The `dev:` prefix adds the package as a [dev dependency][],
Expand All @@ -61,14 +81,14 @@ $ dart pub add --dev foo
## Source descriptor

{{site.alert.version-note}}
YAML-formatted descriptor syntax was added in Dart 2.19.
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
The descriptor replaces arguments like `--path`, `--sdk`, `--git-<option>`, etc.
We still support these arguments, but the recommended method is now
YAML-descriptor only.
YAML-formatted descriptor syntax was added in Dart 2.19.
The descriptor replaces arguments like `--path`, `--sdk`, `--git-<option>`, etc.
We still support these arguments, but the recommended method is now
YAML-descriptor only.
{{site.alert.end}}

The YAML descriptor syntax allows you to add multiple packages from different
sources, and apply different options and contraints to each.
sources, and apply different options and contraints to each.

```nocode
$ dart pub add [options] [dev:]<package>[:descriptor] [[dev:]<package>[:descriptor] ...]
Expand All @@ -83,7 +103,7 @@ The syntax reflects how dependencies are written in `pubspec.yaml`.
The `descriptor` syntax cannot be used in conjunction with any of the optional
arguments it replaces. Their new, corresponding YAML sources are listed below.

### `git`
### `git`

Adds a [git dependency](/tools/pub/dependencies#git-packages).

Expand Down Expand Up @@ -139,7 +159,7 @@ _Previously the `--hosted-url=<package_server_url>` option_.

Adds a [path dependency]() on a locally stored package.

```terminal
```terminal
$ dart pub add 'foo:{"path":"../foo"}'
```

Expand Down Expand Up @@ -167,13 +187,13 @@ For options that apply to all pub commands, see
[Global options](/tools/pub/cmd#global-options).

{{site.alert.note}}
The previous `pub add` syntax for options
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
(without YAML descriptors) applies the
specified options to all the packages
included in an invocation of the command.
For example, `dart pub add test http --dev`
will add both the `test` and `http` packages
as dev dependencies.
The previous `pub add` syntax for options
(without YAML descriptors) applies the
specified options to all the packages
included in an invocation of the command.
For example, `dart pub add test http --dev`
will add both the `test` and `http` packages
as dev dependencies.
{{site.alert.end}}

### `--[no-]offline`
Expand All @@ -192,6 +212,6 @@ in immediate dependencies (`--precompile`).
To prevent precompilation, use `--no-precompile`.

{{site.alert.info}}
*Problems?*
shaikahmadnawaz marked this conversation as resolved.
Show resolved Hide resolved
See [Troubleshooting Pub](/tools/pub/troubleshoot).
_Problems?_
See [Troubleshooting Pub](/tools/pub/troubleshoot).
{{site.alert.end}}