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 pub workspaces #6117

Merged
merged 47 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
3e48208
Document pub workspaces
sigurdm Sep 30, 2024
5f7c04f
Merge branch 'main' into document_workspaces
parlough Sep 30, 2024
d81c9c5
Fix use of old aside/alert syntax
parlough Sep 30, 2024
01bcd66
merge
sigurdm Oct 4, 2024
3425deb
update golink
sigurdm Oct 4, 2024
4140cc7
Merge branch 'document_workspaces' of https://github.com/sigurdm/site…
sigurdm Oct 4, 2024
0296080
Merge branch 'main' into document_workspaces
sigurdm Oct 7, 2024
e8bc54c
Add glossary section
sigurdm Oct 7, 2024
142210c
Document command behavior
sigurdm Oct 7, 2024
391746e
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
7e8aa22
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
fcfdb23
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
4506906
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
6734fa0
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
d403e89
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
f8a0c03
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
1bf11b6
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
f3b45fa
Update src/content/guides/packages.md
sigurdm Oct 7, 2024
46caed4
Fix site-shared submodule
parlough Oct 8, 2024
22ed74d
Merge branch 'main' into document_workspaces
parlough Oct 8, 2024
2efeb9e
Don't use explicit hosted: https://pub.dev
sigurdm Oct 8, 2024
309bd33
Update src/content/guides/packages.md
sigurdm Oct 10, 2024
3d7d790
Merge branch 'main' into document_workspaces
sigurdm Nov 8, 2024
07f1611
Update src/content/guides/packages.md
sigurdm Nov 12, 2024
a487e68
Update src/content/guides/packages.md
sigurdm Nov 12, 2024
ecca39c
Update src/content/guides/packages.md
sigurdm Nov 12, 2024
3b8c6af
Move to separate page
sigurdm Nov 12, 2024
35fec1a
'In a workspace' consistently after 'options'
sigurdm Nov 12, 2024
f974f71
Update src/content/tools/pub/glossary.md
sigurdm Nov 12, 2024
afc6ad9
Update src/content/tools/pub/glossary.md
sigurdm Nov 12, 2024
1cd18bd
Incorporate suggestions
sigurdm Nov 12, 2024
8f5f78b
Update glossary link
sigurdm Nov 12, 2024
5c83bbf
Merge branch 'main' into document_workspaces
MaryaBelanger Nov 12, 2024
1934e67
minor fixes, site-shared test
MaryaBelanger Nov 14, 2024
b20853c
Merge branch 'main' into document_workspaces
parlough Nov 15, 2024
7e6358c
Reupdate site-shared submodule
parlough Nov 15, 2024
2b2e31f
Fix pub workspace links
parlough Nov 15, 2024
d0940f6
Apply suggestions from code review
MaryaBelanger Nov 19, 2024
d017546
close loop on analysis context upside
MaryaBelanger Nov 26, 2024
32c2ffc
Add section about stray files
sigurdm Dec 3, 2024
af90c07
Revert "Add section about stray files"
sigurdm Dec 3, 2024
52de581
remove mention of analysis contexts
MaryaBelanger Dec 6, 2024
b38112e
Merge branch 'main' into document_workspaces
parlough Dec 10, 2024
8d4ff0a
Move to pub directory
parlough Dec 10, 2024
2f5900c
Clean up glossary entry
parlough Dec 10, 2024
3863c2f
Minor formatting fixes to doc
parlough Dec 10, 2024
fc13a5f
Add a /to/ link to the workspaces docs
parlough Dec 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
{ "source": "/go/ffi", "destination": "/interop/c-interop", "type": 301 },
{ "source": "/go/flutter-upper-bound-deprecation", "destination": "https://github.com/flutter/flutter/issues/68143", "type": 301 },
{ "source": "/go/macros", "destination": "/language/macros", "type": 301 },
{ "source": "/go/pub-workspaces", "destination": "https://flutter.dev/go/pub-workspace", "type": 301 },
{ "source": "/go/pub-workspaces", "destination": "/tools/pub/workspaces", "type": 301 },
{ "source": "/go/non-promo-conflicting-getter", "destination": "/tools/non-promotion-reasons#getter-name", "type": 301 },
{ "source": "/go/non-promo-conflicting-non-promotable-field", "destination": "/tools/non-promotion-reasons#field-name", "type": 301 },
{ "source": "/go/non-promo-conflicting-noSuchMethod-forwarder", "destination": "/tools/non-promotion-reasons#nosuchmethod", "type": 301 },
Expand Down Expand Up @@ -375,6 +375,7 @@
{ "source": "/to/doc-comment-references", "destination": "/tools/doc-comments/references", "type": 301 },
{ "source": "/to/enforce-lockfile", "destination": "/tools/pub/packages#get-dependencies-for-production", "type": 301 },
{ "source": "/to/main-function", "destination": "/language/functions#main", "type": 301 },
{ "source": "/to/pub-workspaces", "destination": "/tools/pub/workspaces", "type": 301 },
{ "source": "/to/web-debug-extension", "destination": "https://chromewebstore.google.com/detail/dart-debug-extension/eljbmlghnomdjgdjmbdekegdkbabckhm", "type": 301 },

