From 618a42caed9786be85182b285b06079026d7f69e Mon Sep 17 00:00:00 2001 From: Simon He <57086651+Simon-He95@users.noreply.github.com> Date: Wed, 20 Mar 2024 10:05:11 +0800 Subject: [PATCH] fix(typescript-plugin): fault tolerance for named pipe servers json file (#4075) --- packages/typescript-plugin/lib/server.ts | 18 +++++---------- packages/typescript-plugin/lib/utils.ts | 29 ++++++++++++++++++++---- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/packages/typescript-plugin/lib/server.ts b/packages/typescript-plugin/lib/server.ts index a5bb025653..791117f5a2 100644 --- a/packages/typescript-plugin/lib/server.ts +++ b/packages/typescript-plugin/lib/server.ts @@ -5,7 +5,7 @@ import { collectExtractProps } from './requests/collectExtractProps'; import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos'; import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation'; import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition'; -import { NamedPipeServer, connect, pipeTable } from './utils'; +import { NamedPipeServer, connect, readPipeTable, updatePipeTable } from './utils'; import type { FileRegistry, VueCompilerOptions } from '@vue/language-core'; export interface Request { @@ -103,16 +103,13 @@ export function startNamedPipeServer( cleanupPipeTable(); - if (!fs.existsSync(pipeTable)) { - fs.writeFileSync(pipeTable, JSON.stringify([] satisfies NamedPipeServer[])); - } - const table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8')); + const table = readPipeTable(); table.push({ path: pipeFile, serverKind, currentDirectory, }); - fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2)); + updatePipeTable(table); try { fs.unlinkSync(pipeFile); @@ -122,18 +119,15 @@ export function startNamedPipeServer( } function cleanupPipeTable() { - if (!fs.existsSync(pipeTable)) { - return; - } - for (const server of JSON.parse(fs.readFileSync(pipeTable, 'utf8'))) { + for (const server of readPipeTable()) { connect(server.path).then(client => { if (client) { client.end(); } else { - let table: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8')); + let table: NamedPipeServer[] = readPipeTable(); table = table.filter(item => item.path !== server.path); - fs.writeFileSync(pipeTable, JSON.stringify(table, undefined, 2)); + updatePipeTable(table); } }); } diff --git a/packages/typescript-plugin/lib/utils.ts b/packages/typescript-plugin/lib/utils.ts index 746f3cadc7..53bd86fa97 100644 --- a/packages/typescript-plugin/lib/utils.ts +++ b/packages/typescript-plugin/lib/utils.ts @@ -13,7 +13,29 @@ export interface NamedPipeServer { const { version } = require('../package.json'); -export const pipeTable = path.join(os.tmpdir(), `vue-tsp-table-${version}.json`); +const pipeTableFile = path.join(os.tmpdir(), `vue-tsp-table-${version}.json`); + +export function readPipeTable() { + if (!fs.existsSync(pipeTableFile)) { + return []; + } + try { + const servers: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTableFile, 'utf8')); + return servers; + } catch { + fs.unlinkSync(pipeTableFile); + return []; + } +} + +export function updatePipeTable(servers: NamedPipeServer[]) { + if (servers.length === 0) { + fs.unlinkSync(pipeTableFile); + } + else { + fs.writeFileSync(pipeTableFile, JSON.stringify(servers, undefined, 2)); + } +} export function connect(path: string) { return new Promise(resolve => { @@ -28,10 +50,7 @@ export function connect(path: string) { } export async function searchNamedPipeServerForFile(fileName: string) { - if (!fs.existsSync(pipeTable)) { - return; - } - const servers: NamedPipeServer[] = JSON.parse(fs.readFileSync(pipeTable, 'utf8')); + const servers = readPipeTable(); const configuredServers = servers .filter(item => item.serverKind === 1 satisfies ts.server.ProjectKind.Configured); const inferredServers = servers