diff --git a/packages/nx/src/hasher/hasher.spec.ts b/packages/nx/src/hasher/hasher.spec.ts index 38454a34b5..938870be08 100644 --- a/packages/nx/src/hasher/hasher.spec.ts +++ b/packages/nx/src/hasher/hasher.spec.ts @@ -845,57 +845,72 @@ describe('Hasher', () => { describe('filterUsingGlobPatterns', () => { it('should OR all positive patterns and AND all negative patterns (when positive and negative patterns)', () => { const filtered = filterUsingGlobPatterns( - '/root', + 'root', [ - { file: '/root/a.ts' }, - { file: '/root/b.js' }, - { file: '/root/c.spec.ts' }, - { file: '/root/d.md' }, + { file: 'root/a.ts' }, + { file: 'root/b.js' }, + { file: 'root/c.spec.ts' }, + { file: 'root/d.md' }, ] as any, [ - '/root/**/*.ts', - '/root/**/*.js', - '!/root/**/*.spec.ts', - '!/root/**/*.md', + '{projectRoot}/**/*.ts', + '{projectRoot}/**/*.js', + '!{projectRoot}/**/*.spec.ts', + '!{projectRoot}/**/*.md', ] ); - expect(filtered.map((f) => f.file)).toEqual(['/root/a.ts', '/root/b.js']); + expect(filtered.map((f) => f.file)).toEqual(['root/a.ts', 'root/b.js']); }); it('should OR all positive patterns and AND all negative patterns (when negative patterns)', () => { const filtered = filterUsingGlobPatterns( - '/root', + 'root', [ - { file: '/root/a.ts' }, - { file: '/root/b.js' }, - { file: '/root/c.spec.ts' }, - { file: '/root/d.md' }, + { file: 'root/a.ts' }, + { file: 'root/b.js' }, + { file: 'root/c.spec.ts' }, + { file: 'root/d.md' }, ] as any, - ['!/root/**/*.spec.ts', '!/root/**/*.md'] + ['!{projectRoot}/**/*.spec.ts', '!{projectRoot}/**/*.md'] ); - expect(filtered.map((f) => f.file)).toEqual(['/root/a.ts', '/root/b.js']); + expect(filtered.map((f) => f.file)).toEqual(['root/a.ts', 'root/b.js']); }); it('should OR all positive patterns and AND all negative patterns (when positive patterns)', () => { const filtered = filterUsingGlobPatterns( - '/root', + 'root', [ - { file: '/root/a.ts' }, - { file: '/root/b.js' }, - { file: '/root/c.spec.ts' }, - { file: '/root/d.md' }, + { file: 'root/a.ts' }, + { file: 'root/b.js' }, + { file: 'root/c.spec.ts' }, + { file: 'root/d.md' }, ] as any, - ['/root/**/*.ts', '/root/**/*.js'] + ['{projectRoot}/**/*.ts', '{projectRoot}/**/*.js'] ); expect(filtered.map((f) => f.file)).toEqual([ - '/root/a.ts', - '/root/b.js', - '/root/c.spec.ts', + 'root/a.ts', + 'root/b.js', + 'root/c.spec.ts', ]); }); + + it('should handle projects with the root set to .', () => { + const filtered = filterUsingGlobPatterns( + '.', + [ + { file: 'a.ts' }, + { file: 'b.md' }, + { file: 'dir/a.ts' }, + { file: 'dir/b.md' }, + ] as any, + ['{projectRoot}/**/*.ts', '!{projectRoot}/**/*.md'] + ); + + expect(filtered.map((f) => f.file)).toEqual(['a.ts', 'dir/a.ts']); + }); }); }); diff --git a/packages/nx/src/hasher/hasher.ts b/packages/nx/src/hasher/hasher.ts index c5c69b2000..c0d29b6af4 100644 --- a/packages/nx/src/hasher/hasher.ts +++ b/packages/nx/src/hasher/hasher.ts @@ -489,13 +489,10 @@ class TaskHasher { if (!this.filesetHashes[mapKey]) { this.filesetHashes[mapKey] = new Promise(async (res) => { const p = this.projectGraph.nodes[projectName]; - const filesetWithExpandedProjectRoot = filesetPatterns.map((f) => - f.replace('{projectRoot}', p.data.root) - ); const filteredFiles = filterUsingGlobPatterns( p.data.root, p.data.files, - filesetWithExpandedProjectRoot + filesetPatterns ); const fileNames = filteredFiles.map((f) => f.file); const values = filteredFiles.map((f) => f.hash); @@ -640,13 +637,22 @@ export function expandNamedInput( } export function filterUsingGlobPatterns( - projectRoot: string, + root: string, files: FileData[], patterns: string[] ): FileData[] { + const filesetWithExpandedProjectRoot = patterns + .map((f) => f.replace('{projectRoot}', root)) + .map((r) => { + // handling root level projects that create './' pattern that doesn't work with minimatch + if (r.startsWith('./')) return r.substring(2); + if (r.startsWith('!./')) return '!' + r.substring(3); + return r; + }); + const positive = []; const negative = []; - for (const p of patterns) { + for (const p of filesetWithExpandedProjectRoot) { if (p.startsWith('!')) { negative.push(p); } else { @@ -662,7 +668,7 @@ export function filterUsingGlobPatterns( let matchedPositive = false; if ( positive.length === 0 || - (positive.length === 1 && positive[0] === `${projectRoot}/**/*`) + (positive.length === 1 && positive[0] === `${root}/**/*`) ) { matchedPositive = true; } else {