diff --git a/doc/api/fs.md b/doc/api/fs.md index 1a5090d6c8b8ed..b8d1cfc7dd20a7 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3519,6 +3519,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} @@ -5261,6 +5266,11 @@ with options `{ recursive: true, force: true }`. * `path` {string|Buffer|URL} diff --git a/lib/fs.js b/lib/fs.js index e61cb744a6ed51..76cf6fb88eca5a 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 5bb5d2de553eee..b1ef509a4e6ebf 100644 --- a/test/parallel/test-fs-rm.js +++ b/test/parallel/test-fs-rm.js @@ -5,6 +5,8 @@ const tmpdir = require('../common/tmpdir'); const assert = require('assert'); const fs = require('fs'); const path = require('path'); +const { pathToFileURL } = require('url'); + const { validateRmOptionsSync } = require('internal/fs/utils'); tmpdir.refresh(); @@ -95,6 +97,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); @@ -154,6 +161,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 }); @@ -200,6 +217,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.