diff --git a/doc/api/fs.md b/doc/api/fs.md index fb4a625b863240..c8fa23cf7c4228 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3580,6 +3580,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} @@ -5328,6 +5333,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} diff --git a/lib/fs.js b/lib/fs.js index abdc2f0c39c11b..9884e85d5845ee 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1185,6 +1185,7 @@ function rm(path, options, callback) { callback = options; options = undefined; } + path = getValidatedPath(path); validateRmOptions(path, options, false, (err, options) => { if (err) { @@ -1208,6 +1209,7 @@ function rm(path, options, callback) { * @returns {void} */ function rmSync(path, options) { + path = getValidatedPath(path); options = validateRmOptionsSync(path, options, false); lazyLoadRimraf(); diff --git a/test/parallel/test-fs-rm.js b/test/parallel/test-fs-rm.js index 3fdfc8426248ac..5b30599189dec4 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -5,6 +5,7 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const fs = require('fs'); const path = require('path'); +const { pathToFileURL } = require('url'); const { execSync } = require('child_process'); const { validateRmOptionsSync } = require('internal/fs/utils'); @@ -97,6 +98,11 @@ function removeAsync(dir) { makeNonEmptyDirectory(2, 10, 2, dir, false); removeAsync(dir); + // Same test using URL instead of a path + dir = nextDirPath(); + makeNonEmptyDirectory(2, 10, 2, dir, false); + removeAsync(pathToFileURL(dir)); + // Create a flat folder including symlinks dir = nextDirPath(); makeNonEmptyDirectory(1, 10, 2, dir, true); @@ -156,6 +162,16 @@ function removeAsync(dir) { fs.rmSync(filePath, { force: true }); } + // Should accept URL + const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-file.txt')); + fs.writeFileSync(fileURL, ''); + + try { + fs.rmSync(fileURL, { recursive: true }); + } finally { + fs.rmSync(fileURL, { force: true }); + } + // Recursive removal should succeed. fs.rmSync(dir, { recursive: true }); @@ -202,6 +218,16 @@ function removeAsync(dir) { } finally { fs.rmSync(filePath, { force: true }); } + + // Should accept URL + const fileURL = pathToFileURL(path.join(tmpdir.path, 'rm-promises-file.txt')); + fs.writeFileSync(fileURL, ''); + + try { + await fs.promises.rm(fileURL, { recursive: true }); + } finally { + fs.rmSync(fileURL, { force: true }); + } })().then(common.mustCall()); // Test input validation.