Skip to content

[rush] Rush treats npm alias as link when subspace is enabled #5212

Open
@fzxen

Description

@fzxen

Summary

Rush treats npm alias as link when subspace is enabled.

Repro steps

This repository can reproduce this issue:https://github.com/fzxen/rushstack_reproduce

My project has two subspaces, one is default subspace, and the other is named test-library.

There is a package existing under the default space, which uses npm alias to declare its dependencies.

{
  "devDependencies": {
      "@testing-library/dom": "workspace:*",
      "@testing-library/dom-npm": "npm:@testing-library/dom@^10.4.0"
    }
}

@testing-library/dom is a package in test-library subspace. I expect @testing-library/dom-npm can be downloaded from npm instead of directly creating a local symbol link.

Expected result:

pnpm will download @testing-library/dom from npm.

  ../../../projects/project1:
    devDependencies:
      '@testing-library/dom':
        specifier: link:../test-library-dom
        version: link:../test-library-dom
      '@testing-library/dom-npm':
        specifier: npm:@testing-library/dom@^10.4.0
        version: 10.4.0

Actual result:

pnpm directly links to a package named @testing-library/dom in another sub-space.
I also tried adding @testing-library/dom-npm to decoupledLocalDependencies, but got the same result.

  ../../../projects/project1:
    devDependencies:
      '@testing-library/dom':
        specifier: link:../test-library-dom
        version: link:../test-library-dom
      '@testing-library/dom-npm':
        specifier: link:../test-library-dom
        version: link:../test-library-dom

Details

I located the code causing this problem.

} else if (currentVersion.startsWith('npm:')) {
// Case 3. "<alias>": "npm:<package_name>@<dep_version>"
const packageSpec: string = currentVersion.slice('npm:'.length);
const nameEndsAt: number =
packageSpec[0] === '@' ? packageSpec.slice(1).indexOf('@') + 1 : packageSpec.indexOf('@');
const aliasedPackageName: string = nameEndsAt > 0 ? packageSpec.slice(0, nameEndsAt) : packageSpec;
// const depVersion: string = nameEndsAt > 0 ? packageSpec.slice(nameEndsAt + 1) : '';
const aliasedWorkspaceProjectInfo: IWorkspaceProjectInfo | undefined =
settings.workspaceProjects[aliasedPackageName];
if (aliasedWorkspaceProjectInfo) {
const relativePath: string = path.normalize(
path.relative(
workspaceProject.projectRelativeFolder,
aliasedWorkspaceProjectInfo.projectRelativeFolder
)
);
const newVersion: string = 'link:' + relativePath;
dependencies[dependencyName] = newVersion;
}
}
}
}

The rush pnpmfile script will resolve npm aliases. As long as there is a dependency with the same name in the workspace, it will directly change to link:.

I think that the original specifier should be maintained and should not be changed to link: When this dependency is added to decoupledLocalDependencies.

Standard questions

Please answer these questions to help us investigate your issue more quickly:

Question Answer
@microsoft/rush globally installed version? 5.153.1
rushVersion from rush.json? 5.153.1
useWorkspaces from rush.json? Yes
Operating system? Mac
Would you consider contributing a PR? Yes
Node.js version (node -v)? 20.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Low priority

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions