fix(formatter): honor equivalent Windows home paths

This commit is contained in:
Afloat committed 2026-11-17 14:04:49 +08:00
1 parent 69bbf04bc7
commit e9300358da
2 files changed
+24 -1

No files matched your search

+1 -1
View File
@@ -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;
+23
View File
@@ -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();