-
Notifications
You must be signed in to change notification settings - Fork 609
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
Changes from 12 commits
64ceb69
52f1136
58c5904
8965326
f7a1739
85c9c10
90cfe98
85c986d
dc32432
186ba48
c744202
62d6f1f
8964b1f
d7300a1
f7cc78d
1d4893d
ea770dd
0be2e56
b20d97e
27fae63
186a28e
cd47ff6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,6 +151,7 @@ export class PackageJsonUpdater { | |
); | ||
|
||
const version: string = await this._getNormalizedVersionSpec( | ||
projects, | ||
installManager, | ||
packageName, | ||
initialVersion, | ||
|
@@ -264,17 +265,65 @@ export class PackageJsonUpdater { | |
project.addOrUpdateDependency(packageName, newVersion, dependencyType!); | ||
} | ||
|
||
/** | ||
* Check if the package is the cyclic dependency in the project dependencies in package.json | ||
*/ | ||
public isCyclicDependency( | ||
packageName: string, | ||
projects: RushConfigurationProject[] | ||
): boolean { | ||
for (const project of projects) { | ||
if(project.cyclicDependencyProjects.has(packageName)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Check if the project is local project | ||
* If it is found in rush.json and is not a cyclic dependency, return true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function returns a version specifier, not true or false. |
||
* Otherwise, return false | ||
* @param initialSpec - a semver pattern that should be used to find the latest version matching the spec | ||
* @param packageName - the name of the package to be used | ||
* @param projects - the projects which will have their package.json's updated | ||
* @param rushConfigProjects - the projects which are in the rush.json | ||
*/ | ||
public isLocalProject( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better name for this would be |
||
initialSpec: string | undefined, | ||
packageName: string, | ||
projects: RushConfigurationProject[], | ||
rushConfigProjects: RushConfigurationProject[], | ||
): string | undefined { | ||
for (const rushConfigProject of rushConfigProjects) { | ||
const rushConfigProjectVersion: string = rushConfigProject.packageJson.version; | ||
const isCyclicDependency: boolean = this.isCyclicDependency(packageName, projects); | ||
if(rushConfigProject.packageName === packageName && (!isCyclicDependency)) { | ||
if(initialSpec !== undefined) { | ||
if(semver.satisfies(rushConfigProjectVersion, initialSpec)) { | ||
return initialSpec; | ||
} | ||
} else { | ||
return rushConfigProjectVersion; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
/** | ||
* 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 projects which will have their package.json's updated | ||
* @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[], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we support multiple projects? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is used when the |
||
installManager: InstallManager, | ||
packageName: string, | ||
initialSpec: string | undefined, | ||
|
@@ -306,67 +355,91 @@ export class PackageJsonUpdater { | |
|
||
await installManager.ensureLocalPackageManager(); | ||
let selectedVersion: string | undefined; | ||
const rushConfigProjects: RushConfigurationProject[] = this._rushConfiguration.projects; | ||
|
||
if (initialSpec && initialSpec !== 'latest') { | ||
iclanton marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log(colors.gray('Finding versions that satisfy the selector: ') + initialSpec); | ||
console.log(); | ||
console.log(`Querying registry for all versions of "${packageName}"...`); | ||
|
||
let commandArgs: string[]; | ||
if (this._rushConfiguration.packageManager === 'yarn') { | ||
commandArgs = ['info', packageName, 'versions', '--json']; | ||
// determine if the package is a project in the local repository and if the version exists | ||
const version: string | undefined = this.isLocalProject(initialSpec, packageName, projects, rushConfigProjects); | ||
if(version !== undefined) { | ||
selectedVersion = version | ||
} else { | ||
commandArgs = ['view', packageName, 'versions', '--json']; | ||
throw new Error(`The dependency being added ("${packageName}") is a project in the local Rush repository, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
but the version specifier provided (${initialSpec}) does not match the local project version (${version}). | ||
Correct the version specifier, omit a version specifier, or include "${packageName}" as a cyclicDependencyProject in rush.json | ||
if it is intended for "${packageName}" | ||
to come from an external feed and not from the local Rush repository."`); | ||
} | ||
|
||
const allVersions: string = | ||
Utilities.executeCommandAndCaptureOutput( | ||
this._rushConfiguration.packageManagerToolFilename, | ||
commandArgs, | ||
this._rushConfiguration.commonTempFolder | ||
); | ||
if(selectedVersion === undefined) { | ||
console.log(`Querying registry for all versions of "${packageName}"...`); | ||
|
||
let versionList: string[]; | ||
if (this._rushConfiguration.packageManager === 'yarn') { | ||
versionList = JSON.parse(allVersions).data; | ||
} else { | ||
versionList = JSON.parse(allVersions); | ||
} | ||
let commandArgs: string[]; | ||
if (this._rushConfiguration.packageManager === 'yarn') { | ||
commandArgs = ['info', packageName, 'versions', '--json']; | ||
} else { | ||
commandArgs = ['view', packageName, 'versions', '--json']; | ||
} | ||
|
||
console.log(colors.gray(`Found ${versionList.length} available versions.`)); | ||
const allVersions: string = | ||
Utilities.executeCommandAndCaptureOutput( | ||
this._rushConfiguration.packageManagerToolFilename, | ||
commandArgs, | ||
this._rushConfiguration.commonTempFolder | ||
); | ||
|
||
let versionList: string[]; | ||
if (this._rushConfiguration.packageManager === 'yarn') { | ||
versionList = JSON.parse(allVersions).data; | ||
} else { | ||
versionList = JSON.parse(allVersions); | ||
} | ||
|
||
for (const version of versionList) { | ||
if (semver.satisfies(version, initialSpec)) { | ||
selectedVersion = initialSpec; | ||
console.log(`Found a version that satisfies ${initialSpec}: ${colors.cyan(version)}`); | ||
break; | ||
console.log(colors.gray(`Found ${versionList.length} available versions.`)); | ||
|
||
for (const version of versionList) { | ||
if (semver.satisfies(version, initialSpec)) { | ||
selectedVersion = initialSpec; | ||
console.log(`Found a version that satisfies ${initialSpec}: ${colors.cyan(version)}`); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (!selectedVersion) { | ||
throw new Error(`Unable to find a version of "${packageName}" that satisfies` | ||
+ ` the version specifier "${initialSpec}"`); | ||
if (!selectedVersion) { | ||
throw new Error(`Unable to find a version of "${packageName}" that satisfies` | ||
+ ` the version specifier "${initialSpec}"`); | ||
} | ||
} | ||
} else { | ||
if (!this._rushConfiguration.ensureConsistentVersions) { | ||
console.log(colors.gray(`The "ensureConsistentVersions" policy is NOT active,` | ||
+ ` so we will assign the latest version.`)); | ||
console.log(); | ||
} | ||
console.log(`Querying NPM registry for latest version of "${packageName}"...`); | ||
|
||
let commandArgs: string[]; | ||
if (this._rushConfiguration.packageManager === 'yarn') { | ||
commandArgs = ['info', packageName, 'dist-tags.latest', '--silent']; | ||
} else { | ||
commandArgs = ['view', `${packageName}@latest`, 'version']; | ||
const version: string | undefined = this.isLocalProject(initialSpec, packageName, projects, rushConfigProjects); | ||
if(version !== undefined) { | ||
selectedVersion = version | ||
} | ||
|
||
selectedVersion = Utilities.executeCommandAndCaptureOutput( | ||
this._rushConfiguration.packageManagerToolFilename, | ||
commandArgs, | ||
this._rushConfiguration.commonTempFolder | ||
).trim(); | ||
if (selectedVersion === undefined) { | ||
console.log(`Querying NPM registry for latest version of "${packageName}"...`); | ||
|
||
let commandArgs: string[]; | ||
if (this._rushConfiguration.packageManager === 'yarn') { | ||
commandArgs = ['info', packageName, 'dist-tags.latest', '--silent']; | ||
} else { | ||
commandArgs = ['view', `${packageName}@latest`, 'version']; | ||
} | ||
|
||
selectedVersion = Utilities.executeCommandAndCaptureOutput( | ||
this._rushConfiguration.packageManagerToolFilename, | ||
commandArgs, | ||
this._rushConfiguration.commonTempFolder | ||
).trim(); | ||
} | ||
|
||
console.log(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@microsoft/rush", | ||
"comment": "Fix an issue with Rush add, where Rush was unable to add unpublished local projects as dependencies.", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@microsoft/rush", | ||
"email": "[email protected]" | ||
} |
There was a problem hiding this comment.
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.