Use fs.realpathSync.native when available (#41292)

* Test that forceConsistentCasingInFileNames does not apply to Windows drive roots

* Add file symlink baselines

* Add directory symlink baselines

* Update test to retain its meaning

Its purpose is (apparently) to demonstrate that
forceConsistenCasingInFileNames can interact badly with synthesized
react imports.  Since the casing of the synthesized import has changed,
also modify the casing of the explicit reference to still conflict.

* Make VFSWithWatch.realpath use the path on disk

* Update VFS realpathSync to behave like realpathSync.native

* Use fs.realpathSync.native when available

In local measurements of an Office project, we saw initial project
loading get 5% faster on Windows and 13% faster on Linux.  The only
identified behavioral change is that it restores the case used on disk,
whereas realpathSync retains the input lowercase.

* Rename SortedMap.getKeyAndValue to getEntry
This commit is contained in:
Andrew Casey
2020-12-18 09:23:42 -08:00
committed by GitHub
parent e789cb1356
commit 902fcb0cc7
18 changed files with 2725 additions and 14 deletions

View File

@@ -1185,6 +1185,8 @@ namespace ts {
let activeSession: import("inspector").Session | "stopping" | undefined;
let profilePath = "./profile.cpuprofile";
const realpathSync = _fs.realpathSync.native ?? _fs.realpathSync;
const Buffer: {
new (input: string, encoding?: string): any;
from?(input: string, encoding?: string): any;
@@ -1749,7 +1751,7 @@ namespace ts {
function realpath(path: string): string {
try {
return _fs.realpathSync(path);
return realpathSync(path);
}
catch {
return path;

View File

@@ -50,6 +50,11 @@ namespace collections {
return index >= 0 ? this._values[index] : undefined;
}
public getEntry(key: K): [ K, V ] | undefined {
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
return index >= 0 ? [ this._keys[index], this._values[index] ] : undefined;
}
public set(key: K, value: V) {
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
if (index >= 0) {

View File

@@ -1036,8 +1036,12 @@ namespace vfs {
while (true) {
if (depth >= 40) throw createIOError("ELOOP");
const lastStep = step === components.length - 1;
const basename = components[step];
const node = links.get(basename);
let basename = components[step];
const linkEntry = links.getEntry(basename);
if (linkEntry) {
components[step] = basename = linkEntry[0];
}
const node = linkEntry?.[1];
if (lastStep && (noFollow || !isSymlink(node))) {
return { realpath: vpath.format(components), basename, parent, links, node };
}

View File

@@ -828,9 +828,9 @@ interface Array<T> { length: number; [n: number]: T; }`
return undefined;
}
const realpath = this.realpath(path);
const realpath = this.toPath(this.realpath(path));
if (path !== realpath) {
return this.getRealFsEntry(isFsEntry, this.toPath(realpath));
return this.getRealFsEntry(isFsEntry, realpath);
}
return undefined;
@@ -1097,7 +1097,8 @@ interface Array<T> { length: number; [n: number]: T; }`
return this.realpath(fsEntry.symLink);
}
return realFullPath;
// realpath supports non-existent files, so there may not be an fsEntry
return fsEntry?.fullPath || realFullPath;
}
readonly exitMessage = "System Exit";

View File

@@ -113,11 +113,154 @@ export const Fragment: unique symbol;
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { jsx: "react-jsx", jsxImportSource: "react", forceConsistentCasingInFileNames: true },
files: ["node_modules/react/Jsx-runtime/index.d.ts", "index.tsx"]
files: ["node_modules/react/jsx-Runtime/index.d.ts", "index.tsx"] // NB: casing does not match disk
})
}
], { currentDirectory: projectRoot }),
changes: emptyArray,
});
function verifyWindowsStyleRoot(subScenario: string, windowsStyleRoot: string, projectRootRelative: string) {
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario,
commandLineArgs: ["--w", "--p", `${windowsStyleRoot}/${projectRootRelative}`, "--explainFiles"],
sys: () => {
const moduleA: File = {
path: `${windowsStyleRoot}/${projectRootRelative}/a.ts`,
content: `
export const a = 1;
export const b = 2;
`
};
const moduleB: File = {
path: `${windowsStyleRoot}/${projectRootRelative}/b.ts`,
content: `
import { a } from "${windowsStyleRoot.toLocaleUpperCase()}/${projectRootRelative}/a"
import { b } from "${windowsStyleRoot.toLocaleLowerCase()}/${projectRootRelative}/a"
a;b;
`
};
const tsconfig: File = {
path: `${windowsStyleRoot}/${projectRootRelative}/tsconfig.json`,
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true } })
};
return createWatchedSystem([moduleA, moduleB, libFile, tsconfig], { windowsStyleRoot, useCaseSensitiveFileNames: false });
},
changes: [
{
caption: "Prepend a line to moduleA",
change: sys => sys.prependFile(`${windowsStyleRoot}/${projectRootRelative}/a.ts`, `// some comment
`),
timeouts: runQueuedTimeoutCallbacks,
}
],
});
}
verifyWindowsStyleRoot("when Windows-style drive root is lowercase", "c:/", "project");
verifyWindowsStyleRoot("when Windows-style drive root is uppercase", "C:/", "project");
function verifyFileSymlink(subScenario: string, diskPath: string, targetPath: string, importedPath: string) {
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario,
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
sys: () => {
const moduleA: File = {
path: diskPath,
content: `
export const a = 1;
export const b = 2;
`
};
const symlinkA: SymLink = {
path: `${projectRoot}/link.ts`,
symLink: targetPath,
};
const moduleB: File = {
path: `${projectRoot}/b.ts`,
content: `
import { a } from "${importedPath}";
import { b } from "./link";
a;b;
`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true } })
};
return createWatchedSystem([moduleA, symlinkA, moduleB, libFile, tsconfig], { currentDirectory: projectRoot });
},
changes: [
{
caption: "Prepend a line to moduleA",
change: sys => sys.prependFile(diskPath, `// some comment
`),
timeouts: runQueuedTimeoutCallbacks,
}
],
});
}
verifyFileSymlink("when both file symlink target and import match disk", `${projectRoot}/XY.ts`, `${projectRoot}/XY.ts`, `./XY`);
verifyFileSymlink("when file symlink target matches disk but import does not", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./XY`);
verifyFileSymlink("when import matches disk but file symlink target does not", `${projectRoot}/XY.ts`, `${projectRoot}/XY.ts`, `./Xy`);
verifyFileSymlink("when import and file symlink target agree but do not match disk", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./Xy`);
verifyFileSymlink("when import, file symlink target, and disk are all different", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./yX`);
function verifyDirSymlink(subScenario: string, diskPath: string, targetPath: string, importedPath: string) {
verifyTscWatch({
scenario: "forceConsistentCasingInFileNames",
subScenario,
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
sys: () => {
const moduleA: File = {
path: `${diskPath}/a.ts`,
content: `
export const a = 1;
export const b = 2;
`
};
const symlinkA: SymLink = {
path: `${projectRoot}/link`,
symLink: targetPath,
};
const moduleB: File = {
path: `${projectRoot}/b.ts`,
content: `
import { a } from "${importedPath}/a";
import { b } from "./link/a";
a;b;
`
};
const tsconfig: File = {
path: `${projectRoot}/tsconfig.json`,
// Use outFile because otherwise the real and linked files will have the same output path
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true, outFile: "out.js", module: "system" } })
};
return createWatchedSystem([moduleA, symlinkA, moduleB, libFile, tsconfig], { currentDirectory: projectRoot });
},
changes: [
{
caption: "Prepend a line to moduleA",
change: sys => sys.prependFile(`${diskPath}/a.ts`, `// some comment
`),
timeouts: runQueuedTimeoutCallbacks,
}
],
});
}
verifyDirSymlink("when both directory symlink target and import match disk", `${projectRoot}/XY`, `${projectRoot}/XY`, `./XY`);
verifyDirSymlink("when directory symlink target matches disk but import does not", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./XY`);
verifyDirSymlink("when import matches disk but directory symlink target does not", `${projectRoot}/XY`, `${projectRoot}/XY`, `./Xy`);
verifyDirSymlink("when import and directory symlink target agree but do not match disk", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./Xy`);
verifyDirSymlink("when import, directory symlink target, and disk are all different", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./yX`);
});
}