Skip to content

Commit

Permalink
fix: path resolve incorrect in multi root workspaces
Browse files Browse the repository at this point in the history
close #1585, close #1591, close #1593, close #1590
  • Loading branch information
johnsoncodehk committed Jul 19, 2022
1 parent 3e42c77 commit ecd9a63
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
40 changes: 2 additions & 38 deletions packages/vue-language-server/src/project.ts
Expand Up @@ -4,7 +4,6 @@ import type * as ts from 'typescript/lib/tsserverlibrary';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import * as vscode from 'vscode-languageserver';
import type { createLsConfigs } from './configHost';
import * as path from 'upath';
import { getDocumentSafely } from './utils';
import { LanguageConfigs, loadCustomPlugins, RuntimeEnvironment } from './common';
import { tsShared } from '@volar/vue-typescript';
Expand All @@ -15,6 +14,7 @@ export async function createProject(
runtimeEnv: RuntimeEnvironment,
languageConfigs: LanguageConfigs,
ts: typeof import('typescript/lib/tsserverlibrary'),
projectSys: ts.System,
options: shared.ServerInitializationOptions,
rootPath: string,
tsConfig: string | ts.CompilerOptions,
Expand All @@ -24,32 +24,6 @@ export async function createProject(
lsConfigs: ReturnType<typeof createLsConfigs> | undefined,
) {

const projectSys: typeof ts.sys = {
...ts.sys,
readFile: (path, encoding) => ts.sys.readFile(resolveAbsolutePath(path), encoding),
writeFile: (path, content) => ts.sys.writeFile(resolveAbsolutePath(path), content),
directoryExists: path => {
if (path === '') {
// fix https://github.com/johnsoncodehk/volar/issues/679
return ts.sys.directoryExists(path);
}
return ts.sys.directoryExists(resolveAbsolutePath(path));
},
getDirectories: path => ts.sys.getDirectories(resolveAbsolutePath(path)),
readDirectory: (path, extensions, exclude, include, depth) => ts.sys.readDirectory(resolveAbsolutePath(path), extensions, exclude, include, depth),
realpath: ts.sys.realpath ? path => {
const resolvedPath = resolveAbsolutePath(path);
const realPath = ts.sys.realpath!(resolvedPath);
if (realPath === resolvedPath) {
// rollback if failed
return path;
}
return realPath;
} : undefined,
fileExists: path => ts.sys.fileExists(resolveAbsolutePath(path)),
getCurrentDirectory: () => rootPath,
};

let typeRootVersion = 0;
let projectVersion = 0;
let vueLs: vue.LanguageService | undefined;
Expand All @@ -72,16 +46,6 @@ export async function createProject(
dispose,
};

function resolveAbsolutePath(_path: string) {
const relativePath = path.relative(ts.sys.getCurrentDirectory(), rootPath);
if (relativePath === '') {
return _path;
}
if (_path === '') {
return relativePath;
}
return _path;
}
function getLanguageService() {
if (!vueLs) {
vueLs = languageConfigs.createLanguageService(
Expand Down Expand Up @@ -180,7 +144,7 @@ export async function createProject(
readDirectory: projectSys.readDirectory,
realpath: projectSys.realpath,
fileExists: projectSys.fileExists,
getCurrentDirectory: projectSys.getCurrentDirectory,
getCurrentDirectory: () => rootPath,
getProjectReferences: () => parsedCommandLine.projectReferences, // if circular, broken with provide `getParsedCommandLine: () => parsedCommandLine`
// custom
getDefaultLibFileName: options => ts.getDefaultLibFilePath(options), // TODO: vscode option for ts lib
Expand Down
21 changes: 21 additions & 0 deletions packages/vue-language-server/src/projects.ts
Expand Up @@ -285,6 +285,25 @@ function createWorkspace(
const projects = shared.createPathMap<Project>();
let inferredProject: Project | undefined;

const getRootPath = () => rootPath;
const workspaceSys = ts.sys.getCurrentDirectory() === rootPath ? ts.sys : new Proxy(ts.sys, {
get(target, prop) {
const fn = target[prop as keyof typeof target];
if (typeof fn === 'function') {
return new Proxy(fn, {
apply(target, thisArg, args) {
const cwd = process.cwd;
process.cwd = getRootPath;
const result = (target as any).apply(thisArg, args);
process.cwd = cwd;
return result;
}
});
}
return fn;
},
});

return {
projects,
getProject,
Expand Down Expand Up @@ -313,6 +332,7 @@ function createWorkspace(
runtimeEnv,
languageConfigs,
ts,
workspaceSys,
options,
rootPath,
await getInferredCompilerOptions(),
Expand Down Expand Up @@ -440,6 +460,7 @@ function createWorkspace(
runtimeEnv,
languageConfigs,
ts,
workspaceSys,
options,
path.dirname(tsConfig),
tsConfig,
Expand Down

1 comment on commit ecd9a63

@scottbedard
Copy link

Choose a reason for hiding this comment

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

Awesome, thank you for the quick response to those issues! I appreciate your maintenance of this project.

Please sign in to comment.