From a3c7f8e5766fef01a30711ddcc4d5f1f7bb2e8c2 Mon Sep 17 00:00:00 2001 From: bcoe Date: Thu, 8 Oct 2020 17:17:01 -0700 Subject: [PATCH] fs: rimraf should not recurse on failure PR-URL: https://github.com/nodejs/node/pull/35566 Reviewed-By: Colin Ihrig Reviewed-By: Rich Trott --- lib/internal/fs/rimraf.js | 5 ++-- test/known_issues/known_issues.status | 3 +-- .../test-fs-open-no-close.js | 7 ------ test/parallel/test-fs-rmdir-recursive.js | 25 +++++++++++++++++++ 4 files changed, 29 insertions(+), 11 deletions(-) rename test/{known_issues => parallel}/test-fs-open-no-close.js (62%) diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index e88dd9697b1938..e67b14a418c884 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -12,6 +12,7 @@ const { } = primordials; const { Buffer } = require('buffer'); +const fs = require('fs'); const { chmod, chmodSync, @@ -25,7 +26,7 @@ const { statSync, unlink, unlinkSync -} = require('fs'); +} = fs; const { sep } = require('path'); const { setTimeout } = require('timers'); const { sleep } = require('internal/util'); @@ -249,7 +250,7 @@ function _rmdirSync(path, options, originalErr) { for (let i = 1; i <= tries; i++) { try { - return rmdirSync(path, options); + return fs.rmdirSync(path); } catch (err) { // Only sleep if this is not the last try, and the delay is greater // than zero, and an error was encountered that warrants a retry. diff --git a/test/known_issues/known_issues.status b/test/known_issues/known_issues.status index 01a82246c93fcb..e0f0a456089bf2 100644 --- a/test/known_issues/known_issues.status +++ b/test/known_issues/known_issues.status @@ -1,3 +1,4 @@ + prefix known_issues # If a known issue does not apply to a platform, list the test name in the @@ -27,5 +28,3 @@ test-vm-timeout-escape-queuemicrotask: SKIP # The Raspberry Pis are too slow to run this test. # See https://github.com/nodejs/build/issues/2227#issuecomment-608334574 test-crypto-authenticated-stream: SKIP -# The bug being checked is that the test never exits. -test-fs-open-no-close: TIMEOUT diff --git a/test/known_issues/test-fs-open-no-close.js b/test/parallel/test-fs-open-no-close.js similarity index 62% rename from test/known_issues/test-fs-open-no-close.js rename to test/parallel/test-fs-open-no-close.js index ef990d1a67df83..5e432dd11d8084 100644 --- a/test/known_issues/test-fs-open-no-close.js +++ b/test/parallel/test-fs-open-no-close.js @@ -4,14 +4,7 @@ // Failing to close a file should not keep the event loop open. const common = require('../common'); - -// This issue only shows up on Raspberry Pi devices in our CI. When this test is -// moved out of known_issues, this check can be removed, as the test should pass -// on all platforms at that point. const assert = require('assert'); -if (process.arch !== 'arm' || process.config.variables.arm_version > 7) { - assert.fail('This test is for Raspberry Pi devices in CI'); -} const fs = require('fs'); diff --git a/test/parallel/test-fs-rmdir-recursive.js b/test/parallel/test-fs-rmdir-recursive.js index 95607333d5540c..bbae993a008522 100644 --- a/test/parallel/test-fs-rmdir-recursive.js +++ b/test/parallel/test-fs-rmdir-recursive.js @@ -211,3 +211,28 @@ function removeAsync(dir) { message: /^The value of "options\.maxRetries" is out of range\./ }); } + +// It should not pass recursive option to rmdirSync, when called from +// rimraf (see: #35566) +{ + // Make a non-empty directory: + const original = fs.rmdirSync; + const dir = `${nextDirPath()}/foo/bar`; + fs.mkdirSync(dir, { recursive: true }); + fs.writeFileSync(`${dir}/foo.txt`, 'hello world', 'utf8'); + + // When called the second time from rimraf, the recursive option should + // not be set for rmdirSync: + let callCount = 0; + let rmdirSyncOptionsFromRimraf; + fs.rmdirSync = (path, options) => { + if (callCount > 0) { + rmdirSyncOptionsFromRimraf = { ...options }; + } + callCount++; + return original(path, options); + }; + fs.rmdirSync(dir, { recursive: true }); + fs.rmdirSync = original; + assert.strictEqual(rmdirSyncOptionsFromRimraf.recursive, undefined); +}