Skip to content
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

fix(core): empty exclude pattern should not result in error #16645

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions packages/nx/src/utils/find-matching-projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ describe('findMatchingProjects', () => {
},
};

it('should return no projects when passed no patterns', () => {
expect(findMatchingProjects([], projectGraph)).toEqual([]);
});

it('should return no projects when passed empty string', () => {
expect(findMatchingProjects([''], projectGraph)).toEqual([]);
});

it('should not throw when a pattern is empty string', () => {
expect(findMatchingProjects(['', 'a'], projectGraph)).toEqual(['a']);
});

it('should expand "*"', () => {
expect(findMatchingProjects(['*'], projectGraph)).toEqual([
'test-project',
Expand Down
15 changes: 13 additions & 2 deletions packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,20 @@ export function findMatchingProjects(
patterns: string[] = [],
projects: ProjectNodeMap
): string[] {
if (!patterns.length || patterns.filter((p) => p.length).length === 0) {
return []; // Short circuit if called with no patterns
}

const projectNames = keys(projects);

const selectedProjects: Set<string> = new Set();
const excludedProjects: Set<string> = new Set();

for (const stringPattern of patterns) {
if (!stringPattern.length) {
continue;
}

const pattern = parseStringPattern(stringPattern, projects);

// Handle wildcard with short-circuit, as its a common case with potentially
Expand Down Expand Up @@ -254,16 +262,19 @@ function isValidPatternType(type: string): type is ProjectPatternType {
export const getMatchingStringsWithCache = (() => {
// Map< Pattern, Map< Item, Result >>
const minimatchCache = new Map<string, Map<string, boolean>>();
const regexCache = new Map<string, RegExp>();
return (pattern: string, items: string[]) => {
if (!minimatchCache.has(pattern)) {
minimatchCache.set(pattern, new Map());
}
const patternCache = minimatchCache.get(pattern)!;
let matcher = null;
if (!regexCache.has(pattern)) {
regexCache.set(pattern, minimatch.makeRe(pattern));
}
const matcher = regexCache.get(pattern);
return items.filter((item) => {
let entry = patternCache.get(item);
if (entry === undefined || entry === null) {
matcher ??= minimatch.makeRe(pattern);
entry = item === pattern ? true : matcher.test(item);
patternCache.set(item, entry);
}
Expand Down