From 0cf3629b9b4d1d514c85236426ca085b94d47c4f Mon Sep 17 00:00:00 2001 From: Tushar Singh Date: Sun, 14 May 2023 21:12:27 +0530 Subject: [PATCH] call mkdirP also when drive doesn't exist --- packages/io/__tests__/io.test.ts | 13 ++++++------- packages/io/src/io.ts | 19 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/io/__tests__/io.test.ts b/packages/io/__tests__/io.test.ts index 82a3d402..c6a6afff 100644 --- a/packages/io/__tests__/io.test.ts +++ b/packages/io/__tests__/io.test.ts @@ -867,17 +867,16 @@ describe('mkdirP', () => { if (ioUtil.IS_WINDOWS) { it('show error only if Windows drive does not exist', async () => { - let errMsg: string - const driveRoot = path.parse(getTestTemp()).root - // Assuming 'A:\' is not a common Windows drive lets us test for a non-existing drive - for (const testPath of [driveRoot, 'A:\\']) { + const root = path.parse(getTestTemp()).root + // Assuming A:\ isn't a common Windows drive letter lets us test for a non-existing drive + const testPaths = [root, 'A:\\'] + for (const testPath of testPaths) { if (await ioUtil.exists(testPath)) { await io.mkdirP(testPath) } else { - errMsg = `Drive '${testPath}' does not exist.` await expect(io.mkdirP(testPath)).rejects.toHaveProperty( - 'message', - errMsg + 'code', + 'ENOENT' ) } } diff --git a/packages/io/src/io.ts b/packages/io/src/io.ts index 4b5a6ba5..b36ec626 100644 --- a/packages/io/src/io.ts +++ b/packages/io/src/io.ts @@ -141,17 +141,16 @@ export async function rmRF(inputPath: string): Promise { */ export async function mkdirP(fsPath: string): Promise { ok(fsPath, 'a path argument must be provided') - // mkdirP would throw EPERM error if `fsPath` is a Windows drive root dir, e.g. 'C:\' - // even when it exists. This aligns with node's fs.mkdir impl. However, mkdirP shouldn't - // throw errors if `fsPath` exists regardless of the underlying platform. - // In this case, we will only check if drive exists and throw an error if it doesn't. - if (ioUtil.IS_WINDOWS && path.parse(fsPath).root === fsPath) { - if (!(await ioUtil.exists(fsPath))) { - throw new Error(`Drive '${fsPath}' does not exist.`) - } - } else { - await ioUtil.mkdir(fsPath, {recursive: true}) + // don't call mkdirP for root paths (e.g. C:\) on Windows. This leads to + // 'EPERM: operation not permitted' error when drive already exists. + if ( + ioUtil.IS_WINDOWS && + path.parse(fsPath).root === fsPath && + (await ioUtil.exists(fsPath)) + ) { + return } + await ioUtil.mkdir(fsPath, {recursive: true}) } /**