From 3ea53d921dcb89366c57e96ea991810248ea2b95 Mon Sep 17 00:00:00 2001 From: Daniel Harvey Date: Sat, 11 Jun 2022 21:50:04 +0800 Subject: [PATCH] fix(core): Trailing slash in outputs array prevents correct caching Fix by removing trailing slash when performing `cp -a` into cache. Add test to confirm fix. Resolves nrwl/nx#10549 --- e2e/workspace-core/src/run-commands.test.ts | 37 +++++++++++++++++++++ packages/nx/src/tasks-runner/cache.ts | 4 +++ 2 files changed, 41 insertions(+) diff --git a/e2e/workspace-core/src/run-commands.test.ts b/e2e/workspace-core/src/run-commands.test.ts index 26bebe1b8589be..5d39d8524992f0 100644 --- a/e2e/workspace-core/src/run-commands.test.ts +++ b/e2e/workspace-core/src/run-commands.test.ts @@ -5,6 +5,7 @@ import { uniq, updateFile, updateProjectConfig, + checkFilesExist, } from '@nrwl/e2e/utils'; describe('Run Commands', () => { @@ -143,4 +144,40 @@ describe('Run Commands', () => { }) ).not.toThrow(); }, 1000000); + + it('should handle caching output directories containing trailing slashes', async () => { + const mylib = uniq('lib'); + + const folder = `dist/libs/${mylib}/some-folder`; + + runCLI(`generate @nrwl/workspace:lib ${mylib}`); + + runCLI( + `generate @nrwl/workspace:run-commands build --command=echo --outputs=${folder}/ --project=${mylib}` + ); + + const commands = [ + process.platform === 'win32' + ? `mkdir ${folder}` // Windows + : `mkdir -p ${folder}`, + `echo dummy > ${folder}/dummy.txt`, + ]; + updateProjectConfig(mylib, (config) => { + delete config.targets.build.options.command; + config.targets.build.options = { + ...config.targets.build.options, + parallel: false, + commands: commands, + }; + return config; + }); + + // confirm that it builds correctly + runCLI(`build ${mylib}`); + checkFilesExist(`${folder}/dummy.txt`); + + // confirm that it populates correctly from the cache + runCLI(`build ${mylib}`); + checkFilesExist(`${folder}/dummy.txt`); + }, 120000); }); diff --git a/packages/nx/src/tasks-runner/cache.ts b/packages/nx/src/tasks-runner/cache.ts index c90b84b34bccee..7d15773c732300 100644 --- a/packages/nx/src/tasks-runner/cache.ts +++ b/packages/nx/src/tasks-runner/cache.ts @@ -181,6 +181,10 @@ export class Cache { } private copy(src: string, directory: string): Promise { + // 'cp -a /path/dir/ dest/' operates differently to 'cp -a /path/dir dest/' + // path.resolve() removes trailing slashes + src = resolve(src); + if (this.useFsExtraToCopyAndRemove) { return copy(src, directory); }