From bd759e41e10ce0824b28f94ce70b6c13661e6a71 Mon Sep 17 00:00:00 2001 From: Lincoln <778157949@qq.com> Date: Fri, 10 Jan 2025 10:13:25 +0800 Subject: [PATCH] Setting to avoid manually configuring decoupledLocalDependencies across subspaces (#5072) Co-authored-by: Lincoln --- ...uto-generate-decouple_2025-01-09-12-50.json | 10 ++++++++++ common/config/rush/experiments.json | 9 +++++++++ common/reviews/api/rush-lib.api.md | 1 + .../common/config/rush/experiments.json | 11 ++++++++++- .../src/api/ExperimentsConfiguration.ts | 9 +++++++++ .../installManager/WorkspaceInstallManager.ts | 18 ++++++++++++++---- .../src/schemas/experiments.schema.json | 4 ++++ 7 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 common/changes/@microsoft/rush/chore-auto-generate-decouple_2025-01-09-12-50.json diff --git a/common/changes/@microsoft/rush/chore-auto-generate-decouple_2025-01-09-12-50.json b/common/changes/@microsoft/rush/chore-auto-generate-decouple_2025-01-09-12-50.json new file mode 100644 index 00000000000..e3e9e448a46 --- /dev/null +++ b/common/changes/@microsoft/rush/chore-auto-generate-decouple_2025-01-09-12-50.json @@ -0,0 +1,10 @@ +{ + "changes": [ + { + "packageName": "@microsoft/rush", + "comment": "Add a configuration option to avoid manually configuring decoupledLocalDependencies across subspaces.", + "type": "none" + } + ], + "packageName": "@microsoft/rush" +} \ No newline at end of file diff --git a/common/config/rush/experiments.json b/common/config/rush/experiments.json index ef02a74979f..f8071d80056 100644 --- a/common/config/rush/experiments.json +++ b/common/config/rush/experiments.json @@ -108,4 +108,13 @@ * When this toggle is enabled, Rush will only scan specific paths, significantly speeding up Git operations. */ // "enableSubpathScan": true + + /** + * Rush has a policy that normally requires Rush projects to specify `workspace:*` in package.json when depending + * on other projects in the workspace, unless they are explicitly declared as `decoupledLocalDependencies` + * in rush.json. Enabling this experiment will remove that requirement for dependencies belonging to a different + * subspace. This is useful for large product groups who work in separate subspaces and generally prefer to consume + * each other's packages via the NPM registry. + */ + // "exemptDecoupledDependenciesBetweenSubspaces": true } diff --git a/common/reviews/api/rush-lib.api.md b/common/reviews/api/rush-lib.api.md index d739176d23a..5ead502d072 100644 --- a/common/reviews/api/rush-lib.api.md +++ b/common/reviews/api/rush-lib.api.md @@ -482,6 +482,7 @@ export interface IExperimentsJson { buildSkipWithAllowWarningsInSuccessfulBuild?: boolean; cleanInstallAfterNpmrcChanges?: boolean; enableSubpathScan?: boolean; + exemptDecoupledDependenciesBetweenSubspaces?: boolean; forbidPhantomResolvableNodeModulesFolders?: boolean; generateProjectImpactGraphDuringRushUpdate?: boolean; noChmodFieldInTarHeaderNormalization?: boolean; diff --git a/libraries/rush-lib/assets/rush-init/common/config/rush/experiments.json b/libraries/rush-lib/assets/rush-init/common/config/rush/experiments.json index 0ed08b3edb9..605e320a4c9 100644 --- a/libraries/rush-lib/assets/rush-init/common/config/rush/experiments.json +++ b/libraries/rush-lib/assets/rush-init/common/config/rush/experiments.json @@ -108,5 +108,14 @@ * By default, rush perform a full scan of the entire repository. For example, Rush runs `git status` to check for local file changes. * When this toggle is enabled, Rush will only scan specific paths, significantly speeding up Git operations. */ - /*[LINE "HYPOTHETICAL"]*/ "enableSubpathScan": true + /*[LINE "HYPOTHETICAL"]*/ "enableSubpathScan": true, + + /** + * Rush has a policy that normally requires Rush projects to specify `workspace:*` in package.json when depending + * on other projects in the workspace, unless they are explicitly declared as `decoupledLocalDependencies` + * in rush.json. Enabling this experiment will remove that requirement for dependencies belonging to a different + * subspace. This is useful for large product groups who work in separate subspaces and generally prefer to consume + * each other's packages via the NPM registry. + */ + /*[LINE "HYPOTHETICAL"]*/ "exemptDecoupledDependenciesBetweenSubspaces": false } diff --git a/libraries/rush-lib/src/api/ExperimentsConfiguration.ts b/libraries/rush-lib/src/api/ExperimentsConfiguration.ts index 6f29b79f5ca..505aaf8a044 100644 --- a/libraries/rush-lib/src/api/ExperimentsConfiguration.ts +++ b/libraries/rush-lib/src/api/ExperimentsConfiguration.ts @@ -119,6 +119,15 @@ export interface IExperimentsJson { * When this toggle is enabled, Rush will only scan specific paths, significantly speeding up Git operations. */ enableSubpathScan?: boolean; + + /** + * Rush has a policy that normally requires Rush projects to specify `workspace:*` in package.json when depending + * on other projects in the workspace, unless they are explicitly declared as `decoupledLocalDependencies` + * in rush.json. Enabling this experiment will remove that requirement for dependencies belonging to a different + * subspace. This is useful for large product groups who work in separate subspaces and generally prefer to consume + * each other's packages via the NPM registry. + */ + exemptDecoupledDependenciesBetweenSubspaces?: boolean; } const _EXPERIMENTS_JSON_SCHEMA: JsonSchema = JsonSchema.fromLoadedObject(schemaJson); diff --git a/libraries/rush-lib/src/logic/installManager/WorkspaceInstallManager.ts b/libraries/rush-lib/src/logic/installManager/WorkspaceInstallManager.ts index 768904e0cf3..cf5936f4a5f 100644 --- a/libraries/rush-lib/src/logic/installManager/WorkspaceInstallManager.ts +++ b/libraries/rush-lib/src/logic/installManager/WorkspaceInstallManager.ts @@ -223,9 +223,19 @@ export class WorkspaceInstallManager extends BaseInstallManager { const dependencySpecifier: DependencySpecifier = new DependencySpecifier(name, version); // Is there a locally built Rush project that could satisfy this dependency? - const referencedLocalProject: RushConfigurationProject | undefined = + let referencedLocalProject: RushConfigurationProject | undefined = this.rushConfiguration.getProjectByName(name); + // If we enable exemptDecoupledDependenciesBetweenSubspaces, it will only check dependencies within the subspace. + if ( + this.rushConfiguration.experimentsConfiguration.configuration + .exemptDecoupledDependenciesBetweenSubspaces + ) { + if (referencedLocalProject && !subspace.contains(referencedLocalProject)) { + referencedLocalProject = undefined; + } + } + // Validate that local projects are referenced with workspace notation. If not, and it is not a // cyclic dependency, then it needs to be updated to specify `workspace:*` explicitly. Currently only // supporting versions and version ranges for specifying a local project. @@ -248,9 +258,9 @@ export class WorkspaceInstallManager extends BaseInstallManager { // eslint-disable-next-line no-console console.log( Colorize.red( - `"${rushProject.packageName}" depends on package "${name}" (${version}) which exists ` + - 'within the workspace but cannot be fulfilled with the specified version range. Either ' + - 'specify a valid version range, or add the package as a cyclic dependency.' + `"${rushProject.packageName}" depends on package "${name}" (${version}) which belongs to ` + + 'the workspace but cannot be fulfilled with the specified version range. Either ' + + 'specify a valid version range, or add the package to "decoupledLocalDependencies" in rush.json.' ) ); throw new AlreadyReportedError(); diff --git a/libraries/rush-lib/src/schemas/experiments.schema.json b/libraries/rush-lib/src/schemas/experiments.schema.json index f75a021f36c..35f173444b4 100644 --- a/libraries/rush-lib/src/schemas/experiments.schema.json +++ b/libraries/rush-lib/src/schemas/experiments.schema.json @@ -77,6 +77,10 @@ "enableSubpathScan": { "description": "By default, rush perform a full scan of the entire repository. For example, Rush runs `git status` to check for local file changes. When this toggle is enabled, Rush will only scan specific paths, significantly speeding up Git operations.", "type": "boolean" + }, + "exemptDecoupledDependenciesBetweenSubspaces": { + "description": "Rush has a policy that normally requires Rush projects to specify `workspace:*` in package.json when depending on other projects in the workspace, unless they are explicitly declared as `decoupledLocalDependencies in rush.json. Enabling this experiment will remove that requirement for dependencies belonging to a different subspace. This is useful for large product groups who work in separate subspaces and generally prefer to consume each other's packages via the NPM registry.", + "type": "boolean" } }, "additionalProperties": false