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

fix: recursive update with workspace alias #7999

Merged
merged 15 commits into from
Apr 29, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { type Lockfile } from '@pnpm/lockfile-types'
import { readModulesManifest } from '@pnpm/modules-yaml'
import { install, update } from '@pnpm/plugin-commands-installation'
import { preparePackages } from '@pnpm/prepare'
import { readProjectManifestOnly } from '@pnpm/read-project-manifest'
import { addDistTag } from '@pnpm/registry-mock'
import { sync as readYamlFile } from 'read-yaml-file'
import { DEFAULT_OPTS } from '../utils'
Expand Down Expand Up @@ -415,3 +416,33 @@ test('recursive update in workspace should not add new dependencies', async () =
projects['project-1'].hasNot('is-positive')
projects['project-2'].hasNot('is-positive')
})

test('recursive update with aliased workspace dependency (#7975)', async () => {
const projects = preparePackages([
{
name: 'project-1',
version: '1.0.0',
dependencies: {
pkg: 'workspace:project-2@^',
},
},
{
name: 'project-2',
version: '1.0.0',
},
])

await update.handler({
...DEFAULT_OPTS,
...await readProjects(process.cwd(), []),
depth: 0,
dir: process.cwd(),
recursive: true,
workspaceDir: process.cwd(),
})

projects['project-1'].has('pkg')

const manifest = await readProjectManifestOnly('project-1')
expect(manifest).toHaveProperty(['dependencies', 'pkg'], 'workspace:npm:project-2@^') // TODO: how to remove the `npm:` part?
KSXGitHub marked this conversation as resolved.
Show resolved Hide resolved
})
1 change: 1 addition & 0 deletions pkg-manager/resolve-dependencies/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@pnpm/store-controller-types": "workspace:*",
"@pnpm/types": "workspace:*",
"@pnpm/which-version-is-pinned": "workspace:*",
"@pnpm/workspace-pref": "workspace:*",
"@yarnpkg/core": "4.0.3",
"filenamify": "^4.3.0",
"get-npm-tarball-url": "^2.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type ProjectManifest,
} from '@pnpm/types'
import { whichVersionIsPinned } from '@pnpm/which-version-is-pinned'
import { parseWorkspacePref } from '@pnpm/workspace-pref'

export type PinnedVersion = 'major' | 'minor' | 'patch' | 'none'

Expand Down Expand Up @@ -54,7 +55,12 @@ export function getWantedDependencies (
}

function updateWorkspacePref (pref: string): string {
return pref.startsWith('workspace:') ? 'workspace:*' : pref
const parseResult = parseWorkspacePref(pref)
if (!parseResult) {
throw new Error(`Invalid workspace pref: ${pref}`)
}
const { alias } = parseResult
return alias ? `workspace:${alias}@*` : 'workspace:*'
}

function getWantedDependenciesFromGivenSet (
Expand Down
3 changes: 3 additions & 0 deletions pkg-manager/resolve-dependencies/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@
},
{
"path": "../../store/store-controller-types"
},
{
"path": "../../workspace/workspace-pref"
}
],
"composite": true
Expand Down
26 changes: 15 additions & 11 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions resolving/npm-resolver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@pnpm/resolve-workspace-range": "workspace:*",
"@pnpm/resolver-base": "workspace:*",
"@pnpm/types": "workspace:*",
"@pnpm/workspace-pref": "workspace:*",
"@zkochan/retry": "^0.2.0",
"encode-registry": "^3.0.1",
"load-json-file": "^6.2.0",
Expand Down
21 changes: 9 additions & 12 deletions resolving/npm-resolver/src/workspacePrefToNpm.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
export function workspacePrefToNpm (workspacePref: string): string {
const prefParts = /^workspace:([^._/][^@]*@)?(.*)$/.exec(workspacePref)
import { parseWorkspacePref } from '@pnpm/workspace-pref'

if (prefParts == null) {
export function workspacePrefToNpm (workspacePref: string): string {
const parseResult = parseWorkspacePref(workspacePref)
if (parseResult == null) {
throw new Error(`Invalid workspace spec: ${workspacePref}`)
}
const [workspacePkgAlias, workspaceVersion] = prefParts.slice(1)

const pkgAliasPart = workspacePkgAlias != null && workspacePkgAlias
? `npm:${workspacePkgAlias}`
: ''
const versionPart = workspaceVersion === '^' || workspaceVersion === '~'
? '*'
: workspaceVersion

return `${pkgAliasPart}${versionPart}`
const { alias, version } = parseResult
const versionPart = version === '^' || version === '~' ? '*' : version
return alias
? `npm:${alias}@${versionPart}`
: versionPart
}
3 changes: 3 additions & 0 deletions resolving/npm-resolver/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
{
"path": "../../workspace/resolve-workspace-range"
},
{
"path": "../../workspace/workspace-pref"
},
{
"path": "../resolver-base"
}
Expand Down
15 changes: 15 additions & 0 deletions workspace/workspace-pref/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @pnpm/workspace-pref

> Parse and stringify workspace pref

[![npm version](https://img.shields.io/npm/v/@pnpm/workspace-pref.svg)](https://www.npmjs.com/package/@pnpm/workspace-pref)

## Installation

```sh
pnpm add @pnpm/workspace-pref
```

## License

MIT
37 changes: 37 additions & 0 deletions workspace/workspace-pref/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@pnpm/workspace-pref",
KSXGitHub marked this conversation as resolved.
Show resolved Hide resolved
"version": "6.0.0",
"description": "Parse and stringify workspace pref",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib",
"!*.map"
],
"engines": {
"node": ">=18.12"
},
"scripts": {
"lint": "eslint \"src/**/*.ts\"",
"test": "pnpm run compile",
"prepublishOnly": "pnpm run compile",
"compile": "tsc --build && pnpm run lint --fix"
},
"repository": "https://github.com/pnpm/pnpm/blob/main/workspace/workspace-pref",
"keywords": [
"pnpm9",
"pnpm"
],
"license": "MIT",
"bugs": {
"url": "https://github.com/pnpm/pnpm/issues"
},
"homepage": "https://github.com/pnpm/pnpm/blob/main/workspace/workspace-pref#readme",
"funding": "https://opencollective.com/pnpm",
"devDependencies": {
"@pnpm/workspace-pref": "workspace:*"
},
"exports": {
".": "./lib/index.js"
}
}
12 changes: 12 additions & 0 deletions workspace/workspace-pref/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const WORKSPACE_PREF_REGEX = /^workspace:((?<alias>[^._/][^@]*)@)?(?<version>.*)$/

export interface ParsedWorkspacePref {
alias?: string
version: string
}

export function parseWorkspacePref (pref: string): ParsedWorkspacePref | null {
const parts = WORKSPACE_PREF_REGEX.exec(pref)
if (parts === null) return null
return parts.groups! as unknown as ParsedWorkspacePref
}
13 changes: 13 additions & 0 deletions workspace/workspace-pref/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "@pnpm/tsconfig",
"compilerOptions": {
"outDir": "lib",
"rootDir": "src"
},
"include": [
"src/**/*.ts",
"../../__typings__/**/*.d.ts"
],
"references": [],
"composite": true
}
8 changes: 8 additions & 0 deletions workspace/workspace-pref/tsconfig.lint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"test/**/*.ts",
"../../__typings__/**/*.d.ts"
]
}