Skip to content

Commit

Permalink
feat: update projects when catalogs config changes
Browse files Browse the repository at this point in the history
  • Loading branch information
gluxon committed Apr 26, 2024
1 parent 7c36470 commit 0d26ded
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg-manager/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@pnpm/build-modules": "workspace:*",
"@pnpm/builder.policy": "3.0.0",
"@pnpm/calc-dep-state": "workspace:*",
"@pnpm/catalogs.protocol-parser": "workspace:*",
"@pnpm/catalogs.types": "workspace:*",
"@pnpm/constants": "workspace:*",
"@pnpm/core-loggers": "workspace:*",
"@pnpm/crypto.base32-hash": "workspace:*",
Expand Down
11 changes: 11 additions & 0 deletions pkg-manager/core/src/install/allCatalogsAreUpToDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type CatalogSnapshots } from '@pnpm/lockfile-file'
import { type Catalogs } from '@pnpm/catalogs.types'

export function allCatalogsAreUpToDate (
catalogsConfig: Catalogs,
snapshot: CatalogSnapshots | undefined
): boolean {
return Object.entries(snapshot ?? {})
.every(([catalogName, catalog]) => Object.entries(catalog ?? {})
.every(([alias, entry]) => entry.specifier === catalogsConfig[catalogName]?.[alias]))
}
25 changes: 23 additions & 2 deletions pkg-manager/core/src/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import crypto from 'crypto'
import path from 'path'
import { buildModules, type DepsStateCache, linkBinsOfDependencies } from '@pnpm/build-modules'
import { createAllowBuildFunction } from '@pnpm/builder.policy'
import { parseCatalogProtocol } from '@pnpm/catalogs.protocol-parser'
import {
LAYOUT_VERSION,
LOCKFILE_VERSION,
Expand Down Expand Up @@ -85,6 +86,7 @@ import {
} from './extendInstallOptions'
import { linkPackages } from './link'
import { reportPeerDependencyIssues } from './reportPeerDependencyIssues'
import { allCatalogsAreUpToDate } from './allCatalogsAreUpToDate'

class LockfileConfigMismatchError extends PnpmError {
constructor (outdatedLockfileSettingName: string) {
Expand Down Expand Up @@ -389,6 +391,7 @@ export async function mutateModules (
ctx.wantedLockfile.lockfileVersion === LOCKFILE_VERSION_V6 ||
ctx.wantedLockfile.lockfileVersion === '6.1'
) &&
allCatalogsAreUpToDate(ctx.catalogs, ctx.wantedLockfile.catalogs) &&
await allProjectsAreUpToDate(Object.values(ctx.projects), {
autoInstallPeers: opts.autoInstallPeers,
excludeLinksFromLockfile: opts.excludeLinksFromLockfile,
Expand Down Expand Up @@ -624,8 +627,26 @@ Note that in CI environments, this setting is enabled by default.`,
}
/* eslint-enable no-await-in-loop */

function isWantedDepPrefSame (_alias: string, prevPref: string | undefined, nextPref: string): boolean {
return prevPref !== nextPref
function isWantedDepPrefSame (alias: string, prevPref: string | undefined, nextPref: string): boolean {
if (prevPref !== nextPref) {
return false
}

// When pnpm catalogs are used, the specifiers can be the same (e.g.
// "catalog:default"), but the wanted versions for the dependency can be
// different after resolution if the catalog config was just edited.
const catalogName = parseCatalogProtocol(prevPref)

// If there's no catalog name, the catalog protocol was not used and we
// can assume the pref is the same since prevPref and nextPref match.
if (catalogName === null) {
return true
}

const prevCatalogEntrySpec = ctx.wantedLockfile.catalogs?.[catalogName]?.[alias]?.specifier
const nextCatalogEntrySpec = ctx.catalogs[catalogName]?.[alias]

return prevCatalogEntrySpec === nextCatalogEntrySpec
}

async function installCase (project: any) { // eslint-disable-line
Expand Down
6 changes: 6 additions & 0 deletions pkg-manager/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
{
"path": "../../__utils__/test-ipc-server"
},
{
"path": "../../catalogs/protocol-parser"
},
{
"path": "../../catalogs/types"
},
{
"path": "../../config/matcher"
},
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0d26ded

Please sign in to comment.