// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import * as path from 'node:path'; import ignore, { type Ignore } from 'ignore'; import type { IReadonlyLookupByPath, LookupByPath, IPrefixMatch } from '@rushstack/lookup-by-path'; import { Path, FileSystem, Async, AlreadyReportedError, Sort, JsonFile, Objects, type IPackageJson } from '@rushstack/node-core-library'; import { getRepoChanges, getRepoRoot, getDetailedRepoStateAsync, hashFilesAsync, type IDetailedRepoState, type IFileDiffStatus, RepoStateCache } from '@rushstack/package-deps-hash'; import type { ITerminal } from '@rushstack/terminal'; import type { RushConfiguration } from '../api/RushConfiguration'; import type { Subspace } from '../api/Subspace'; import { RushProjectConfiguration } from '../api/RushProjectConfiguration'; import type { RushConfigurationProject } from '../api/RushConfigurationProject'; import { BaseProjectShrinkwrapFile } from './base/BaseProjectShrinkwrapFile'; import { PnpmShrinkwrapFile } from './pnpm/PnpmShrinkwrapFile'; import { Git } from './Git'; import type { IPnpmOptionsJson } from './pnpm/PnpmOptionsConfiguration'; import { tryParsePnpmCatalogName } from './pnpm/PnpmCatalogReference'; import { getCommonPnpmConfigFilePath, getEffectivePnpmOptions, type IEffectivePnpmOptions } from './pnpm/EffectivePnpmOptions'; import { type IInputsSnapshotProjectMetadata, type IInputsSnapshot, InputsSnapshot, type GetInputsSnapshotAsyncFn } from './incremental/InputsSnapshot'; /** * @beta */ export interface IGetChangedProjectsOptions { targetBranchName: string; terminal: ITerminal; shouldFetch?: boolean; variant?: string; /** * If set to `true`, consider a project's external dependency installation layout as defined in the * package manager lockfile when determining if it has changed. */ includeExternalDependencies: boolean; /** * If set to `true` apply the `incrementalBuildIgnoredGlobs` property in a project's `rush-project.json` * and exclude matched files from change detection. */ enableFiltering: boolean; /** Optional request-owned configuration source, avoiding shared project/rig caches. */ getIncrementalBuildIgnoredGlobsAsync?: ( project: RushConfigurationProject ) => Promise | undefined>; /** * If set to `true`, excludes projects where the only changes are: * - A version change to `package.json`, optionally accompanied by changes to `peerDependencies` * - Changes to `CHANGELOG.md` and/or `CHANGELOG.json` files * * This prevents `rush version --bump` from triggering `rush change --verify` to request change files * for the version bumps and changelog updates it creates. */ excludeVersionOnlyChanges?: boolean; } /** * @internal */ export interface IRawRepoState { projectState: Map> | undefined; rootDir: string; rawHashes: Map; } /** * A project dependency file (shrinkwrap-deps.json) that change detection hashes is missing. */ class MissingProjectShrinkwrapFileError extends Error { public constructor(projectShrinkwrapFilePath: string) { super( `A project dependency file (${projectShrinkwrapFilePath}) is missing. You may need to run ` + '"rush install" or "rush update".' ); } } /** * @beta */ export class ProjectChangeAnalyzer { readonly #rushConfiguration: RushConfiguration; readonly #git: Git; public constructor(rushConfiguration: RushConfiguration) { this.#rushConfiguration = rushConfiguration; this.#git = new Git(this.#rushConfiguration); } /** * Gets a list of projects that have changed in the current state of the repo * when compared to the specified branch, optionally taking the shrinkwrap and settings in * the rush-project.json file into consideration. */ public async getChangedProjectsAsync( options: IGetChangedProjectsOptions ): Promise> { const rushConfiguration: RushConfiguration = this.#rushConfiguration; const { targetBranchName, terminal, includeExternalDependencies, enableFiltering, shouldFetch, variant, excludeVersionOnlyChanges } = options; const gitPath: string = this.#git.getGitPathOrThrow(); const repoRoot: string = getRepoRoot(rushConfiguration.rushJsonFolder); // if the given targetBranchName is a commit, we assume it is the merge base const isTargetBranchACommit: boolean = await this.#git.determineIfRefIsACommitAsync(targetBranchName); const mergeCommit: string = isTargetBranchACommit ? targetBranchName : await this.#git.getMergeBaseAsync(targetBranchName, terminal, shouldFetch); const changedFiles: Map = getRepoChanges(repoRoot, mergeCommit, gitPath); const lookup: LookupByPath = rushConfiguration.getProjectLookupForRoot(repoRoot); const changesByProject: Map< RushConfigurationProject, Map > = this.getChangesByProject(lookup, changedFiles); const changedProjects: Set = new Set(); await Async.forEachAsync( changesByProject, async ([project, projectChanges]) => { const filteredChanges: Map = enableFiltering ? await this._filterProjectDataAsync( project, projectChanges, repoRoot, terminal, options.getIncrementalBuildIgnoredGlobsAsync ) : projectChanges; // Skip if no changes if (filteredChanges.size === 0) { return; } // If excludeVersionOnlyChanges is not enabled, include the project if (!excludeVersionOnlyChanges) { changedProjects.add(project); return; } // Filter out version bumps, peer dependency updates accompanying a version bump, and changelogs. for (const [filePath, diffStatus] of filteredChanges) { // Use lookup to find the project-relative path const match: IPrefixMatch | undefined = lookup.findLongestPrefixMatch(filePath); if (!match) { // This should be unreachable as projectChanges contains files where match.value === project changedProjects.add(project); return; } const projectRelativePath: string = filePath.slice(match.index); // Skip CHANGELOG.md and CHANGELOG.json files at project root if (projectRelativePath === '/CHANGELOG.md' || projectRelativePath === '/CHANGELOG.json') { continue; } // Check if this is package.json at project root with only an allowed version bump change. if (projectRelativePath === '/package.json') { const isVersionBumpChange: boolean = await isVersionBumpChangeAsync( diffStatus, repoRoot, this.#git ); if (isVersionBumpChange) { continue; } } // Found a non-excluded change changedProjects.add(project); break; } }, { concurrency: 10 } ); // Detect per-subspace changes: catalog entries in pnpm-config.json and external dependency lockfiles const subspaces: Iterable = rushConfiguration.subspacesFeatureEnabled ? rushConfiguration.subspaces : [rushConfiguration.defaultSubspace]; const variantToUse: string | undefined = includeExternalDependencies ? (variant ?? (await this.#rushConfiguration.getCurrentlyInstalledVariantAsync())) : undefined; await Async.forEachAsync(subspaces, async (subspace: Subspace) => { const subspaceProjects: RushConfigurationProject[] = subspace.getProjects(); // Detect changes to pnpm catalog entries in pnpm-config.json if (rushConfiguration.isPnpm) { await this.#detectCatalogChangesAsync( subspace, rushConfiguration, changedFiles, mergeCommit, repoRoot, terminal, changedProjects ); } // External dependency changes are not allowed to be filtered, so add these after filtering if (includeExternalDependencies) { // Even though changing the installed version of a nested dependency merits a change file, // ignore lockfile changes for `rush change` for the moment const fullShrinkwrapPath: string = subspace.getCommittedShrinkwrapFilePath(variantToUse); const relativeShrinkwrapFilePath: string = Path.convertToSlashes( path.relative(repoRoot, fullShrinkwrapPath) ); const shrinkwrapStatus: IFileDiffStatus | undefined = changedFiles.get(relativeShrinkwrapFilePath); if (shrinkwrapStatus) { if (shrinkwrapStatus.status !== 'M') { if (rushConfiguration.subspacesFeatureEnabled) { terminal.writeWarningLine( `"${subspace.subspaceName}" subspace lockfile was created or deleted. Assuming all projects are affected.` ); } else { terminal.writeWarningLine( `Lockfile was created or deleted. Assuming all projects are affected.` ); } for (const project of subspaceProjects) { changedProjects.add(project); } return; } if (rushConfiguration.isPnpm) { const subspaceHasNoProjects: boolean = subspaceProjects.length === 0; const currentShrinkwrap: PnpmShrinkwrapFile | undefined = PnpmShrinkwrapFile.loadFromFile( fullShrinkwrapPath, { subspaceHasNoProjects } ); if (!currentShrinkwrap) { throw new Error(`Unable to obtain current shrinkwrap file.`); } const oldShrinkwrapText: string = await this.#git.getBlobContentAsync({ // : syntax: https://git-scm.com/docs/gitrevisions blobSpec: `${mergeCommit}:${relativeShrinkwrapFilePath}`, repositoryRoot: repoRoot }); const oldShrinkWrap: PnpmShrinkwrapFile = PnpmShrinkwrapFile.loadFromString(oldShrinkwrapText, { subspaceHasNoProjects }); for (const project of subspaceProjects) { if ( currentShrinkwrap .getProjectShrinkwrap(project) .hasChanges(oldShrinkWrap.getProjectShrinkwrap(project)) ) { changedProjects.add(project); } } } else { if (rushConfiguration.subspacesFeatureEnabled) { terminal.writeLine( `"${subspace.subspaceName}" subspace lockfile has changed and lockfile content comparison is only supported for pnpm. Assuming all projects are affected.` ); } else { terminal.writeLine( `Lockfile has changed and lockfile content comparison is only supported for pnpm. Assuming all projects are affected.` ); } subspaceProjects.forEach((project) => changedProjects.add(project)); return; } } } }); // Sort the set by projectRelativeFolder to avoid race conditions in the results const sortedChangedProjects: RushConfigurationProject[] = Array.from(changedProjects); Sort.sortBy(sortedChangedProjects, (project) => project.projectRelativeFolder); return new Set(sortedChangedProjects); } protected getChangesByProject( lookup: LookupByPath, changedFiles: Map ): Map> { return lookup.groupByChild(changedFiles); } /** * Gets a snapshot of the input state of the Rush workspace that can be queried for incremental * build operations and use by the build cache. * * @remarks * If the state cannot be calculated, this writes a warning and continues without a snapshot. With * `throwOnMissingProjectShrinkwrapFile`, a missing project dependency file (shrinkwrap-deps.json) instead * throws its error, whether it is detected when the provider is created or when a snapshot fails. A host that * cannot continue without a snapshot can then report that actionable cause. * * With `reuseUnchangedInputs`, the provider keeps the state of the Git repository between snapshots, and * returns its previous snapshot while the inputs are unchanged. This suits a long-lived host, such as the Rush * daemon, that takes a snapshot for each request. * @internal */ public async _tryGetSnapshotProviderAsync( projectConfigurations: ReadonlyMap, terminal: ITerminal, projectSelection?: ReadonlySet, options?: { readonly throwOnMissingProjectShrinkwrapFile?: boolean; readonly reuseUnchangedInputs?: boolean; } ): Promise { const throwOnMissingProjectShrinkwrapFile: boolean = !!options?.throwOnMissingProjectShrinkwrapFile; try { const gitPath: string = this.#git.getGitPathOrThrow(); if (!this.#git.isPathUnderGitWorkingTree()) { terminal.writeLine( `The Rush monorepo is not in a Git repository. Rush will proceed without incremental build support.` ); return; } const rushConfiguration: RushConfiguration = this.#rushConfiguration; // Do not use getGitInfo().root; it is the root of the *primary* worktree, not the *current* one. const rootDirectory: string = getRepoRoot(rushConfiguration.rushJsonFolder, gitPath); // Load the rush-project.json files for the whole repository const additionalGlobs: IAdditionalGlob[] = []; const projectMap: Map = new Map(); for (const project of rushConfiguration.projects) { const projectConfig: RushProjectConfiguration | undefined = projectConfigurations.get(project); const additionalFilesByOperationName: Map> = new Map(); const projectMetadata: IInputsSnapshotProjectMetadata = { projectConfig, additionalFilesByOperationName }; projectMap.set(project, projectMetadata); if (projectConfig) { const { operationSettingsByOperationName } = projectConfig; for (const [operationName, { dependsOnAdditionalFiles }] of operationSettingsByOperationName) { if (dependsOnAdditionalFiles) { const additionalFilesForOperation: Set = new Set(); additionalFilesByOperationName.set(operationName, additionalFilesForOperation); for (const pattern of dependsOnAdditionalFiles) { additionalGlobs.push({ project, operationName, additionalFilesForOperation, pattern }); } } } } } // Include project shrinkwrap files as part of the computation const additionalRelativePathsToHash: string[] = []; const projectShrinkwrapFilePaths: string[] = []; const globalAdditionalFiles: string[] = []; if (rushConfiguration.isPnpm) { await Async.forEachAsync(rushConfiguration.projects, async (project: RushConfigurationProject) => { const projectShrinkwrapFilePath: string = BaseProjectShrinkwrapFile.getFilePathForProject(project); if (!(await FileSystem.existsAsync(projectShrinkwrapFilePath))) { if (rushConfiguration.subspacesFeatureEnabled) { return; } throw new MissingProjectShrinkwrapFileError(projectShrinkwrapFilePath); } const relativeProjectShrinkwrapFilePath: string = Path.convertToSlashes( path.relative(rootDirectory, projectShrinkwrapFilePath) ); additionalRelativePathsToHash.push(relativeProjectShrinkwrapFilePath); if (!rushConfiguration.subspacesFeatureEnabled) { projectShrinkwrapFilePaths.push(projectShrinkwrapFilePath); } }); } else { // Add the shrinkwrap file to every project's dependencies const currentVariant: string | undefined = await this.#rushConfiguration.getCurrentlyInstalledVariantAsync(); const shrinkwrapFile: string = Path.convertToSlashes( path.relative( rootDirectory, rushConfiguration.defaultSubspace.getCommittedShrinkwrapFilePath(currentVariant) ) ); globalAdditionalFiles.push(shrinkwrapFile); } const lookupByPath: IReadonlyLookupByPath = this.#rushConfiguration.getProjectLookupForRoot(rootDirectory); let filterPath: string[] = []; if ( projectSelection && projectSelection.size > 0 && this.#rushConfiguration.experimentsConfiguration.configuration.enableSubpathScan ) { filterPath = Array.from(projectSelection, ({ projectFolder }) => projectFolder); } const repoStateCache: RepoStateCache | undefined = options?.reuseUnchangedInputs ? getRepoStateCache(rootDirectory, gitPath, rushConfiguration.commonTempFolder) : undefined; let previousSnapshot: IReusableInputsSnapshot | undefined; return async function tryGetSnapshotAsync(): Promise { // Recorded before Git reads the working tree: a file saved after this may be newer than its hash. const workingTreeReadStartTimeMs: number = Date.now(); try { const [repoState, additionalFiles] = await Promise.all([ repoStateCache ? repoStateCache.getDetailedRepoStateAsync(additionalRelativePathsToHash, filterPath) : getDetailedRepoStateAsync(rootDirectory, additionalRelativePathsToHash, gitPath, filterPath), getAdditionalFilesFromRushProjectConfigurationAsync( additionalGlobs, lookupByPath, rootDirectory, terminal ) ]); const { files: hashes, symlinks, hasUncommittedChanges } = repoState; if (symlinks.size > 0) { terminal.writeWarningLine( `Warning: Detected ${symlinks.size} Git-tracked symlinks in the repository. ` + `These will be ignored by the change detection engine.` ); } for (const file of additionalFiles) { if (hashes.has(file) || symlinks.has(file)) { additionalFiles.delete(file); } } const additionalHashes: Map = new Map( additionalFiles.size > 0 ? await hashFilesAsync(rootDirectory, additionalFiles, gitPath) : [] ); const environment: Record = { ...process.env }; // The files that each operation depends on accumulate across snapshots const operationAdditionalFileCount: number = countOperationAdditionalFiles(projectMap); if ( previousSnapshot && previousSnapshot.repoState === repoState && previousSnapshot.operationAdditionalFileCount === operationAdditionalFileCount && areMapsEqual(previousSnapshot.additionalHashes, additionalHashes) && areEnvironmentsEqual(previousSnapshot.environment, environment) ) { // Every hash that a new snapshot would compute is the same. The previous snapshot began reading the // working tree earlier, which only widens the window in which a saved file may be newer than its hash. return previousSnapshot.snapshot; } const snapshot: InputsSnapshot = new InputsSnapshot({ additionalHashes, environment, globalAdditionalFiles, hashes, hasUncommittedChanges, lookupByPath, // Only the projects whose inputs changed need new state, and their hashes computed again previousSnapshot: previousSnapshot?.snapshot, projectMap, rootDir: rootDirectory, workingTreeReadStartTimeMs }); if (repoStateCache) { previousSnapshot = { additionalHashes, environment, operationAdditionalFileCount, repoState, snapshot }; } return snapshot; } catch (e) { // The files were checked once, when this provider was created. A file removed since then fails // "git hash-object" with an obscure message, so check them again, only after a failure. const error: Error = (await tryGetMissingFileErrorAsync(projectShrinkwrapFilePaths)) ?? e; if (throwOnMissingProjectShrinkwrapFile && error instanceof MissingProjectShrinkwrapFileError) { throw error; } // If getRepoState fails, don't fail the whole build. Treat this case as if we don't know anything about // the state of the files in the repo. This can happen if the environment doesn't have Git. terminal.writeWarningLine( `Error calculating the state of the repo. (inner error: ${ error.stack ?? error.message ?? error }). Continuing without diffing files.` ); return; } }; } catch (e) { if (throwOnMissingProjectShrinkwrapFile && e instanceof MissingProjectShrinkwrapFileError) { throw e; } // If getRepoState fails, don't fail the whole build. Treat this case as if we don't know anything about // the state of the files in the repo. This can happen if the environment doesn't have Git. terminal.writeWarningLine( `Error calculating the state of the repo. (inner error: ${ e.stack ?? e.message ?? e }). Continuing without diffing files.` ); return; } } /** * @internal */ public async _filterProjectDataAsync( project: RushConfigurationProject, unfilteredProjectData: Map, rootDir: string, terminal: ITerminal, getIgnoreGlobsAsync?: IGetChangedProjectsOptions['getIncrementalBuildIgnoredGlobsAsync'] ): Promise> { const ignoreMatcher: Ignore | undefined = await this.#getIgnoreMatcherForProjectAsync( project, terminal, getIgnoreGlobsAsync ); if (!ignoreMatcher) { return unfilteredProjectData; } const projectKey: string = path.relative(rootDir, project.projectFolder); const projectKeyLength: number = projectKey.length + 1; // At this point, `filePath` is guaranteed to start with `projectKey`, so // we can safely slice off the first N characters to get the file path relative to the // root of the project. const filteredProjectData: Map = new Map(); for (const [filePath, value] of unfilteredProjectData) { const relativePath: string = filePath.slice(projectKeyLength); if (!ignoreMatcher.ignores(relativePath)) { // Add the file path to the filtered data if it is not ignored filteredProjectData.set(filePath, value); } } return filteredProjectData; } async #getIgnoreMatcherForProjectAsync( project: RushConfigurationProject, terminal: ITerminal, getIgnoreGlobsAsync?: IGetChangedProjectsOptions['getIncrementalBuildIgnoredGlobsAsync'] ): Promise { const incrementalBuildIgnoredGlobs: ReadonlyArray | undefined = getIgnoreGlobsAsync ? await getIgnoreGlobsAsync(project) : await RushProjectConfiguration.tryLoadIgnoreGlobsForProjectAsync(project, terminal); if (incrementalBuildIgnoredGlobs && incrementalBuildIgnoredGlobs.length) { const ignoreMatcher: Ignore = ignore(); ignoreMatcher.add(incrementalBuildIgnoredGlobs as string[]); return ignoreMatcher; } } /** * Detects changes to the pnpm catalog entries that install uses for a subspace and marks * affected projects as changed. */ async #detectCatalogChangesAsync( subspace: Subspace, rushConfiguration: RushConfiguration, changedFiles: Map, mergeCommit: string, repoRoot: string, terminal: ITerminal, changedProjects: Set ): Promise { const effectivePnpmOptions: IEffectivePnpmOptions = getEffectivePnpmOptions(subspace, rushConfiguration); // Default to an empty object if no global catalogs are configured, handle case of globalCatalogs being deleted const currentCatalogs: Record> = effectivePnpmOptions.pnpmOptions .globalCatalogs ?? {}; const toRepoRelativePath = (filePath: string): string => Path.convertToSlashes(path.relative(repoRoot, filePath)); const subspacePnpmConfigRelativePath: string = toRepoRelativePath(subspace.getPnpmConfigFilePath()); const commonPnpmConfigRelativePath: string = toRepoRelativePath( getCommonPnpmConfigFilePath(rushConfiguration) ); // Check the subspace's own file even if deleted, since deleting it switches the subspace to the common file if ( !changedFiles.has(subspacePnpmConfigRelativePath) && !(effectivePnpmOptions.isFallback && changedFiles.has(commonPnpmConfigRelativePath)) ) { return; } // Determine which specific packages changed within each catalog namespace // Maps catalogNamespace (e.g. "default", "react17") → Set of changed package names let oldCatalogs: Record> | undefined; // Use whichever file was in effect at the merge commit const oldPnpmConfigCandidatePaths: Set = new Set([ subspacePnpmConfigRelativePath, commonPnpmConfigRelativePath ]); for (const oldPnpmConfigRelativePath of oldPnpmConfigCandidatePaths) { let oldPnpmConfigText: string; try { oldPnpmConfigText = await this.#git.getBlobContentAsync({ blobSpec: `${mergeCommit}:${oldPnpmConfigRelativePath}`, repositoryRoot: repoRoot }); } catch { continue; } try { const oldPnpmConfig: IPnpmOptionsJson = JsonFile.parseString(oldPnpmConfigText); oldCatalogs = oldPnpmConfig.globalCatalogs ?? {}; } catch { // Unparseable: treat all packages as changed } break; } if (oldCatalogs === undefined) { // Old file didn't exist or was unparseable — treat all packages in all current catalogs as changed if (rushConfiguration.subspacesFeatureEnabled) { terminal.writeWarningLine( `"${subspace.subspaceName}" subspace pnpm-config.json was created or unparseable. Assuming all projects are affected.` ); } else { terminal.writeWarningLine( `pnpm-config.json was created or unparseable. Assuming all projects are affected.` ); } } const changedCatalogPackages: Map> = new Map(); const currentCatalogEntries: Map> = new Map( Object.entries(currentCatalogs) ); if (oldCatalogs === undefined) { // Could not load old catalogs — treat all packages in all current catalogs as changed for (const [catalogName, packages] of currentCatalogEntries) { changedCatalogPackages.set(catalogName, new Set(Object.keys(packages))); } } else { // Check current catalogs for new or modified package entries for (const [catalogName, packages] of currentCatalogEntries) { const oldPackages: Record | undefined = oldCatalogs[catalogName]; if (!oldPackages) { // Entire catalog is new — all packages in it are changed changedCatalogPackages.set(catalogName, new Set(Object.keys(packages))); continue; } const changedPackages: Set = new Set(); for (const [pkgName, version] of Object.entries(packages)) { if (oldPackages[pkgName] !== version) { changedPackages.add(pkgName); } } // Check for packages that were removed from this catalog for (const pkgName of Object.keys(oldPackages)) { if (!Object.prototype.hasOwnProperty.call(packages, pkgName)) { changedPackages.add(pkgName); } } if (changedPackages.size > 0) { changedCatalogPackages.set(catalogName, changedPackages); } } // Check for catalogs that were entirely removed for (const [catalogName, oldPackages] of Object.entries(oldCatalogs)) { if (!Object.prototype.hasOwnProperty.call(currentCatalogs, catalogName)) { changedCatalogPackages.set(catalogName, new Set(Object.keys(oldPackages))); } } } if (changedCatalogPackages.size > 0) { // Check each project in the subspace to see if it depends on a changed catalog package const subspaceProjects: RushConfigurationProject[] = subspace.getProjects(); subspaceProjects.forEach((project) => { const { dependencies, devDependencies, optionalDependencies, peerDependencies } = project.packageJson; const allDependencies: Set<[string, string]> = new Set( [dependencies, devDependencies, optionalDependencies, peerDependencies].flatMap((deps) => Object.entries(deps ?? {}) ) ); for (const [depName, depVersion] of allDependencies) { const catalogName: string | undefined = tryParsePnpmCatalogName(depVersion); if (catalogName !== undefined) { const changedPkgs: Set | undefined = changedCatalogPackages.get(catalogName); if (changedPkgs?.has(depName)) { changedProjects.add(project); return; } } } }); } } } async function isVersionBumpChangeAsync( diffStatus: IFileDiffStatus, repoRoot: string, git: Git ): Promise { if (diffStatus.status !== 'M') { return false; } try { const [oldPackageJsonContent, newPackageJsonContent] = await Promise.all([ git.getBlobContentAsync({ blobSpec: diffStatus.oldhash, repositoryRoot: repoRoot }), git.getBlobContentAsync({ blobSpec: diffStatus.newhash, repositoryRoot: repoRoot }) ]); return isPackageJsonVersionOnlyChange(oldPackageJsonContent, newPackageJsonContent); } catch (error) { return false; } } /** * Returns an error naming the first of the specified project dependency files that is missing, if any. */ async function tryGetMissingFileErrorAsync( projectShrinkwrapFilePaths: ReadonlyArray ): Promise { const exists: boolean[] = await Async.mapAsync( projectShrinkwrapFilePaths, (filePath: string) => FileSystem.existsAsync(filePath), { concurrency: 50 } ); const missingIndex: number = exists.indexOf(false); return missingIndex < 0 ? undefined : new MissingProjectShrinkwrapFileError(projectShrinkwrapFilePaths[missingIndex]); } /** * Returns the error that {@link ProjectChangeAnalyzer._tryGetSnapshotProviderAsync} reports for a missing * project dependency file, if any is missing. This happens when a project is added before "rush install". */ export async function tryGetMissingProjectShrinkwrapFileErrorAsync( rushConfiguration: RushConfiguration ): Promise { if (!rushConfiguration.isPnpm || rushConfiguration.subspacesFeatureEnabled) { return undefined; } return await tryGetMissingFileErrorAsync( rushConfiguration.projects.map((project: RushConfigurationProject) => BaseProjectShrinkwrapFile.getFilePathForProject(project) ) ); } interface IAdditionalGlob { project: RushConfigurationProject; operationName: string; additionalFilesForOperation: Set; pattern: string; } /** * A snapshot, and the inputs that it was computed from. */ interface IReusableInputsSnapshot { readonly additionalHashes: ReadonlyMap; readonly environment: Readonly>; readonly operationAdditionalFileCount: number; readonly repoState: IDetailedRepoState; readonly snapshot: InputsSnapshot; } /** * The caches of the state of each Git repository, by the path of Git and the root of the repository. They outlive * the snapshot providers, so that a long-lived host, such as the Rush daemon, keeps using a cache when it creates * a provider again. */ const repoStateCacheByRepository: Map = new Map(); function getRepoStateCache( rootDirectory: string, gitPath: string, temporaryFolderPath: string ): RepoStateCache { const key: string = `${gitPath}\0${rootDirectory}`; let repoStateCache: RepoStateCache | undefined = repoStateCacheByRepository.get(key); if (!repoStateCache) { repoStateCache = new RepoStateCache({ rootDirectory, gitPath, temporaryFolderPath }); repoStateCacheByRepository.set(key, repoStateCache); } return repoStateCache; } function countOperationAdditionalFiles( projectMap: ReadonlyMap ): number { let count: number = 0; for (const { additionalFilesByOperationName } of projectMap.values()) { for (const additionalFiles of additionalFilesByOperationName?.values() ?? []) { count += additionalFiles.size; } } return count; } function areMapsEqual(a: ReadonlyMap, b: ReadonlyMap): boolean { if (a.size !== b.size) { return false; } for (const [key, value] of a) { if (b.get(key) !== value || !b.has(key)) { return false; } } return true; } function areEnvironmentsEqual( a: Readonly>, b: Readonly> ): boolean { return areMapsEqual(new Map(Object.entries(a)), new Map(Object.entries(b))); } async function getAdditionalFilesFromRushProjectConfigurationAsync( additionalGlobs: IAdditionalGlob[], rootRelativeLookupByPath: IReadonlyLookupByPath, rootDirectory: string, terminal: ITerminal ): Promise> { const additionalFilesFromRushProjectConfiguration: Set = new Set(); if (!additionalGlobs.length) { return additionalFilesFromRushProjectConfiguration; } const { default: glob } = await import('fast-glob'); await Async.forEachAsync(additionalGlobs, async (item: IAdditionalGlob) => { const { project, operationName, additionalFilesForOperation, pattern } = item; const matches: string[] = await glob(pattern, { cwd: project.projectFolder, onlyFiles: true, // We want to keep path's type unchanged, // i.e. if the pattern was a relative path, then matched paths should also be relative paths // if the pattern was an absolute path, then matched paths should also be absolute paths // // We are doing this because these paths are going to be used to calculate operation state hashes and some users // might choose to depend on global files (e.g. `/etc/os-release`) and some might choose to depend on local non-project files // (e.g. `../path/to/workspace/file`) // // In both cases we want that path to the resource to be the same on all machines, // regardless of what is the current working directory. // // That being said, we want to keep `absolute` options here as false: absolute: false }); for (const match of matches) { // The glob result is relative to the project folder, but we want it to be relative to the repo root const rootRelativeFilePath: string = Path.convertToSlashes( path.relative(rootDirectory, path.resolve(project.projectFolder, match)) ); if (rootRelativeFilePath.startsWith('../')) { // The target file is outside of the Git tree, use the original result of the match. additionalFilesFromRushProjectConfiguration.add(match); additionalFilesForOperation.add(match); } else { // The target file is inside of the Git tree, find out if it is in a Rush project. const projectMatch: RushConfigurationProject | undefined = rootRelativeLookupByPath.findChildPath(rootRelativeFilePath); if (projectMatch && projectMatch !== project) { terminal.writeErrorLine( `In project "${project.packageName}" ("${project.projectRelativeFolder}"), ` + `config for operation "${operationName}" specifies a glob "${pattern}" that selects a file "${rootRelativeFilePath}" in a different workspace project ` + `"${projectMatch.packageName}" ("${projectMatch.projectRelativeFolder}"). ` + `This is forbidden. The "dependsOnAdditionalFiles" property of "rush-project.json" may only be used to refer to non-workspace files, non-project files, ` + `or untracked files in the current project. To depend on files in another workspace project, use "devDependencies" in "package.json".` ); throw new AlreadyReportedError(); } additionalFilesForOperation.add(rootRelativeFilePath); additionalFilesFromRushProjectConfiguration.add(rootRelativeFilePath); } } }); return additionalFilesFromRushProjectConfiguration; } /** * Compares two package.json file contents and determines whether the package's version changed and * all other changes are limited to peerDependencies. * @param oldPackageJsonContent - The old package.json content as a string * @param newPackageJsonContent - The new package.json content as a string * @returns true if the package version changed and every other field except peerDependencies is unchanged */ export function isPackageJsonVersionOnlyChange( oldPackageJsonContent: string, newPackageJsonContent: string ): boolean { try { return isPackageJsonVersionBumpChange( JSON.parse(oldPackageJsonContent), JSON.parse(newPackageJsonContent) ); } catch (error) { // If we can't parse the JSON, assume it's not a version-only change return false; } } /** * Determines whether a package.json differs only by its version and peerDependencies. */ export function isPackageJsonVersionBumpChange( oldPackageJson: IPackageJson, newPackageJson: IPackageJson ): boolean { if ( typeof oldPackageJson.version !== 'string' || typeof newPackageJson.version !== 'string' || oldPackageJson.version === newPackageJson.version || oldPackageJson.name !== newPackageJson.name ) { return false; } const oldPackageJsonWithoutBumpFields: Partial = { ...oldPackageJson }; const newPackageJsonWithoutBumpFields: Partial = { ...newPackageJson }; oldPackageJsonWithoutBumpFields.version = undefined; newPackageJsonWithoutBumpFields.version = undefined; oldPackageJsonWithoutBumpFields.peerDependencies = undefined; newPackageJsonWithoutBumpFields.peerDependencies = undefined; return Objects.areDeepEqual(oldPackageJsonWithoutBumpFields, newPackageJsonWithoutBumpFields); }