Skip to content

[rush-sdk] Expose file change analysis on the ProjectChangeAnalyzer class #4959

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

Merged
merged 15 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Expose `getChangesByProject` to allow classes that extend ProjectChangeAnalyzer to override file change analysis",
"type": "patch"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/lookup-by-path",
"comment": "Allow for a map of file paths to arbitrary info to be grouped by the nearest entry in the LookupByPath trie",
"type": "minor"
}
],
"packageName": "@rushstack/lookup-by-path"
}
25 changes: 13 additions & 12 deletions common/config/subspaces/build-tests-subspace/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush.
{
"pnpmShrinkwrapHash": "0e569d956f72f98565ea4207cba2d7b359e5cdaa",
"pnpmShrinkwrapHash": "5b75a8ef91af53a8caf52319e5eb0042c4d06852",
"preferredVersionsHash": "ce857ea0536b894ec8f346aaea08cfd85a5af648",
"packageJsonInjectedDependenciesHash": "15081ac6b4174f98e6a82a839055fbda1a33680d"
"packageJsonInjectedDependenciesHash": "8927ca4e0147b9436659f98a2ff8ca347107d52f"
}
3 changes: 3 additions & 0 deletions common/config/subspaces/default/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/reviews/api/lookup-by-path.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class LookupByPath<TItem> {
findChildPath(childPath: string): TItem | undefined;
findChildPathFromSegments(childPathSegments: Iterable<string>): TItem | undefined;
findLongestPrefixMatch(query: string): IPrefixMatch<TItem> | undefined;
groupByChild<TInfo>(infoByPath: Map<string, TInfo>): Map<TItem, Map<string, TInfo>>;
static iteratePathSegments(serializedPath: string, delimiter?: string): Iterable<string>;
setItem(serializedPath: string, value: TItem): this;
setItemFromSegments(pathSegments: Iterable<string>, value: TItem): this;
Expand Down
3 changes: 3 additions & 0 deletions common/reviews/api/rush-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type { CollatedWriter } from '@rushstack/stream-collator';
import type { CommandLineParameter } from '@rushstack/ts-command-line';
import { CommandLineParameterKind } from '@rushstack/ts-command-line';
import { HookMap } from 'tapable';
import { IFileDiffStatus } from '@rushstack/package-deps-hash';
import { IPackageJson } from '@rushstack/node-core-library';
import { IPrefixMatch } from '@rushstack/lookup-by-path';
import { ITerminal } from '@rushstack/terminal';
Expand Down Expand Up @@ -1116,6 +1117,8 @@ export class ProjectChangeAnalyzer {
// (undocumented)
_filterProjectDataAsync<T>(project: RushConfigurationProject, unfilteredProjectData: Map<string, T>, rootDir: string, terminal: ITerminal): Promise<Map<string, T>>;
getChangedProjectsAsync(options: IGetChangedProjectsOptions): Promise<Set<RushConfigurationProject>>;
// (undocumented)
protected getChangesByProject(lookup: LookupByPath<RushConfigurationProject>, changedFiles: Map<string, IFileDiffStatus>): Map<RushConfigurationProject, Map<string, IFileDiffStatus>>;
// @internal
_tryGetProjectDependenciesAsync(project: RushConfigurationProject, terminal: ITerminal): Promise<Map<string, string> | undefined>;
// @internal
Expand Down
27 changes: 27 additions & 0 deletions libraries/lookup-by-path/src/LookupByPath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,33 @@ export class LookupByPath<TItem> {
return best;
}

/**
* Groups the provided map of info by the nearest entry in the trie that contains the path. If the path
* is not found in the trie, the info is ignored.
*
* @returns The grouped info, grouped by the nearest entry in the trie that contains the path
*
* @param infoByPath - The info to be grouped, keyed by path
*/
public groupByChild<TInfo>(infoByPath: Map<string, TInfo>): Map<TItem, Map<string, TInfo>> {
const groupedInfo: Map<TItem, Map<string, TInfo>> = new Map();

for (const [path, info] of infoByPath) {
const group: TItem | undefined = this.findChildPath(path);
if (!group) {
continue;
}
let groupInfo: Map<string, TInfo> | undefined = groupedInfo.get(group);
if (!groupInfo) {
groupInfo = new Map();
groupedInfo.set(group, groupInfo);
}
groupInfo.set(path, info);
}

return groupedInfo;
}

/**
* Iterates through progressively longer prefixes of a given string and returns as soon
* as the number of candidate items that match the prefix are 1 or 0.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { LookupByPath } from './LookupByPath';
import { LookupByPath } from '../LookupByPath';

describe(LookupByPath.iteratePathSegments.name, () => {
it('returns empty for an empty string', () => {
Expand Down Expand Up @@ -143,3 +143,61 @@ describe(LookupByPath.prototype.findLongestPrefixMatch.name, () => {
expect(tree.findLongestPrefixMatch('foo/foo')).toEqual({ value: 1, index: 3 });
});
});

describe(LookupByPath.prototype.groupByChild.name, () => {
const lookup: LookupByPath<string> = new LookupByPath([
['foo', 'foo'],
['foo/bar', 'bar'],
['foo/bar/baz', 'baz']
]);

it('returns empty map for empty input', () => {
expect(lookup.groupByChild(new Map())).toEqual(new Map());
});

it('groups items by the closest group that contains the file path', () => {
const infoByPath: Map<string, string> = new Map([
['foo', 'foo'],
['foo/bar', 'bar'],
['foo/bar/baz', 'baz'],
['foo/bar/baz/qux', 'qux'],
['foo/bar/baz/qux/quux', 'quux']
]);

const expected: Map<string, Map<string, string>> = new Map([
['foo', new Map([['foo', 'foo']])],
['bar', new Map([['foo/bar', 'bar']])],
[
'baz',
new Map([
['foo/bar/baz', 'baz'],
['foo/bar/baz/qux', 'qux'],
['foo/bar/baz/qux/quux', 'quux']
])
]
]);

expect(lookup.groupByChild(infoByPath)).toEqual(expected);
});

it('ignores items that do not exist in the lookup', () => {
const infoByPath: Map<string, string> = new Map([
['foo', 'foo'],
['foo/qux', 'qux'],
['bar', 'bar'],
['baz', 'baz']
]);

const expected: Map<string, Map<string, string>> = new Map([
[
'foo',
new Map([
['foo', 'foo'],
['foo/qux', 'qux']
])
]
]);

expect(lookup.groupByChild(infoByPath)).toEqual(expected);
});
});
92 changes: 43 additions & 49 deletions libraries/rush-lib/src/logic/ProjectChangeAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,47 @@ export class ProjectChangeAnalyzer {
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
const isTargetBranchACommit: boolean = await this._git.determineIfRefIsACommitAsync(targetBranchName);
const mergeCommit: string = isTargetBranchACommit
? targetBranchName
: await this._git.getMergeBaseAsync(targetBranchName, terminal, shouldFetch);

const repoChanges: Map<string, IFileDiffStatus> = getRepoChanges(repoRoot, mergeCommit, gitPath);
const changedFiles: Map<string, IFileDiffStatus> = getRepoChanges(repoRoot, mergeCommit, gitPath);
const lookup: LookupByPath<RushConfigurationProject> =
rushConfiguration.getProjectLookupForRoot(repoRoot);
const changesByProject: Map<
RushConfigurationProject,
Map<string, IFileDiffStatus>
> = this.getChangesByProject(lookup, changedFiles);

const changedProjects: Set<RushConfigurationProject> = new Set();
if (enableFiltering) {
// Reading rush-project.json may be problematic if, e.g. rush install has not yet occurred and rigs are in use
await Async.forEachAsync(
changesByProject,
async ([project, projectChanges]) => {
const filteredChanges: Map<string, IFileDiffStatus> = await this._filterProjectDataAsync(
project,
projectChanges,
repoRoot,
terminal
);

if (filteredChanges.size > 0) {
changedProjects.add(project);
}
},
{ concurrency: 10 }
);
} else {
for (const [project, projectChanges] of changesByProject) {
if (projectChanges.size > 0) {
changedProjects.add(project);
}
}
}

// 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
Expand All @@ -246,7 +278,7 @@ export class ProjectChangeAnalyzer {
const relativeShrinkwrapFilePath: string = Path.convertToSlashes(
path.relative(repoRoot, fullShrinkwrapPath)
);
const shrinkwrapStatus: IFileDiffStatus | undefined = repoChanges.get(relativeShrinkwrapFilePath);
const shrinkwrapStatus: IFileDiffStatus | undefined = changedFiles.get(relativeShrinkwrapFilePath);

if (shrinkwrapStatus) {
if (shrinkwrapStatus.status !== 'M') {
Expand Down Expand Up @@ -289,54 +321,16 @@ export class ProjectChangeAnalyzer {
}
}

const changesByProject: Map<RushConfigurationProject, Map<string, IFileDiffStatus>> = new Map();
const lookup: LookupByPath<RushConfigurationProject> =
rushConfiguration.getProjectLookupForRoot(repoRoot);

for (const [file, diffStatus] of repoChanges) {
const project: RushConfigurationProject | undefined = lookup.findChildPath(file);
if (project) {
if (changedProjects.has(project)) {
// Lockfile changes cannot be ignored via rush-project.json
continue;
}

if (enableFiltering) {
let projectChanges: Map<string, IFileDiffStatus> | undefined = changesByProject.get(project);
if (!projectChanges) {
projectChanges = new Map();
changesByProject.set(project, projectChanges);
}
projectChanges.set(file, diffStatus);
} else {
changedProjects.add(project);
}
}
}

if (enableFiltering) {
// Reading rush-project.json may be problematic if, e.g. rush install has not yet occurred and rigs are in use
await Async.forEachAsync(
changesByProject,
async ([project, projectChanges]) => {
const filteredChanges: Map<string, IFileDiffStatus> = await this._filterProjectDataAsync(
project,
projectChanges,
repoRoot,
terminal
);

if (filteredChanges.size > 0) {
changedProjects.add(project);
}
},
{ concurrency: 10 }
);
}

return changedProjects;
}

protected getChangesByProject(
lookup: LookupByPath<RushConfigurationProject>,
changedFiles: Map<string, IFileDiffStatus>
): Map<RushConfigurationProject, Map<string, IFileDiffStatus>> {
return lookup.groupByChild(changedFiles);
}

private async _getDataAsync(terminal: ITerminal): Promise<IRawRepoState> {
const repoState: IGitState | undefined = await this._getRepoDepsAsync(terminal);
if (!repoState) {
Expand Down
1 change: 1 addition & 0 deletions libraries/rush-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"dependencies": {
"@rushstack/lookup-by-path": "workspace:*",
"@rushstack/node-core-library": "workspace:*",
"@rushstack/package-deps-hash": "workspace:*",
"@rushstack/terminal": "workspace:*",
"@types/node-fetch": "2.6.2",
"tapable": "2.2.1"
Expand Down
Loading