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

fix: add realpath to host to properly resolve monorepos / symlinks #332

Merged
merged 2 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions __tests__/host.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterAll, beforeAll, test, expect, jest } from "@jest/globals";
import * as ts from "typescript";
import * as path from "path";
import { normalizePath as normalize } from "@rollup/pluginutils";
import { remove, ensureDir, writeFile } from "fs-extra";
import { remove, ensureDir, writeFile, ensureSymlink } from "fs-extra";

import { setTypescriptModule } from "../src/tsproxy";
import { LanguageServiceHost } from "../src/host";
Expand All @@ -18,12 +18,14 @@ const unaryFuncSnap = { text: unaryFunc };
const local = (x: string) => normalize(path.resolve(__dirname, x));
const testDir = local("__temp/host");
const testFile = `${testDir}/file.ts`;
const linkedTestFile = `${testDir}/link.ts`;
const nonExistent = `${testDir}/this-does-not-exist.ts`;

afterAll(() => remove(testDir));
beforeAll(async () => {
await ensureDir(testDir);
await writeFile(testFile, unaryFunc, "utf8");
await ensureSymlink(testFile, linkedTestFile);
});

test("LanguageServiceHost", async () => {
Expand Down Expand Up @@ -55,10 +57,13 @@ test("LanguageServiceHost", async () => {
expect(host.directoryExists(nonExistent)).toBeFalsy();
expect(host.fileExists(nonExistent)).toBeFalsy();
expect(host.fileExists(testFile)).toBeTruthy();
expect(host.readDirectory(testDir)).toEqual([testFile]);
expect(host.readDirectory(testDir)).toEqual([testFile, linkedTestFile]);
expect(host.readFile(nonExistent)).toBeFalsy();
expect(host.readFile(testFile)).toEqual(unaryFunc);
expect(host.useCaseSensitiveFileNames()).toBe(process.platform === "linux");
// test realpath w/ symlinks. this returns a host path, so expect path.normalize()
expect(host.realpath(testFile)).toEqual(path.normalize(testFile));
expect(host.realpath(linkedTestFile)).toEqual(path.normalize(testFile));

// test misc functionality
expect(host.getCompilationSettings()).toEqual(testOpts);
Expand Down
5 changes: 5 additions & 0 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost
return tsModule.sys.fileExists(path);
}

public realpath(path: string): string
{
return tsModule.sys.realpath!(path); // this exists in the default implementation: https://github.com/microsoft/TypeScript/blob/ab2523bbe0352d4486f67b73473d2143ad64d03d/src/compiler/sys.ts#L1288
}

public getTypeRootsVersion(): number
{
return 0;
Expand Down