Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fs.realpathSync.native when available #41292

Merged
merged 8 commits into from
Dec 18, 2020
4 changes: 3 additions & 1 deletion src/compiler/sys.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ namespace collections {
return index >= 0 ? this._values[index] : undefined;
}

public getKeyAndValue(key: K): [ K, V ] | [ undefined, undefined ] {
amcasey marked this conversation as resolved.
Show resolved Hide resolved
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
return index >= 0 ? [ this._keys[index], this._values[index] ] : [ undefined, undefined ];
}

public set(key: K, value: V) {
const index = ts.binarySearch(this._keys, key, ts.identity, this._comparer);
if (index >= 0) {
Expand Down
7 changes: 5 additions & 2 deletions src/harness/vfsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,11 @@ 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 [key, node] = links.getKeyAndValue(basename);
if (key) {
components[step] = basename = key;
}
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the testcase missing that we discussed and is important change is when current directory is "c:/temp" but on disk it is "C:/Temp" and all realpath results with new API will differ in casing and give errors vs it wasn't giving earlier ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And may be it is ok for watch scenario but it is something that will impact tsserver and in turn vscode.. vscode keeps track of open files from previous session so not sure if that gets updated if you change the casing and restart vscode from directory but users will not be able to get rid of error easily unless they close all files if the casing is retained across sessions by vscode. You would need to experiment to figure that out.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when current directory is "c:/temp" but on disk it is "C:/Temp"

I attempted to provide test coverage for drive root casing differences here. It's entirely possible I misunderstood your concerns though - please let me know if there's something else you'd like me to test.

may be it is ok for watch scenario but it is something that will impact tsserver

From our offline discussion (before the break), I thought I understood that the watch VFS was the best place to add these tests. It sounds like you may be suggesting adding server tests as well. If that's the case, can you please point me at an example I can model them on?

vscode keeps track of open files from previous session

I think you're saying that the paths returned by the server to VS Code might reflect the casing returned by realpathSync, but I'm not sure I understand what consequences you expect from this. If it's about re-opening files in a new session, I would have guessed VS Code used case-insensitive system calls to open them. If it's about VS Code passing stale paths back to the server when it starts a fresh instance, I would have guessed they would be re-normalized with an additional realpathSync call. Can you please elaborate on your concerns?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I attempted to provide test coverage for drive root casing differences here. It's entirely possible I misunderstood your concerns though - please let me know if there's something else you'd like me to test.

That test case also gives incorrect result? This should have been earlier been error and should be error because on case sensitive file system the import will not resolve, which is what intention of forceConsistentFileNames is..
The case that it doesnt verify is also when the files on disk and all imports are consistent, but you start with current directory = different casing and build the project. Previously that would not error now it will ?

From our offline discussion (before the break), I thought I understood that the watch VFS was the best place to add these tests. It sounds like you may be suggesting adding server tests as well. If that's the case, can you please point me at an example I can model them on?

If the above scenario breaks for watch there is a way to fix those errors by closing tsc and restarting from correct casing directory. But what happens in vscode is question. vscode keeps list of open files, so is the solution to close all files and open editor again? Does restart work , depending on answers to that we may need coverage for some tests here to lock the expected behaviour for baselines. There are tests for these in unittests\tsserver\forceConsistentCasingInFileNames.ts

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the impression that forceConsistentFileNames ignored drive roots. Sorry for not including a comment to that effect.

The case that it doesnt verify is also when the files on disk and all imports are consistent, but you start with current directory = different casing and build the project. Previously that would not error now it will ?

I'm not sure I follow. Where is the current directory specified in this case?

But what happens in vscode is question

So you'd just like me to manually verify that VS Code doesn't have obvious breaks if I close a folder, change the casing on disk, and re-open it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the impression that forceConsistentFileNames ignored drive roots. Sorry for not including a comment to that effect.

Yes. But your import doesnt test import "somethingsomething/a" and "somethingsomething/A" you would get error on either one of those and that was expected behavior which is not the current case.

I'm not sure I follow. Where is the current directory specified in this case?

Current directory need to be specified as part of the https://github.com/microsoft/TypeScript/pull/41292/files#diff-742ed890d46f805d54cbcf33f4a6f2cd286a81ac5df890b0b20636445b464093R149 . We need test where current directory differs in casing from whats on the disk.

The bigger missing part is import in wrong casing should give error which it wont because the realpath will resolve to casing on the disk and then we will loose that info. But same code in case sensitive file system will give error because import will not be resolved.

Disk file name: a.ts

import "a" > success
import "A" > will fail on case sensistive file systems... so it should error with forceConsistentFileCasing on case insensitive filesystem which it would not when realpath is invoked as part of this change.


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`);
});
}
Original file line number Diff line number Diff line change
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...

error TS1149: File 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.
error TS1149: File 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
1 {"compilerOptions":{"jsx":"react-jsx","jsxImportSource":"react","forceConsistentCasingInFileNames":true},"files":["node_modules/react/Jsx-runtime/index.d.ts","index.tsx"]}
1 {"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