Skip to content

Commit

Permalink
Use fs.realpathSync.native when available (#41292)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
amcasey committed Dec 18, 2020
1 parent e789cb1 commit 902fcb0
Show file tree
Hide file tree
Showing 18 changed files with 2,725 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/compiler/sys.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -1749,7 +1751,7 @@ namespace ts {

function realpath(path: string): string {
try {
return _fs.realpathSync(path);
return realpathSync(path);
}
catch {
return path;
Expand Down
5 changes: 5 additions & 0 deletions src/harness/collectionsImpl.ts
Expand Up @@ -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) {
Expand Down
8 changes: 6 additions & 2 deletions src/harness/vfsUtil.ts
Expand Up @@ -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 };
}
Expand Down
7 changes: 4 additions & 3 deletions src/harness/virtualFileSystemWithWatch.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
145 changes: 144 additions & 1 deletion src/testRunner/unittests/tscWatch/forceConsistentCasingInFileNames.ts
Expand Up @@ -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`);
});
}
Expand Up @@ -33,27 +33,27 @@ export const Fragment: unique symbol;
export const App = () => <div propA={true}></div>;

//// [/user/username/projects/myproject/tsconfig.json]
{"compilerOptions":{"jsx":"react-jsx","jsxImportSource":"react","forceConsistentCasingInFileNames":true},"files":["node_modules/react/Jsx-runtime/index.d.ts","index.tsx"]}
{"compilerOptions":{"jsx":"react-jsx","jsxImportSource":"react","forceConsistentCasingInFileNames":true},"files":["node_modules/react/jsx-Runtime/index.d.ts","index.tsx"]}


/a/lib/tsc.js --w --p . --explainFiles
Output::
>> Screen clear
[12:00:31 AM] Starting compilation in watch mode...

[91merror[0m[90m TS1149: [0mFile name '/user/username/projects/myproject/node_modules/react/jsx-runtime/index.d.ts' differs from already included file name '/user/username/projects/myproject/node_modules/react/Jsx-runtime/index.d.ts' only in casing.
[91merror[0m[90m TS1149: [0mFile name '/user/username/projects/myproject/node_modules/react/Jsx-runtime/index.d.ts' differs from already included file name '/user/username/projects/myproject/node_modules/react/jsx-Runtime/index.d.ts' only in casing.
The file is in the program because:
Part of 'files' list in tsconfig.json
Imported via "react/jsx-runtime" from file '/user/username/projects/myproject/index.tsx' with packageId 'react/jsx-runtime/index.d.ts@0.0.1' to import 'jsx' and 'jsxs' factory functions

tsconfig.json:1:115
[7m1[0m {"compilerOptions":{"jsx":"react-jsx","jsxImportSource":"react","forceConsistentCasingInFileNames":true},"files":["node_modules/react/Jsx-runtime/index.d.ts","index.tsx"]}
[7m1[0m {"compilerOptions":{"jsx":"react-jsx","jsxImportSource":"react","forceConsistentCasingInFileNames":true},"files":["node_modules/react/jsx-Runtime/index.d.ts","index.tsx"]}
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
File is matched by 'files' list specified here.

../../../../a/lib/lib.d.ts
Default library
node_modules/react/Jsx-runtime/index.d.ts
node_modules/react/jsx-Runtime/index.d.ts
Part of 'files' list in tsconfig.json
Imported via "react/jsx-runtime" from file 'index.tsx' with packageId 'react/jsx-runtime/index.d.ts@0.0.1' to import 'jsx' and 'jsxs' factory functions
index.tsx
Expand All @@ -62,12 +62,12 @@ index.tsx



Program root files: ["/user/username/projects/myproject/node_modules/react/Jsx-runtime/index.d.ts","/user/username/projects/myproject/index.tsx"]
Program root files: ["/user/username/projects/myproject/node_modules/react/jsx-Runtime/index.d.ts","/user/username/projects/myproject/index.tsx"]
Program options: {"jsx":4,"jsxImportSource":"react","forceConsistentCasingInFileNames":true,"watch":true,"project":"/user/username/projects/myproject","explainFiles":true,"configFilePath":"/user/username/projects/myproject/tsconfig.json"}
Program structureReused: Not
Program files::
/a/lib/lib.d.ts
/user/username/projects/myproject/node_modules/react/Jsx-runtime/index.d.ts
/user/username/projects/myproject/node_modules/react/jsx-Runtime/index.d.ts
/user/username/projects/myproject/index.tsx

No cached semantic diagnostics in the builder::
Expand All @@ -76,7 +76,7 @@ WatchedFiles::
/user/username/projects/myproject/tsconfig.json:
{"fileName":"/user/username/projects/myproject/tsconfig.json","pollingInterval":250}
/user/username/projects/myproject/node_modules/react/jsx-runtime/index.d.ts:
{"fileName":"/user/username/projects/myproject/node_modules/react/Jsx-runtime/index.d.ts","pollingInterval":250}
{"fileName":"/user/username/projects/myproject/node_modules/react/jsx-Runtime/index.d.ts","pollingInterval":250}
/user/username/projects/myproject/index.tsx:
{"fileName":"/user/username/projects/myproject/index.tsx","pollingInterval":250}
/a/lib/lib.d.ts:
Expand Down

0 comments on commit 902fcb0

Please sign in to comment.