Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"changes": [
{
"comment": "Fix workspace package aliases using caret shorthand, such as \"workspace:@scope/pkg@^\".",
"type": "patch",
"packageName": "@microsoft/rush"
}
],
"packageName": "@microsoft/rush",
"email": "whoalin414@gmail.com"
}
36 changes: 31 additions & 5 deletions libraries/rush-lib/src/logic/DependencySpecifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { InternalError } from '@rushstack/node-core-library';
*/
const WORKSPACE_PREFIX_REGEX: RegExp = /^workspace:((?<alias>[^._/][^@]*)@)?(?<version>.*)$/;

// pnpm supports "workspace:^" and "workspace:~" as shorthand for the current project's version
// with the corresponding semver range operator. These strings are not complete npm semver ranges
// by themselves, so workspace alias targets using them cannot be validated by npm-package-arg.
const WORKSPACE_VERSION_SHORTHANDS: ReadonlySet<string> = new Set(['^', '~']);

/**
* match catalog protocol in dependencies value declaration in `package.json`
* example:
Expand Down Expand Up @@ -157,10 +162,20 @@ export class DependencySpecifier {
*/
public readonly aliasTarget: DependencySpecifier | undefined;

public constructor(packageName: string, versionSpecifier: string) {
public constructor(
packageName: string,
versionSpecifier: string,
options?: { isWorkspaceVersionShorthand?: boolean }
) {
this.packageName = packageName;
this.versionSpecifier = versionSpecifier;

if (options?.isWorkspaceVersionShorthand) {
this.specifierType = DependencySpecifierType.Range;
this.aliasTarget = undefined;
return;
}

// Catalog protocol is a feature from PNPM for centralized version management
const catalogSpecResult: CatalogSpec | undefined = CatalogSpec.tryParse(versionSpecifier);
if (catalogSpecResult) {
Expand All @@ -179,10 +194,7 @@ export class DependencySpecifier {

if (workspaceSpecResult.alias) {
// "workspace:some-package@^1.2.3" should be resolved as alias
this.aliasTarget = DependencySpecifier.parseWithCache(
workspaceSpecResult.alias,
workspaceSpecResult.version
);
this.aliasTarget = parseWorkspaceAliasTarget(workspaceSpecResult.alias, workspaceSpecResult.version);
} else {
this.aliasTarget = undefined;
}
Expand Down Expand Up @@ -253,3 +265,17 @@ export class DependencySpecifier {
}
}
}

function parseWorkspaceAliasTarget(alias: string, version: string): DependencySpecifier {
if (!WORKSPACE_VERSION_SHORTHANDS.has(version)) {
return DependencySpecifier.parseWithCache(alias, version);
}

const cacheKey: string = `${alias}\0${version}`;
let result: DependencySpecifier | undefined = dependencySpecifierParseCache.get(cacheKey);
if (!result) {
result = new DependencySpecifier(alias, version, { isWorkspaceVersionShorthand: true });
dependencySpecifierParseCache.set(cacheKey, result);
}
return result;
}
17 changes: 17 additions & 0 deletions libraries/rush-lib/src/logic/test/DependencySpecifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ DependencySpecifier {
"specifierType": "Workspace",
"versionSpecifier": "alias-target@*",
}
`);
});

it('correctly parses a "workspace:alias@^" version', () => {
const specifier = new DependencySpecifier('dep', 'workspace:@example/ui-core@^');
expect(specifier).toMatchInlineSnapshot(`
DependencySpecifier {
"aliasTarget": DependencySpecifier {
"aliasTarget": undefined,
"packageName": "@example/ui-core",
"specifierType": "Range",
"versionSpecifier": "^",
},
"packageName": "dep",
"specifierType": "Workspace",
"versionSpecifier": "@example/ui-core@^",
}
`);
});
});
Expand Down
Loading