mirror of
https://github.com/Fission-AI/OpenSpec.git
synced 2026-10-02 05:24:34 +08:00
fix: prefer native realpath for canonical paths
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { promises as fs, constants as fsConstants, realpathSync } from 'fs';
|
||||
import * as nodeFs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const fs = nodeFs.promises;
|
||||
const { constants: fsConstants } = nodeFs;
|
||||
|
||||
function isMarkerOnOwnLine(content: string, markerIndex: number, markerLength: number): boolean {
|
||||
let leftIndex = markerIndex - 1;
|
||||
while (leftIndex >= 0 && content[leftIndex] !== '\n') {
|
||||
@@ -56,9 +59,14 @@ export class FileSystemUtils {
|
||||
*/
|
||||
static canonicalizeExistingPath(targetPath: string): string {
|
||||
try {
|
||||
return realpathSync(targetPath);
|
||||
// Prefer the native resolver so Windows short-path aliases are expanded.
|
||||
return nodeFs.realpathSync.native(targetPath);
|
||||
} catch {
|
||||
return path.resolve(targetPath);
|
||||
try {
|
||||
return nodeFs.realpathSync(targetPath);
|
||||
} catch {
|
||||
return path.resolve(targetPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
||||
import * as nodeFs from 'fs';
|
||||
import { promises as fs } from 'fs';
|
||||
import path from 'path';
|
||||
import os from 'os';
|
||||
@@ -92,6 +93,22 @@ describe('FileSystemUtils', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('canonicalizeExistingPath', () => {
|
||||
it('should prefer the native realpath resolver when available', async () => {
|
||||
const filePath = path.join(testDir, 'canonical.txt');
|
||||
await fs.writeFile(filePath, 'content');
|
||||
|
||||
const nativeSpy = vi.spyOn(nodeFs.realpathSync, 'native');
|
||||
|
||||
const resolved = FileSystemUtils.canonicalizeExistingPath(filePath);
|
||||
|
||||
expect(nativeSpy).toHaveBeenCalledWith(filePath);
|
||||
expect(resolved).toBe(nodeFs.realpathSync.native(filePath));
|
||||
|
||||
nativeSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('writeFile', () => {
|
||||
it('should write content to file', async () => {
|
||||
const filePath = path.join(testDir, 'output.txt');
|
||||
|
||||
Reference in New Issue
Block a user