Skip to content

Commit 618a42c

Browse files
authored
fix(typescript-plugin): fault tolerance for named pipe servers json file (#4075)
1 parent bacea2a commit 618a42c

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

packages/typescript-plugin/lib/server.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { collectExtractProps } from './requests/collectExtractProps';
55
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos';
66
import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation';
77
import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition';
8-
import { NamedPipeServer, connect, pipeTable } from './utils';
8+
import { NamedPipeServer, connect, readPipeTable, updatePipeTable } from './utils';
99
import type { FileRegistry, VueCompilerOptions } from '@vue/language-core';
1010

1111
export interface Request {
@@ -103,16 +103,13 @@ export function startNamedPipeServer(
103103

104104
cleanupPipeTable();
105105

106-
if (!fs.existsSync(pipeTable)) {
107-
fs.writeFileSync(pipeTable, JSON.stringify([] satisfies NamedPipeServer[]));
108-
}
109-
const table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
106+
const table = readPipeTable();
110107
table.push({
111108
path: pipeFile,
112109
serverKind,
113110
currentDirectory,
114111
});
115-
fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2));
112+
updatePipeTable(table);
116113

117114
try {
118115
fs.unlinkSync(pipeFile);
@@ -122,18 +119,15 @@ export function startNamedPipeServer(
122119
}
123120

124121
function cleanupPipeTable() {
125-
if (!fs.existsSync(pipeTable)) {
126-
return;
127-
}
128-
for (const server of JSON.parse(fs.readFileSync(pipeTable, 'utf8'))) {
122+
for (const server of readPipeTable()) {
129123
connect(server.path).then(client => {
130124
if (client) {
131125
client.end();
132126
}
133127
else {
134-
let table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
128+
let table: NamedPipeServer[] = readPipeTable();
135129
table = table.filter(item => item.path !== server.path);
136-
fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2));
130+
updatePipeTable(table);
137131
}
138132
});
139133
}

packages/typescript-plugin/lib/utils.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,29 @@ export interface NamedPipeServer {
1313

1414
const { version } = require('../package.json');
1515

16-
export const pipeTable = path.join(os.tmpdir(), `vue-tsp-table-${version}.json`);
16+
const pipeTableFile = path.join(os.tmpdir(), `vue-tsp-table-${version}.json`);
17+
18+
export function readPipeTable() {
19+
if (!fs.existsSync(pipeTableFile)) {
20+
return [];
21+
}
22+
try {
23+
const servers: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTableFile, 'utf8'));
24+
return servers;
25+
} catch {
26+
fs.unlinkSync(pipeTableFile);
27+
return [];
28+
}
29+
}
30+
31+
export function updatePipeTable(servers: NamedPipeServer[]) {
32+
if (servers.length === 0) {
33+
fs.unlinkSync(pipeTableFile);
34+
}
35+
else {
36+
fs.writeFileSync(pipeTableFile, JSON.stringify(servers, undefined, 2));
37+
}
38+
}
1739

1840
export function connect(path: string) {
1941
return new Promise<net.Socket | undefined>(resolve => {
@@ -28,10 +50,7 @@ export function connect(path: string) {
2850
}
2951

3052
export async function searchNamedPipeServerForFile(fileName: string) {
31-
if (!fs.existsSync(pipeTable)) {
32-
return;
33-
}
34-
const servers: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8'));
53+
const servers = readPipeTable();
3554
const configuredServers = servers
3655
.filter(item => item.serverKind === 1 satisfies ts.server.ProjectKind.Configured);
3756
const inferredServers = servers

0 commit comments

Comments
 (0)