From 40f9a1dfcc49507f28193d6049b08a246baa0ad7 Mon Sep 17 00:00:00 2001 From: Nico Jansen Date: Tue, 27 Apr 2021 23:08:27 +0200 Subject: [PATCH] fix(sandbox): make directory if not exists before symlinking node_modules (#2856) First, create the directory if doesn't exist _before_ creating the symlink to `node_modules` inside the sandbox directory. --- packages/core/src/utils/file-utils.ts | 3 ++- packages/core/test/unit/utils/file-utils.spec.ts | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/core/src/utils/file-utils.ts b/packages/core/src/utils/file-utils.ts index 4efe8b9aef..c13a87dc89 100644 --- a/packages/core/src/utils/file-utils.ts +++ b/packages/core/src/utils/file-utils.ts @@ -69,7 +69,8 @@ export function moveDirectoryRecursiveSync(from: string, to: string): void { * @param to The thing you want to point to * @param from The thing you want to point from */ -export function symlinkJunction(to: string, from: string): Promise { +export async function symlinkJunction(to: string, from: string): Promise { + await fs.promises.mkdir(path.dirname(from), { recursive: true }); return fs.promises.symlink(to, from, 'junction'); } diff --git a/packages/core/test/unit/utils/file-utils.spec.ts b/packages/core/test/unit/utils/file-utils.spec.ts index 76e405acf6..f0bd8c7bb1 100644 --- a/packages/core/test/unit/utils/file-utils.spec.ts +++ b/packages/core/test/unit/utils/file-utils.spec.ts @@ -21,6 +21,7 @@ describe('fileUtils', () => { beforeEach(() => { sinon.stub(fs.promises, 'writeFile'); sinon.stub(fs.promises, 'symlink'); + sinon.stub(fs.promises, 'mkdir'); readdirStub = sinon.stub(fs.promises, 'readdir'); }); @@ -29,6 +30,10 @@ describe('fileUtils', () => { await fileUtils.symlinkJunction('a', 'b'); expect(fs.promises.symlink).calledWith('a', 'b', 'junction'); }); + it("should create the dir if it doesn't exist", async () => { + await fileUtils.symlinkJunction('a', path.resolve('b', 'c')); + expect(fs.promises.mkdir).calledWith(path.resolve('b'), { recursive: true }); + }); }); describe('findNodeModulesList', () => {