Skip to content

Commit

Permalink
fix: should not replace workspace:* when update (#5764)
Browse files Browse the repository at this point in the history
close #5317
close #5303
close #5312
close #5726
  • Loading branch information
await-ovo committed Dec 9, 2022
1 parent 28a99f4 commit 49f6c91
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .changeset/old-islands-perform.md
@@ -0,0 +1,8 @@
---
"@pnpm/core": patch
"@pnpm/plugin-commands-installation": patch
"@pnpm/resolve-dependencies": patch
"pnpm": patch
---

`pnpm update` should not replace `workspace:*`, `workspace:~`, and `workspace:^` with `workspace:<version>` [#5764](https://github.com/pnpm/pnpm/pull/5764).
2 changes: 1 addition & 1 deletion pkg-manager/core/src/install/index.ts
Expand Up @@ -538,7 +538,7 @@ export async function mutateModules (
projectsToInstall.push({
pruneDirectDependencies: false,
...project,
wantedDependencies: wantedDeps.map(wantedDep => ({ ...wantedDep, isNew: true, updateSpec: true, nodeExecPath: opts.nodeExecPath })),
wantedDependencies: wantedDeps.map(wantedDep => ({ ...wantedDep, isNew: !currentPrefs[wantedDep.alias], updateSpec: true, nodeExecPath: opts.nodeExecPath })),
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg-manager/core/src/parseWantedDependencies.ts
Expand Up @@ -41,7 +41,7 @@ export function parseWantedDependencies (
dev: Boolean(opts.dev || alias && !!opts.devDependencies[alias]),
optional: Boolean(opts.optional || alias && !!opts.optionalDependencies[alias]),
pinnedVersion,
raw: rawWantedDependency,
raw: alias && opts.currentPrefs?.[alias]?.startsWith('workspace:') ? `${alias}@${opts.currentPrefs[alias]}` : rawWantedDependency,
}
if (pref) {
return {
Expand Down
26 changes: 25 additions & 1 deletion pkg-manager/core/test/install/multipleImporters.ts
Expand Up @@ -946,6 +946,8 @@ test('update workspace range', async () => {
dep4: 'workspace:1',
dep5: 'workspace:1.0',
dep6: 'workspace:*',
dep7: 'workspace:^',
dep8: 'workspace:~',
},
},
rootDir: path.resolve('project-1'),
Expand All @@ -963,6 +965,8 @@ test('update workspace range', async () => {
dep4: 'workspace:1',
dep5: 'workspace:1.0',
dep6: 'workspace:*',
dep7: 'workspace:^',
dep8: 'workspace:~',
},
},
rootDir: path.resolve('project-2'),
Expand Down Expand Up @@ -1024,6 +1028,24 @@ test('update workspace range', async () => {
},
},
},
dep7: {
'2.0.0': {
dir: '',
manifest: {
name: 'dep7',
version: '2.0.0',
},
},
},
dep8: {
'2.0.0': {
dir: '',
manifest: {
name: 'dep8',
version: '2.0.0',
},
},
},
},
}))

Expand All @@ -1033,7 +1055,9 @@ test('update workspace range', async () => {
dep3: 'workspace:^2.0.0',
dep4: 'workspace:^2.0.0',
dep5: 'workspace:~2.0.0',
dep6: 'workspace:2.0.0',
dep6: 'workspace:*',
dep7: 'workspace:^',
dep8: 'workspace:~',
}
expect(updatedImporters[0].manifest.dependencies).toStrictEqual(expected)
expect(updatedImporters[1].manifest.dependencies).toStrictEqual(expected)
Expand Down
Expand Up @@ -27,7 +27,7 @@ export function createWorkspaceSpecs (specs: string[], workspacePackages: Worksp
const parsed = parseWantedDependency(spec)
if (!parsed.alias) throw new PnpmError('NO_PKG_NAME_IN_SPEC', `Cannot update/install from workspace through "${spec}"`)
if (!workspacePackages[parsed.alias]) throw new PnpmError('WORKSPACE_PACKAGE_NOT_FOUND', `"${parsed.alias}" not found in the workspace`)
if (!parsed.pref) return `${parsed.alias}@workspace:*`
if (!parsed.pref) return `${parsed.alias}@workspace:>=0.0.0`
if (parsed.pref.startsWith('workspace:')) return spec
return `${parsed.alias}@workspace:${parsed.pref}`
})
Expand Down
Expand Up @@ -72,7 +72,7 @@ test('updateToWorkspacePackagesFromManifest()', () => {
})

test('createWorkspaceSpecs', () => {
expect(createWorkspaceSpecs(['bar', 'foo@2', 'qar@workspace:3'], WORKSPACE_PACKAGES)).toStrictEqual(['bar@workspace:*', 'foo@workspace:2', 'qar@workspace:3'])
expect(createWorkspaceSpecs(['bar', 'foo@2', 'qar@workspace:3'], WORKSPACE_PACKAGES)).toStrictEqual(['bar@workspace:>=0.0.0', 'foo@workspace:2', 'qar@workspace:3'])
let err!: PnpmError
try {
createWorkspaceSpecs(['express'], WORKSPACE_PACKAGES)
Expand Down
Expand Up @@ -83,7 +83,7 @@ function getWantedDependenciesFromGivenSet (
nodeExecPath: opts.nodeExecPath ?? opts.dependenciesMeta[alias]?.node,
pinnedVersion: whichVersionIsPinned(pref),
pref: updatedPref,
raw: `${alias}@${updatedPref}`,
raw: `${alias}@${pref}`,
}
})
}
19 changes: 15 additions & 4 deletions pkg-manager/resolve-dependencies/src/updateProjectManifest.ts
Expand Up @@ -117,7 +117,7 @@ function resolvedDirectDepToSpecObject (
nodeExecPath: opts.nodeExecPath,
peer: importer['peer'],
pref,
saveType: (isNew === true) ? importer['targetDependenciesField'] : undefined,
saveType: importer['targetDependenciesField'],
}
}

Expand Down Expand Up @@ -159,12 +159,23 @@ function getPrefPreferSpecifiedExoticSpec (
}
) {
const prefix = getPrefix(opts.alias, opts.name)
if (opts.specRaw?.startsWith(`${opts.alias}@${prefix}`) && opts.specRaw !== `${opts.alias}@workspace:*`) {
const specWithoutName = opts.specRaw.slice(`${opts.alias}@${prefix}`.length)
if (opts.specRaw?.startsWith(`${opts.alias}@${prefix}`)) {
let specWithoutName = opts.specRaw.slice(`${opts.alias}@${prefix}`.length)
if (specWithoutName.startsWith('workspace:')) {
specWithoutName = specWithoutName.slice(10)
if (specWithoutName === '*' || specWithoutName === '^' || specWithoutName === '~') {
return specWithoutName
}
}
const selector = versionSelectorType(specWithoutName)
if (!((selector != null) && (selector.type === 'version' || selector.type === 'range'))) {
if (!selector) {
return opts.specRaw.slice(opts.alias.length + 1)
}
}
// A prerelease version is always added as an exact version
if (semver.parse(opts.version)?.prerelease.length) {
return `${prefix}${opts.version}`
}

return `${prefix}${createVersionSpec(opts.version, { pinnedVersion: opts.pinnedVersion, rolling: opts.rolling })}`
}

0 comments on commit 49f6c91

Please sign in to comment.