Skip to content

Commit 902fcb0

Browse files
authored
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
1 parent e789cb1 commit 902fcb0

18 files changed

+2725
-14
lines changed

Diff for: src/compiler/sys.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,8 @@ namespace ts {
11851185
let activeSession: import("inspector").Session | "stopping" | undefined;
11861186
let profilePath = "./profile.cpuprofile";
11871187

1188+
const realpathSync = _fs.realpathSync.native ?? _fs.realpathSync;
1189+
11881190
const Buffer: {
11891191
new (input: string, encoding?: string): any;
11901192
from?(input: string, encoding?: string): any;
@@ -1749,7 +1751,7 @@ namespace ts {
17491751

17501752
function realpath(path: string): string {
17511753
try {
1752-
return _fs.realpathSync(path);
1754+
return realpathSync(path);
17531755
}
17541756
catch {
17551757
return path;

Diff for: src/harness/collectionsImpl.ts

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ namespace collections {
5050
return index >= 0 ? this._values[index] : undefined;
5151
}
5252

53+
public getEntry(key: K): [ K, V ] | undefined {
54+
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
55+
return index >= 0 ? [ this._keys[index], this._values[index] ] : undefined;
56+
}
57+
5358
public set(key: K, value: V) {
5459
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
5560
if (index >= 0) {

Diff for: src/harness/vfsUtil.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,12 @@ namespace vfs {
10361036
while (true) {
10371037
if (depth >= 40) throw createIOError("ELOOP");
10381038
const lastStep = step === components.length - 1;
1039-
const basename = components[step];
1040-
const node = links.get(basename);
1039+
let basename = components[step];
1040+
const linkEntry = links.getEntry(basename);
1041+
if (linkEntry) {
1042+
components[step] = basename = linkEntry[0];
1043+
}
1044+
const node = linkEntry?.[1];
10411045
if (lastStep && (noFollow || !isSymlink(node))) {
10421046
return { realpath: vpath.format(components), basename, parent, links, node };
10431047
}

Diff for: src/harness/virtualFileSystemWithWatch.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -828,9 +828,9 @@ interface Array<T> { length: number; [n: number]: T; }`
828828
return undefined;
829829
}
830830

831-
const realpath = this.realpath(path);
831+
const realpath = this.toPath(this.realpath(path));
832832
if (path !== realpath) {
833-
return this.getRealFsEntry(isFsEntry, this.toPath(realpath));
833+
return this.getRealFsEntry(isFsEntry, realpath);
834834
}
835835

836836
return undefined;
@@ -1097,7 +1097,8 @@ interface Array<T> { length: number; [n: number]: T; }`
10971097
return this.realpath(fsEntry.symLink);
10981098
}
10991099

1100-
return realFullPath;
1100+
// realpath supports non-existent files, so there may not be an fsEntry
1101+
return fsEntry?.fullPath || realFullPath;
11011102
}
11021103

11031104
readonly exitMessage = "System Exit";

Diff for: src/testRunner/unittests/tscWatch/forceConsistentCasingInFileNames.ts

+144-1
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,154 @@ export const Fragment: unique symbol;
113113
path: `${projectRoot}/tsconfig.json`,
114114
content: JSON.stringify({
115115
compilerOptions: { jsx: "react-jsx", jsxImportSource: "react", forceConsistentCasingInFileNames: true },
116-
files: ["node_modules/react/Jsx-runtime/index.d.ts", "index.tsx"]
116+
files: ["node_modules/react/jsx-Runtime/index.d.ts", "index.tsx"] // NB: casing does not match disk
117117
})
118118
}
119119
], { currentDirectory: projectRoot }),
120120
changes: emptyArray,
121121
});
122+
123+
function verifyWindowsStyleRoot(subScenario: string, windowsStyleRoot: string, projectRootRelative: string) {
124+
verifyTscWatch({
125+
scenario: "forceConsistentCasingInFileNames",
126+
subScenario,
127+
commandLineArgs: ["--w", "--p", `${windowsStyleRoot}/${projectRootRelative}`, "--explainFiles"],
128+
sys: () => {
129+
const moduleA: File = {
130+
path: `${windowsStyleRoot}/${projectRootRelative}/a.ts`,
131+
content: `
132+
export const a = 1;
133+
export const b = 2;
134+
`
135+
};
136+
const moduleB: File = {
137+
path: `${windowsStyleRoot}/${projectRootRelative}/b.ts`,
138+
content: `
139+
import { a } from "${windowsStyleRoot.toLocaleUpperCase()}/${projectRootRelative}/a"
140+
import { b } from "${windowsStyleRoot.toLocaleLowerCase()}/${projectRootRelative}/a"
141+
142+
a;b;
143+
`
144+
};
145+
const tsconfig: File = {
146+
path: `${windowsStyleRoot}/${projectRootRelative}/tsconfig.json`,
147+
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true } })
148+
};
149+
return createWatchedSystem([moduleA, moduleB, libFile, tsconfig], { windowsStyleRoot, useCaseSensitiveFileNames: false });
150+
},
151+
changes: [
152+
{
153+
caption: "Prepend a line to moduleA",
154+
change: sys => sys.prependFile(`${windowsStyleRoot}/${projectRootRelative}/a.ts`, `// some comment
155+
`),
156+
timeouts: runQueuedTimeoutCallbacks,
157+
}
158+
],
159+
});
160+
}
161+
162+
verifyWindowsStyleRoot("when Windows-style drive root is lowercase", "c:/", "project");
163+
verifyWindowsStyleRoot("when Windows-style drive root is uppercase", "C:/", "project");
164+
165+
function verifyFileSymlink(subScenario: string, diskPath: string, targetPath: string, importedPath: string) {
166+
verifyTscWatch({
167+
scenario: "forceConsistentCasingInFileNames",
168+
subScenario,
169+
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
170+
sys: () => {
171+
const moduleA: File = {
172+
173+
path: diskPath,
174+
content: `
175+
export const a = 1;
176+
export const b = 2;
177+
`
178+
};
179+
const symlinkA: SymLink = {
180+
path: `${projectRoot}/link.ts`,
181+
symLink: targetPath,
182+
};
183+
const moduleB: File = {
184+
path: `${projectRoot}/b.ts`,
185+
content: `
186+
import { a } from "${importedPath}";
187+
import { b } from "./link";
188+
189+
a;b;
190+
`
191+
};
192+
const tsconfig: File = {
193+
path: `${projectRoot}/tsconfig.json`,
194+
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true } })
195+
};
196+
return createWatchedSystem([moduleA, symlinkA, moduleB, libFile, tsconfig], { currentDirectory: projectRoot });
197+
},
198+
changes: [
199+
{
200+
caption: "Prepend a line to moduleA",
201+
change: sys => sys.prependFile(diskPath, `// some comment
202+
`),
203+
timeouts: runQueuedTimeoutCallbacks,
204+
}
205+
],
206+
});
207+
}
208+
209+
verifyFileSymlink("when both file symlink target and import match disk", `${projectRoot}/XY.ts`, `${projectRoot}/XY.ts`, `./XY`);
210+
verifyFileSymlink("when file symlink target matches disk but import does not", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./XY`);
211+
verifyFileSymlink("when import matches disk but file symlink target does not", `${projectRoot}/XY.ts`, `${projectRoot}/XY.ts`, `./Xy`);
212+
verifyFileSymlink("when import and file symlink target agree but do not match disk", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./Xy`);
213+
verifyFileSymlink("when import, file symlink target, and disk are all different", `${projectRoot}/XY.ts`, `${projectRoot}/Xy.ts`, `./yX`);
214+
215+
function verifyDirSymlink(subScenario: string, diskPath: string, targetPath: string, importedPath: string) {
216+
verifyTscWatch({
217+
scenario: "forceConsistentCasingInFileNames",
218+
subScenario,
219+
commandLineArgs: ["--w", "--p", ".", "--explainFiles"],
220+
sys: () => {
221+
const moduleA: File = {
222+
223+
path: `${diskPath}/a.ts`,
224+
content: `
225+
export const a = 1;
226+
export const b = 2;
227+
`
228+
};
229+
const symlinkA: SymLink = {
230+
path: `${projectRoot}/link`,
231+
symLink: targetPath,
232+
};
233+
const moduleB: File = {
234+
path: `${projectRoot}/b.ts`,
235+
content: `
236+
import { a } from "${importedPath}/a";
237+
import { b } from "./link/a";
238+
239+
a;b;
240+
`
241+
};
242+
const tsconfig: File = {
243+
path: `${projectRoot}/tsconfig.json`,
244+
// Use outFile because otherwise the real and linked files will have the same output path
245+
content: JSON.stringify({ compilerOptions: { forceConsistentCasingInFileNames: true, outFile: "out.js", module: "system" } })
246+
};
247+
return createWatchedSystem([moduleA, symlinkA, moduleB, libFile, tsconfig], { currentDirectory: projectRoot });
248+
},
249+
changes: [
250+
{
251+
caption: "Prepend a line to moduleA",
252+
change: sys => sys.prependFile(`${diskPath}/a.ts`, `// some comment
253+
`),
254+
timeouts: runQueuedTimeoutCallbacks,
255+
}
256+
],
257+
});
258+
}
259+
260+
verifyDirSymlink("when both directory symlink target and import match disk", `${projectRoot}/XY`, `${projectRoot}/XY`, `./XY`);
261+
verifyDirSymlink("when directory symlink target matches disk but import does not", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./XY`);
262+
verifyDirSymlink("when import matches disk but directory symlink target does not", `${projectRoot}/XY`, `${projectRoot}/XY`, `./Xy`);
263+
verifyDirSymlink("when import and directory symlink target agree but do not match disk", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./Xy`);
264+
verifyDirSymlink("when import, directory symlink target, and disk are all different", `${projectRoot}/XY`, `${projectRoot}/Xy`, `./yX`);
122265
});
123266
}

