Skip to content

Commit

Permalink
fix(core): empty exclude pattern should not result in error (#16645)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Apr 28, 2023
1 parent e09c055 commit 2695e97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
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

0 comments on commit 2695e97

Please sign in to comment.