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

[rush] Fix the issue: "rush add" should be able to add unpublished locally-linked projects #1612

Merged
merged 22 commits into from
Nov 21, 2019

Conversation

Claudiazhaoya
Copy link
Contributor

@Claudiazhaoya Claudiazhaoya commented Nov 1, 2019

This address the issue: #991

.vscode/launch.json Outdated Show resolved Hide resolved
apps/rush-lib/src/logic/PackageJsonUpdater.ts Outdated Show resolved Hide resolved
common/config/rush/pnpm-lock.yaml Outdated Show resolved Hide resolved
apps/rush-lib/src/logic/PackageJsonUpdater.ts Outdated Show resolved Hide resolved
const rushConfigProjects: RushConfigurationProject[] = this._rushConfiguration.projects;
rushConfigProjects.forEach(rushConfigProject => {
if(rushConfigProject.packageName === packageName) {
selectedVersion = rushConfigProject.packageJson.version;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should break out of the loop here.

We also probably want to handle the case where a version is specified on the command-line and it doesn't match the version in the package.json

We also need to handle the case where the dependency that we're adding is marked as a cyclic dependency for the project in rush.json.

@@ -355,18 +355,28 @@ export class PackageJsonUpdater {
}
console.log(`Querying NPM registry for latest version of "${packageName}"...`);

//if the project exists in the local
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//if the project exists in the local
// determine if the package is a project in the local repository

selectedVersion = rushConfigProject.packageJson.version;
}
});

let commandArgs: string[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code should go inside the below condition.

@iclanton iclanton changed the title Fix the issue: "rush add" should be able to add unpublished locally-linked projects [rush] Fix the issue: "rush add" should be able to add unpublished locally-linked projects Nov 2, 2019
// determine if the package is a project in the local repository and if the version exists
for (const rushConfigProject of rushConfigProjects) {
const rushConfigProjectVersion: string = rushConfigProject.packageJson.version;
if(rushConfigProject.packageName === packageName && rushConfigProject.cyclicDependencyProjects.size === 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cyclic dependency check should probably happen before the loop starts.

I think this logic is also backwards: we need to check if the project we're adding the dependency to has the dependency listed as a cyclic. We also need to see if the dependency is in the set of cyclics, not just if there are any cyclic dependencies listed.

for (const rushConfigProject of rushConfigProjects) {
const rushConfigProjectVersion: string = rushConfigProject.packageJson.version;
if(rushConfigProject.packageName === packageName && rushConfigProject.cyclicDependencyProjects.size === 0) {
if(initialSpec === rushConfigProjectVersion) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use semver.satisfies(...). If the initialspec is anything other than a single version (like ~1.2.3), this will always cause an exception to be thrown.

apps/rush-lib/src/logic/PackageJsonUpdater.ts Show resolved Hide resolved
@iclanton
Copy link
Member

iclanton commented Nov 2, 2019

Can you list out the scenarios you tested? There are a lot of them and we should be sure the behavior is expected.

} else {
commandArgs = ['view', `${packageName}@latest`, 'version'];
// determine if the package is a project in the local repository
for (const rushConfigProject of rushConfigProjects) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be duplicated above (very similar). Better to move to its own function

commandArgs = ['info', packageName, 'versions', '--json'];
} else {
commandArgs = ['view', packageName, 'versions', '--json'];
// determine if the package is a project in the local repository and if the version exists
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we check for local packages after we checked the npm registry ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we find the local package, we don't need to check the registry.

@@ -268,13 +269,15 @@ export class PackageJsonUpdater {
* Selects an appropriate version number for a particular package, given an optional initial SemVer spec.
* If ensureConsistentVersions, tries to pick a version that will be consistent.
* Otherwise, will choose the latest semver matching the initialSpec and append the proper range style.
* @param projects - the project which will have its package.json updated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param projects - the project which will have its package.json updated
* @param projects - the projects which will have their `package.json`s updated

commandArgs = ['info', packageName, 'versions', '--json'];
} else {
commandArgs = ['view', packageName, 'versions', '--json'];
// determine if the package is a project in the local repository and if the version exists
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we find the local package, we don't need to check the registry.

// determine if the package is a project in the local repository and if the version exists
for (const rushConfigProject of rushConfigProjects) {
const rushConfigProjectVersion: string = rushConfigProject.packageJson.version;
if(rushConfigProject.packageName === packageName && !projects[0].cyclicDependencyProjects.has(packageName)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't just want to check the first project in the projects array. It should probably check all of them.

* @param packageName - the name of the package to be used
* @param initialSpec - a semver pattern that should be used to find the latest version matching the spec
* @param implicitlyPinnedVersion - the implicitly preferred (aka common/primary) version of the package in use
* @param rangeStyle - if this version is selected by querying registry, then this range specifier is prepended to
* the selected version.
*/
private async _getNormalizedVersionSpec(
projects: RushConfigurationProject[],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we support multiple projects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.. That's why I'm curious about why it has an array here. The statement is like this: projects = [currentProject]; Don't you think we actually don't need this array here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is used when the --all flag is specified.

selectedVersion = initialSpec;
break;
} else {
throw new Error(`Unable to find a version of "${packageName}" that satisfies`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be a little more elaborate with this error:

Suggested change
throw new Error(`Unable to find a version of "${packageName}" that satisfies`
throw new Error(`The dependency being added ("${packageName}") is a project in the local Rush repository, but the version specifier provided (${initialSpec}) does not match the local project version (${rushConfigProjectVersion}). Correct the version specifier, omit a version specifier, or include "${packageName}" as a cyclicDependencyProject in rush.json for ${<project-being-added-to>} if it is intended for "${packageName}" to come from an external feed and not from the local Rush repository.`);

// determine if the package is a project in the local repository
for (const rushConfigProject of rushConfigProjects) {
if(rushConfigProject.packageName === packageName && !projects[0].cyclicDependencyProjects.has(packageName)) {
if(selectedVersion === undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should go outside of the loop.

/**
* Check if the package is the cyclic dependency in the project dependencies in package.json
*/
public isCyclicDependency(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make these private to begin with.


/**
* Check if the project is local project
* If it is found in rush.json and is not a cyclic dependency, return true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function returns a version specifier, not true or false.

* @param projects - the projects which will have their package.json's updated
* @param rushConfigProjects - the projects which are in the rush.json
*/
public isLocalProject(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better name for this would be getVersionForLocalDependency.

} else {
commandArgs = ['view', packageName, 'versions', '--json'];
throw new Error(`The dependency being added ("${packageName}") is a project in the local Rush repository,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split this onto multiple lines by concatenating strings. The way this is written, there will be line breaks and indentation in the error message.

} else {
commandArgs = ['view', packageName, 'versions', '--json'];
throw new Error(`The dependency being added ("${packageName}") is a project in the local Rush repository,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will throw if the dependency being added isn't a local dependency. In that case, we want to just query the feed.

@iclanton iclanton merged commit 0c5c8d9 into master Nov 21, 2019
@iclanton iclanton deleted the zhas/rush-add-test branch November 21, 2019 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants