From db5b9d11bb88fe2aadc16c8a4db18c052dafc301 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Fri, 28 Apr 2023 13:09:02 -0400 Subject: [PATCH] fix(core): empty exclude pattern should not result in error --- .../nx/src/utils/find-matching-projects.spec.ts | 12 ++++++++++++ packages/nx/src/utils/find-matching-projects.ts | 15 +++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/nx/src/utils/find-matching-projects.spec.ts b/packages/nx/src/utils/find-matching-projects.spec.ts index 9db133323f417..41caa8e7ccb52 100644 --- a/packages/nx/src/utils/find-matching-projects.spec.ts +++ b/packages/nx/src/utils/find-matching-projects.spec.ts @@ -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', diff --git a/packages/nx/src/utils/find-matching-projects.ts b/packages/nx/src/utils/find-matching-projects.ts index 3b988e9836277..3e489401c1e2f 100644 --- a/packages/nx/src/utils/find-matching-projects.ts +++ b/packages/nx/src/utils/find-matching-projects.ts @@ -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 = new Set(); const excludedProjects: Set = 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 @@ -254,16 +262,19 @@ function isValidPatternType(type: string): type is ProjectPatternType { export const getMatchingStringsWithCache = (() => { // Map< Pattern, Map< Item, Result >> const minimatchCache = new Map>(); + const regexCache = new Map(); 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); }