Skip to content

Commit

Permalink
fix(core): remove trailing slash in outputs that prevents correct cac…
Browse files Browse the repository at this point in the history
…hing (#10708)

Fix by removing trailing slash when performing `cp -a` into cache.

Add test to confirm fix.

Resolves #10549

(cherry picked from commit 2889372)
  • Loading branch information
danielsharvey authored and FrozenPandaz committed Oct 3, 2022
1 parent 2f2c7d8 commit c3f853a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
42 changes: 42 additions & 0 deletions e2e/nx-misc/src/extras.test.ts
@@ -1,4 +1,5 @@
import {
checkFilesExist,
cleanupProject,
isNotWindows,
newProject,
Expand Down Expand Up @@ -221,5 +222,46 @@ describe('Extra Nx Misc Tests', () => {
})
).not.toThrow();
}, 1000000);

it('should handle caching output directories containing trailing slashes', async () => {
// this test relates to https://github.com/nrwl/nx/issues/10549
// 'cp -a /path/dir/ dest/' operates differently to 'cp -a /path/dir dest/'
// --> which means actual build works but subsequent populate from cache (using cp -a) does not
// --> the fix is to remove trailing slashes to ensure consistent & expected behaviour

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);
});
});
5 changes: 5 additions & 0 deletions packages/nx/src/tasks-runner/cache.ts
Expand Up @@ -181,6 +181,11 @@ export class Cache {
}

private async copy(src: string, destination: string): Promise<void> {
// 'cp -a /path/dir/ dest/' operates differently to 'cp -a /path/dir dest/'
// --> which means actual build works but subsequent populate from cache (using cp -a) does not
// --> the fix is to remove trailing slashes to ensure consistent & expected behaviour
src = src.replace(/[\/\\]$/, '');

if (this.useFsExtraToCopyAndRemove) {
return copy(src, destination);
}
Expand Down

0 comments on commit c3f853a

Please sign in to comment.