diff --git a/scripts/lib/resolve-formatter.js b/scripts/lib/resolve-formatter.js index e0d40bc38..37e7ab8ea 100644 --- a/scripts/lib/resolve-formatter.js +++ b/scripts/lib/resolve-formatter.js @@ -72,7 +72,7 @@ function findProjectRoot(startDir) { while (dir !== path.dirname(dir)) { // Stop before checking the home directory to avoid treating global // dotfiles (e.g. ~/.prettierrc) as a project root marker. - if (dir === homeDir) break; + if (path.relative(homeDir, dir) === '') break; if (detectFormatter(dir)) { projectRootCache.set(startDir, dir); return dir; diff --git a/tests/lib/resolve-formatter.test.js b/tests/lib/resolve-formatter.test.js index 1cc77b9f2..e603ac0fc 100644 --- a/tests/lib/resolve-formatter.test.js +++ b/tests/lib/resolve-formatter.test.js @@ -8,6 +8,7 @@ const assert = require('assert'); const path = require('path'); const fs = require('fs'); const os = require('os'); +const vm = require('vm'); const { findProjectRoot, detectFormatter, resolveFormatterBin, clearCaches } = require('../../scripts/lib/resolve-formatter'); @@ -162,6 +163,28 @@ function runTests() { assert.strictEqual(findProjectRoot(workspace), workspace); }); + run('findProjectRoot: keeps the home boundary across Windows path casing', () => { + const source = fs.readFileSync(path.join(__dirname, '../../scripts/lib/resolve-formatter.js'), 'utf8'); + const existing = new Set([ + 'c:\\users\\alice\\.prettierrc', + 'c:\\users\\alice\\project\\package.json', + ]); + const mod = { exports: {} }; + const dependencies = { + path: path.win32, + os: { homedir: () => 'C:\\Users\\Alice' }, + fs: { + existsSync: file => existing.has(file.toLowerCase()), + readFileSync: () => '{}', + }, + }; + vm.runInNewContext(source, { module: mod, require: id => dependencies[id] }); + for (const project of ['c:\\Users\\Alice\\project', 'C:\\USERS\\ALICE\\project']) { + assert.strictEqual(mod.exports.findProjectRoot(path.win32.join(project, 'src')), project); + assert.strictEqual(mod.exports.detectFormatter(project), null); + } + }); + run('findProjectRoot: does not borrow a formatter from outside a repository', () => { for (const isWorktree of [false, true]) { const outer = makeTmpDir();