@@ -111,7 +111,8 @@ export class InstallManager {
111111 private _commonTempFolderRecycler : AsyncRecycler ;
112112
113113 /**
114- * Returns a map of all direct dependencies that only have a single semantic version specifier
114+ * Returns a map of all direct dependencies that only have a single semantic version specifier.
115+ * Returns a map: dependency name --> version specifier
115116 */
116117 public static collectImplicitlyPreferredVersions ( rushConfiguration : RushConfiguration ) : Map < string , string > {
117118 // First, collect all the direct dependencies of all local projects, and their versions:
@@ -381,6 +382,7 @@ export class InstallManager {
381382 shrinkwrapIsUpToDate = false ;
382383 }
383384
385+ // dependency name --> version specifier
384386 const allExplicitPreferredVersions : Map < string , string > = this . _rushConfiguration . commonVersions
385387 . getAllPreferredVersions ( ) ;
386388
@@ -429,6 +431,8 @@ export class InstallManager {
429431 // Find the implicitly preferred versions
430432 // These are any first-level dependencies for which we only consume a single version range
431433 // (e.g. every package that depends on react uses an identical specifier)
434+
435+ // dependency name --> version specifier
432436 const allPreferredVersions : Map < string , string > =
433437 InstallManager . collectImplicitlyPreferredVersions ( this . _rushConfiguration ) ;
434438
@@ -469,65 +473,69 @@ export class InstallManager {
469473 dependencies : { }
470474 } ;
471475
472- // Collect pairs of (packageName, packageVersion) to be added as temp package dependencies
473- const pairs : { packageName : string , packageVersion : string } [ ] = [ ] ;
476+ // Collect pairs of (packageName, packageVersion) to be added as dependencies of the @rush- temp package.json
477+ const tempDependencies : Map < string , string > = new Map < string , string > ( ) ;
474478
479+ // These can be regular, optional, or peer dependencies (but NOT dev dependencies).
480+ // (A given packageName will never appear more than once in this list.)
475481 for ( const dependency of packageJson . dependencyList ) {
476482
477- // If there are any optional dependencies, copy them over directly
483+ // If there are any optional dependencies, copy them directly into the optionalDependencies field.
478484 if ( dependency . dependencyType === DependencyType . Optional ) {
479485 if ( ! tempPackageJson . optionalDependencies ) {
480486 tempPackageJson . optionalDependencies = { } ;
481487 }
482488 tempPackageJson . optionalDependencies [ dependency . name ] = dependency . version ;
483489 } else {
484- pairs . push ( { packageName : dependency . name , packageVersion : dependency . version } ) ;
490+ tempDependencies . set ( dependency . name , dependency . version ) ;
485491 }
486492 }
487493
488494 for ( const dependency of packageJson . devDependencyList ) {
489- // If there are devDependencies, we need to merge them with the regular
490- // dependencies. If the same library appears in both places, then the
491- // regular dependency takes precedence over the devDependency.
492- // It also takes precedence over a duplicate in optionalDependencies,
493- // but NPM will take care of that for us. (Frankly any kind of duplicate
494- // should be an error, but NPM is pretty lax about this.)
495- pairs . push ( { packageName : dependency . name , packageVersion : dependency . version } ) ;
495+ // If there are devDependencies, we need to merge them with the regular dependencies. If the same
496+ // library appears in both places, then the dev dependency wins (because presumably it's saying what you
497+ // want right now for development, not the range that you support for consumers).
498+ tempDependencies . set ( dependency . name , dependency . version ) ;
496499 }
500+ const sortedTempDependencyNames : string [ ] = [ ... tempDependencies . keys ( ) ] ;
501+ sortedTempDependencyNames . sort ( ) ; // alphabetize them to minimize churn in the tarball
502+
503+ for ( const packageName of sortedTempDependencyNames ) {
504+ const packageVersion : string = tempDependencies . get ( packageName ) ! ;
497505
498- for ( const pair of pairs ) {
499506 // Is there a locally built Rush project that could satisfy this dependency?
500507 // If so, then we will symlink to the project folder rather than to common/temp/node_modules.
501508 // In this case, we don't want "npm install" to process this package, but we do need
502509 // to record this decision for "rush link" later, so we add it to a special 'rushDependencies' field.
503510 const localProject : RushConfigurationProject | undefined =
504- this . _rushConfiguration . getProjectByName ( pair . packageName ) ;
505- if ( localProject ) {
511+ this . _rushConfiguration . getProjectByName ( packageName ) ;
506512
513+ if ( localProject ) {
507514 // Don't locally link if it's listed in the cyclicDependencyProjects
508- if ( ! rushProject . cyclicDependencyProjects . has ( pair . packageName ) ) {
515+ if ( ! rushProject . cyclicDependencyProjects . has ( packageName ) ) {
509516
510517 // Also, don't locally link if the SemVer doesn't match
511518 const localProjectVersion : string = localProject . packageJsonEditor . version ;
512- if ( semver . satisfies ( localProjectVersion , pair . packageVersion ) ) {
519+ if ( semver . satisfies ( localProjectVersion , packageVersion ) ) {
513520
514- // We will locally link this package
521+ // We will locally link this package, so instead add it to our special "rushDependencies"
522+ // field in the package.json file.
515523 if ( ! tempPackageJson . rushDependencies ) {
516524 tempPackageJson . rushDependencies = { } ;
517525 }
518- tempPackageJson . rushDependencies [ pair . packageName ] = pair . packageVersion ;
526+ tempPackageJson . rushDependencies [ packageName ] = packageVersion ;
519527 continue ;
520528 }
521529 }
522530 }
523531
524532 // We will NOT locally link this package; add it as a regular dependency.
525- tempPackageJson . dependencies ! [ pair . packageName ] = pair . packageVersion ;
533+ tempPackageJson . dependencies ! [ packageName ] = packageVersion ;
526534
527535 if ( shrinkwrapFile ) {
528- if ( ! shrinkwrapFile . tryEnsureCompatibleDependency ( pair . packageName , pair . packageVersion ,
536+ if ( ! shrinkwrapFile . tryEnsureCompatibleDependency ( packageName , packageVersion ,
529537 rushProject . tempProjectName ) ) {
530- shrinkwrapWarnings . push ( `"${ pair . packageName } " (${ pair . packageVersion } ) required by`
538+ shrinkwrapWarnings . push ( `"${ packageName } " (${ packageVersion } ) required by`
531539 + ` "${ rushProject . packageName } "` ) ;
532540 shrinkwrapIsUpToDate = false ;
533541 }
0 commit comments