Skip to content

Commit cc52990

Browse files
committed
fix(typescript-plugin): handle named pipe server timeout
1 parent f2789d9 commit cc52990

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

packages/typescript-plugin/lib/server.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@ export function startNamedPipeServer(
4646
const fileName = request.args[0];
4747
const project = getProject(fileName);
4848
if (request.type === 'projectInfoForFile') {
49-
connection.write(JSON.stringify(project
50-
? {
51-
name: project.info.project.getProjectName(),
52-
kind: project.info.project.projectKind,
53-
}
54-
: undefined
55-
));
49+
connection.write(
50+
JSON.stringify(
51+
project
52+
? {
53+
name: project.info.project.getProjectName(),
54+
kind: project.info.project.projectKind,
55+
}
56+
: null
57+
)
58+
);
5659
}
5760
else if (project) {
5861
const requestContext = {

packages/typescript-plugin/lib/utils.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,16 @@ export function updatePipeTable(servers: NamedPipeServer[]) {
4040
export function connect(path: string) {
4141
return new Promise<net.Socket | undefined>(resolve => {
4242
const client = net.connect(path);
43+
client.setTimeout(1000);
4344
client.on('connect', () => {
4445
resolve(client);
4546
});
4647
client.on('error', () => {
4748
return resolve(undefined);
4849
});
50+
client.on('timeout', () => {
51+
return resolve(undefined);
52+
});
4953
});
5054
}
5155

@@ -56,7 +60,7 @@ export async function searchNamedPipeServerForFile(fileName: string) {
5660
const inferredServers = servers
5761
.filter(item => item.serverKind === 0 satisfies ts.server.ProjectKind.Inferred)
5862
.sort((a, b) => b.currentDirectory.length - a.currentDirectory.length);
59-
for (const server of configuredServers) {
63+
for (const server of configuredServers.sort((a, b) => sortTSConfigs(fileName, a.currentDirectory, b.currentDirectory))) {
6064
const client = await connect(server.path);
6165
if (client) {
6266
const projectInfo = await sendRequestWorker<{ name: string; kind: ts.server.ProjectKind; }>({ type: 'projectInfoForFile', args: [fileName] }, client);
@@ -81,9 +85,32 @@ export async function searchNamedPipeServerForFile(fileName: string) {
8185
}
8286
}
8387

88+
function sortTSConfigs(file: string, a: string, b: string) {
89+
90+
const inA = isFileInDir(file, path.dirname(a));
91+
const inB = isFileInDir(file, path.dirname(b));
92+
93+
if (inA !== inB) {
94+
const aWeight = inA ? 1 : 0;
95+
const bWeight = inB ? 1 : 0;
96+
return bWeight - aWeight;
97+
}
98+
99+
const aLength = a.split('/').length;
100+
const bLength = b.split('/').length;
101+
102+
return bLength - aLength;
103+
}
104+
105+
function isFileInDir(fileName: string, dir: string) {
106+
const relative = path.relative(dir, fileName);
107+
return !!relative && !relative.startsWith('..') && !path.isAbsolute(relative);
108+
}
109+
84110
export function sendRequestWorker<T>(request: Request, client: net.Socket) {
85111
return new Promise<T | undefined | null>(resolve => {
86112
let dataChunks: Buffer[] = [];
113+
client.setTimeout(5000);
87114
client.on('data', chunk => {
88115
dataChunks.push(chunk);
89116
});
@@ -105,6 +132,14 @@ export function sendRequestWorker<T>(request: Request, client: net.Socket) {
105132
}
106133
resolve(json);
107134
});
135+
client.on('error', err => {
136+
console.error('[Vue Named Pipe Client] Error:', err.message);
137+
resolve(undefined);
138+
});
139+
client.on('timeout', () => {
140+
console.error('[Vue Named Pipe Client] Timeout');
141+
resolve(undefined);
142+
});
108143
client.write(JSON.stringify(request));
109144
});
110145
}

0 commit comments

Comments
 (0)