{ "source": "/tools/**/download{,.html,/**}", "destination": "/get-dart", "type": 301 },
Expand Down
2 changes: 2 additions & 0 deletions src/_data/side-nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
permalink: /tools/pub/publishing
- title: Writing package pages
permalink: /tools/pub/writing-package-pages
- title: Workspaces (monorepo support)
permalink: /tools/pub/workspaces
- title: Package reference
expanded: false
children:
Expand Down
4 changes: 4 additions & 0 deletions src/content/tools/pub/cmd/pub-add.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,9 @@ By default, pub precompiles executables
in immediate dependencies (`--precompile`).
To prevent precompilation, use `--no-precompile`.

## In a workspace

In a [Pub workspace](/tools/pub/workspaces) `dart pub add` will add
dependencies only to the package in the current directory.

{% render 'pub-problems.md' %}
5 changes: 5 additions & 0 deletions src/content/tools/pub/cmd/pub-deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,8 @@ Generates output in JSON format.


{% render 'pub-problems.md' %}

## In a workspace

In a [Pub workspace](/tools/pub/workspaces) `dart pub deps` will list
dependencies for all packages in the workspace, one workspace package at a time.
7 changes: 5 additions & 2 deletions src/content/tools/pub/cmd/pub-downgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ In offline mode, pub looks only in your local package cache,
trying to find a set of versions that work with your package from what's already
available.



## Options

