Changesets in a Monorepo
semantic-release reads intent from commit messages. Changesets asks for it explicitly: every pull request that should release something adds a small Markdown file, a changeset, saying which packages change, by how much, and what to write in their changelogs. That works especially well in monorepos, where one commit can touch five packages that need different bumps. The sessions use @changesets/cli 3.0.3 with npm workspaces.
The workflow
- A contributor runs
changeset add(or writes the file by hand) in their PR:.changeset/<three-words>.md. - The PR is reviewed including the changeset: the reviewer sees the bump and the changelog text.
- Changesets accumulate on
main. - At release time,
changeset versionconsumes them: bumpspackage.jsonversions, updates internal dependency ranges, writes each package'sCHANGELOG.md, and deletes the changeset files. - That result is committed (usually as an automated "Version Packages" PR), then
changeset publishpublishes and tags.
A changeset file
---
"@acme/core": minor
"@acme/cli": patch
---
Add a `parseDuration()` helper and use it for `--timeout`.The front matter maps package names to major, minor or patch; the body becomes the changelog entry for each listed package. One PR may add several changesets with different texts.
Setting up a small monorepo
changeset init asks questions interactively in version 3; writing .changeset/config.json yourself is equivalent. npm workspaces need a package-lock.json before tools can see the packages (npm install --package-lock-only writes one without downloading anything here, since the only dependency is the other workspace):
mkdir mono && cd mono && git init -q
cat > package.json <<'JSON'
{ "name": "mono", "private": true, "workspaces": ["packages/*"] }
JSON
mkdir -p packages/core packages/cli .changeset
echo '{ "name": "@acme/core", "version": "1.2.0" }' > packages/core/package.json
echo '{ "name": "@acme/cli", "version": "0.4.1", "dependencies": { "@acme/core": "^1.2.0" } }' > packages/cli/package.json
cat > .changeset/config.json <<'JSON'
{
"baseBranch": "main",
"access": "restricted",
"changelog": "@changesets/cli/changelog",
"commit": false,
"updateInternalDependencies": "patch",
"fixed": [],
"linked": [],
"ignore": []
}
JSON
npm install --package-lock-only --offline >/dev/null
git add -A && git commit -q -m "chore: set up changesets"
changeset add --minor @acme/core -m "Add a parseDuration() helper" | sed -E 's#\.changeset/[a-z]+-[a-z]+-[a-z]+\.md#.changeset/<three-words>.md#'
echo "--- the file it wrote"
cat .changeset/*-*-*.md🦋 changeset v3.0.3
Summary of changesets:
minor: @acme/core
◇ NOTE ───────────────────────────────────────────────────────────────────╮
│ │
│ All packages that depend on these whose required versions will be │
│ incompatible will also be patch bumped when this changeset is applied. │
│ │
╰──────────────────────────────────────────────────────────────────────────╯
Changeset added - you can now commit it!
If you want to modify or expand on the changeset summary, you can find it here:
.changeset/<three-words>.md
--- the file it wrote
---
"@acme/core": minor
---
Add a parseDuration() helper
The non-interactive flags (--minor <pkg>, --patch, --major, -m) are new in version 3; without them changeset add asks which packages changed. File names are random (three words) so that parallel PRs never conflict; the sed hides the name here because it differs on every run.
Versioning
changeset status shows the plan, and changeset version executes it. Here @acme/core gets a minor change and, separately, a major one, to show what happens to packages that depend on it:
mkdir mono && cd mono && git init -q
echo '{ "name": "mono", "private": true, "workspaces": ["packages/*"] }' > package.json
mkdir -p packages/core packages/cli packages/docs .changeset
echo '{ "name": "@acme/core", "version": "1.2.0" }' > packages/core/package.json
echo '{ "name": "@acme/cli", "version": "0.4.1", "dependencies": { "@acme/core": "^1.2.0" } }' > packages/cli/package.json
echo '{ "name": "@acme/docs", "version": "3.0.0", "private": true }' > packages/docs/package.json
cat > .changeset/config.json <<'JSON'
{ "baseBranch": "main", "changelog": "@changesets/cli/changelog", "commit": false,
"updateInternalDependencies": "patch", "access": "restricted", "fixed": [], "linked": [], "ignore": [] }
JSON
npm install --package-lock-only --offline >/dev/null
git add -A && git commit -q -m "chore: set up changesets"
cat > .changeset/add-duration.md <<'MD'
---
"@acme/core": minor
---
Add a `parseDuration()` helper.
MD
echo "=== minor change to core"
changeset status --verbose | tail -n +2
changeset version | tail -1
cat packages/*/package.json
git add -A && git commit -q -m "chore: version packages"
cat > .changeset/remove-legacy.md <<'MD'
---
"@acme/core": major
---
Remove the deprecated `parseTime()`; use `parseDuration()`.
MD
echo "=== major change to core"
changeset version | tail -1
cat packages/*/package.json
echo "--- packages/core/CHANGELOG.md"
cat packages/core/CHANGELOG.md
echo "--- packages/cli/CHANGELOG.md"
cat packages/cli/CHANGELOG.md
ls .changeset=== minor change to core
Packages to be bumped:
- minor
- @acme/core -> 1.3.0
- .changeset/add-duration.md
All files have been updated. Review them and commit at your leisure
{ "name": "@acme/cli", "version": "0.4.1", "dependencies": { "@acme/core": "^1.2.0" } }
{ "name": "@acme/core", "version": "1.3.0" }
{ "name": "@acme/docs", "version": "3.0.0", "private": true }
=== major change to core
All files have been updated. Review them and commit at your leisure
{ "name": "@acme/cli", "version": "0.4.2", "dependencies": { "@acme/core": "^2.0.0" } }
{ "name": "@acme/core", "version": "2.0.0" }
{ "name": "@acme/docs", "version": "3.0.0", "private": true }
--- packages/core/CHANGELOG.md
# @acme/core
## 2.0.0
### Major Changes
- Remove the deprecated `parseTime()`; use `parseDuration()`.
## 1.3.0
### Minor Changes
- Add a `parseDuration()` helper.
--- packages/cli/CHANGELOG.md
# @acme/cli
## 0.4.2
### Patch Changes
- Updated dependencies
- @acme/core@2.0.0
config.json
What the two runs show:
- Minor change: only
@acme/coremoved (1.2.0 → 1.3.0).@acme/cliwas left alone: its range^1.2.0already accepts 1.3.0, so nothing in it needs to change. - Major change: core went to 2.0.0, which
^1.3.0does not accept. Changesets updated the range in@acme/clito^2.0.0and gave cli a patch release with an "Updated dependencies" changelog entry, because a published cli 0.4.1 would otherwise keep pulling the old core. @acme/docsisprivate; it is never versioned (configurable withprivatePackages).- The consumed changeset files were deleted; only
config.jsonremains. - The changelog contains no commit links: the default
@changesets/cli/changelogformatter is plain.@changesets/changelog-githubadds PR links and author thanks (it calls the GitHub API, so it needs a token and the network).
Fixed and linked packages
Two config options group packages' versions:
| Option | Meaning | Use for |
|---|---|---|
"fixed": [["@acme/core", "@acme/cli"]] |
always the same version; a bump to one bumps all | packages users install together and expect to match (a framework and its plugins) |
"linked": [["@acme/core", "@acme/cli"]] |
packages that are bumped in the same release share the highest version; untouched ones stay | loosely related packages with a shared version line |
"ignore": ["@acme/experimental"] |
never version these | work in progress |
Test yourself
Where does Changesets read the release intent from?
- Commit messages
- Markdown files in
.changeset/, added in pull requests - Git tags
- package.json scripts
core goes from 1.3.0 to 2.0.0 and cli depends on ^1.3.0. What does changeset version do to cli?
- Nothing
- Updates the range to
^2.0.0and releases cli as a patch - Gives cli a major release
- Fails
Why are changeset files named with random words?
- For security
- So parallel pull requests never create conflicting file names
- To make them hidden
- Git requires unique names