Diff for: tests/baselines/reference/tscWatch/forceConsistentCasingInFileNames/jsxImportSource-option-changed.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,27 @@ export const Fragment: unique symbol;
3333
export const App = () => <div propA={true}></div>;
3434

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

3838

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

44-
[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.
44+
[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.
4545
The file is in the program because:
4646
Part of 'files' list in tsconfig.json
4747
Imported via "react/jsx-runtime" from file '/user/username/projects/myproject/index.tsx' with packageId 'react/jsx-runtime/[email protected]' to import 'jsx' and 'jsxs' factory functions
4848

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

5454
../../../../a/lib/lib.d.ts
5555
Default library
56-
node_modules/react/Jsx-runtime/index.d.ts
56+
node_modules/react/jsx-Runtime/index.d.ts
5757
Part of 'files' list in tsconfig.json
5858
Imported via "react/jsx-runtime" from file 'index.tsx' with packageId 'react/jsx-runtime/[email protected]' to import 'jsx' and 'jsxs' factory functions
5959
index.tsx
@@ -62,12 +62,12 @@ index.tsx
6262

6363

6464

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

7373
No cached semantic diagnostics in the builder::
@@ -76,7 +76,7 @@ WatchedFiles::
7676
/user/username/projects/myproject/tsconfig.json:
7777
{"fileName":"/user/username/projects/myproject/tsconfig.json","pollingInterval":250}
7878
/user/username/projects/myproject/node_modules/react/jsx-runtime/index.d.ts:
79-
{"fileName":"/user/username/projects/myproject/node_modules/react/Jsx-runtime/index.d.ts","pollingInterval":250}
79+
{"fileName":"/user/username/projects/myproject/node_modules/react/jsx-Runtime/index.d.ts","pollingInterval":250}
8080
/user/username/projects/myproject/index.tsx:
8181
{"fileName":"/user/username/projects/myproject/index.tsx","pollingInterval":250}
8282
/a/lib/lib.d.ts:

0 commit comments

Comments
 (0)