For options that apply to all pub commands, see
Expand All @@ -122,3 +120,8 @@ resolved versions, and returns a list of the changed constraints.
Can be applied to [specific dependencies](#downgrading-specific-dependencies).

{% render 'pub-problems.md' %}

## In a workspace

In a [Pub workspace](/tools/pub/workspaces) `dart pub downgrade` will
downgrade all dependencies across the workspace.
5 changes: 5 additions & 0 deletions src/content/tools/pub/cmd/pub-lish.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ You can either wait a few minutes in between publishing the first and the second
to publish the second package immediately, by side-stepping client-side validation.

{% render 'pub-problems.md' %}

## In a workspace

In a [Pub workspace](/tools/pub/workspaces) `dart pub publish` publishes
the package in the current directory.
6 changes: 5 additions & 1 deletion src/content/tools/pub/cmd/pub-outdated.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ When you edit the `pubspec.yaml` file,
you generally update the **dependencies** and **dev_dependencies** sections
so that each package uses the versions in the **Resolvable** column.


## Options

For options that apply to all pub commands, see
Expand Down Expand Up @@ -234,6 +233,11 @@ are at the latest version (`--no-up-to-date`).
To include up-to-date dependencies, use `--up-to-date`.


## In a workspace

In a [Pub workspace](/tools/pub/workspaces) `dart pub outdated` lists
all dependencies

{% render 'pub-problems.md' %}

[`args`]: {{site.pub-pkg}}/args
Expand Down
4 changes: 4 additions & 0 deletions src/content/tools/pub/cmd/pub-remove.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@ By default, pub precompiles executables
in immediate dependencies (`--precompile`).
To prevent precompilation, use `--no-precompile`.

## In a workspace

In a [Pub workspace](/tools/pub/workspaces) `dart pub remove` removes
dependencies from the package in the current directory.

{% render 'pub-problems.md' %}
10 changes: 9 additions & 1 deletion src/content/tools/pub/cmd/pub-upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ offline `dart pub upgrade` locks your app to that old version.
The next time you are online, you will likely want to
run `dart pub upgrade` again to upgrade to a later version.


## Options

The `dart pub upgrade` command supports the
Expand Down Expand Up @@ -160,4 +159,13 @@ When used with a list of packages to unlock, first the transitive closure of
those packages' dependencies (in the current resolution) is computed,
and then all those packages are unlocked.

## In a workspace

In a [Pub workspace](/tools/pub/workspaces) `dart pub upgrade` will
upgrade all dependencies in the shared resolution from across all workspace
packages.

`dart pub upgrade --major-versions` and `dart pub upgrade --tighten` will update
constraints in all workspace `pubspec.yaml` files.

{% render 'pub-problems.md' %}
11 changes: 11 additions & 0 deletions src/content/tools/pub/glossary.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,14 @@ since they use the [lockfile](#lockfile) to manage their dependency versions.

For more information, see
[Pub Versioning Philosophy](/tools/pub/versioning).

## Workspace

A collection of packages that are developed together with a
shared resolution of their dependency constraints.
Useful for developing in a monorepo.

The packages have a shared `pubspec.lock` and `.dart_tool/package_config.json`.

To learn more about setting up and developing in a workspace,
check out [Pub workspaces](/tools/pub/workspaces).
1 change: 1 addition & 0 deletions src/content/tools/pub/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ the pub package manager.

* [Creating packages](/tools/pub/create-packages)
* [Publishing packages](/tools/pub/publishing)
* [Pub workspaces (monorepo support)](/tools/pub/workspaces)

### Reference

Expand Down
182 changes: 182 additions & 0 deletions src/content/tools/pub/workspaces.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
---
title: Pub workspaces (monorepo support)
short-title: Workspaces
description: Learn more about pub workspaces, a way to manage package monorepos.
---

When working on a project, you might develop multiple Dart packages in the same
version control repository (a _monorepo_).

For example you might have a directory layout like:

```plaintext
/
packages/
shared/
pubspec.yaml
pubspec.lock
.dart_tool/package_config.json
client_package/
pubspec.yaml
pubspec.lock
.dart_tool/package_config.json
server_package/
pubspec.yaml
pubspec.lock
.dart_tool/package_config.json
```

There are some downsides to this setup:

* You need to run `dart pub get` once for each package.
* You risk ending up with different versions of dependencies for each package,
leading to confusion when context switching between the packages.
* If you open the root folder in your IDE, the dart analyzer will create
separate analysis contexts for each package, increasing memory usage.

Pub allows you to organize your repository as a _workspace_ using a single
shared resolution for all your packages.
Using workspaces for large repositories reduces the amount of memory
required for analysis, hence improving performance.

:::note
Using a single shared dependency resolution for all your packages increases
the risks of dependency conflicts, because Dart doesn't allow multiple versions
of the same package.

If the packages are going to be used together (as is commonly the case),
this risk is a useful feature. It forces you to resolve incompatibilities between
your packages when they arise, rather than when you start using the packages.
:::

To create a workspace:

* Add a `pubspec.yaml` at the repository root directory with a `workspace` entry
enumerating the paths to the packages of the repository (the workspace
packages):

```yaml
name: _
publish_to: none
environment:
sdk: ^3.6.0
workspace:
- packages/helper
- packages/client_package
- packages/server_package
```
* For each of the existing `pubspec.yaml` files, make sure their SDK constraint
is at least `^3.6.0` and add a `resolution` entry:

```yaml
environment:
sdk: ^3.6.0
resolution: workspace
```

* Run `dart pub get` anywhere in the repository. This will:
* Create a single `pubspec.lock` next to the root `pubspec.yaml` that contains
the resolution of all the `dependencies` and `dev_dependencies` of all the
workspace packages.
* Create a single shared `.dart_tool/package_config.json` that maps package
names to file locations.
* Delete any other existing `pubspec.lock` and
`.dart_tool/package_config.json` files next to workspace packages.

Now the file structure looks like this:

```plaintext
/
packages/
shared/
pubspec.yaml
client_package/
pubspec.yaml
server_package/
pubspec.yaml
pubspec.yaml
pubspec.lock
.dart_tool/package_config.json
```

:::version-note
Support for pub workspaces was introduced in Dart 3.6.0.

To use pub workspaces, all your workspace packages (but not your dependencies)
must have an SDK version constraint of `^3.6.0` or higher.
:::

## Interdependencies between workspace packages

If any of the workspace packages depend on each other, they will automatically
resolve to the one in the workspace, regardless of the source.

Eg. `packages/client_package/pubspec.yaml` might depend on `shared`:

```yaml
dependencies:
shared: ^2.3.0
```

When resolved inside the workspace, the _local_ version of `shared` will be
used.

The local version of `shared` would still have to match the constraint
(`^2.3.0`) though.

But when the package is consumed as a dependency without being part of the
workspace, the original source (here implicitly `hosted`) is used.

So if `client_package` is published to pub.dev and someone depends on it, they
will get the hosted version of `shared` as a transitive dependency.

## Dependency overrides in a workspace

All `dependency_overrides` sections in the workspace packages are respected.
You can also place a `pubspec_overrides.yaml` file next to any of the
workspace `pubspec.yaml` files.

You can only override a package once in the workspace. To keep overrides organized,
it's preferable to keep `dependency_overrides` in the root `pubspec.yaml`.

## Running a command in a specific workspace package

Some pub commands, such as `dart pub add`, and `dart pub publish` operate on a
"current" package. You can either change the directory, or use `-C` to point pub at
a directory:

```console
$ dart pub -C packages/client_package publish
# Same as
$ cd packages/client_package ; dart pub publish ; cd -
```

## Temporarily resolving a package outside its workspace:

Sometimes you might want to resolve a workspace package on its own, for example
to validate its dependency constraints.

One way to do this is to create a `pubspec_overides.yaml` file that resets the
`resolution` setting, like so:

```yaml
# packages/client_package/pubspec_overrides.yaml
resolution:
```

Now running `dart pub get` inside `packages/client_package` will create an
independent resolution.

## Listing all workspace packages

You can run `dart pub workspace list` to list the packages of a workspace.

```console
$ dart pub workspace list
Package Path
_ ./
client_package packages/client_package/
server_package packages/server_package/
shared packages/shared/
```
Loading