Skip to content

Commit

Permalink
fix(core): Trailing slash in outputs array prevents correct caching
Browse files Browse the repository at this point in the history
Fix by removing trailing slash when performing `cp -a` into cache.

Add test to confirm fix.

Resolves nrwl#10549
  • Loading branch information
danielsharvey committed Jun 11, 2022
1 parent 9721844 commit 3ea53d9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
37 changes: 37 additions & 0 deletions e2e/workspace-core/src/run-commands.test.ts
Expand Up @@ -5,6 +5,7 @@ import {
uniq,
updateFile,
updateProjectConfig,
checkFilesExist,
} from '@nrwl/e2e/utils';

describe('Run Commands', () => {
Expand Down Expand Up @@ -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);
});
4 changes: 4 additions & 0 deletions packages/nx/src/tasks-runner/cache.ts
Expand Up @@ -181,6 +181,10 @@ export class Cache {
}

private copy(src: string, directory: string): Promise<void> {
// '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);
}
Expand Down

0 comments on commit 3ea53d9

Please sign in to comment.