From 64c2fedd2e3059490b2e6d0c855596f6852c8663 Mon Sep 17 00:00:00 2001 From: DetachHead Date: Sun, 18 Feb 2024 22:13:30 +1000 Subject: [PATCH] create a copy of the language server exe when it startgs to prevent it from being locked when updating/uninstalling the package --- packages/vscode-pyright/src/extension.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/vscode-pyright/src/extension.ts b/packages/vscode-pyright/src/extension.ts index 587a6fd484..1fb84e5f8b 100644 --- a/packages/vscode-pyright/src/extension.ts +++ b/packages/vscode-pyright/src/extension.ts @@ -45,6 +45,7 @@ import { } from 'vscode-languageclient/node'; import { FileBasedCancellationStrategy } from './cancellationUtils'; import { githubRepo, toolName } from 'pyright-internal/constants'; +import { cp } from 'fs/promises'; let cancellationStrategy: FileBasedCancellationStrategy | undefined; @@ -95,16 +96,19 @@ export async function activate(context: ExtensionContext) { let serverOptions: ServerOptions | undefined = undefined; if (workspace.getConfiguration('basedpyright').get('importStrategy') === 'fromEnvironment') { const pythonApi = await PythonExtension.api(); - const scriptName = 'basedpyright-langserver'; - const executablePath = path.join( - pythonApi.environments.getActiveEnvironmentPath().path, - '..', - os.platform() === 'win32' ? `${scriptName}.exe` : scriptName - ); + const executableName = `basedpyright-langserver${os.platform() === 'win32' ? '.exe' : ''}`; + const executableDir = path.join(pythonApi.environments.getActiveEnvironmentPath().path, '..'); + const executablePath = path.join(executableDir, executableName); if (existsSync(executablePath)) { console.log('using pyright executable:', executablePath); + + // make a copy of the exe to avoid locking it, which would otherwise cause crashes when you try to + // update/uninstall basedpyright while vscode is open + const copiedExecutablePath = path.join(executableDir, `_vscode_copy_${executableName}`); + await cp(executablePath, copiedExecutablePath, { force: true }); + serverOptions = { - command: executablePath, + command: copiedExecutablePath, transport: TransportKind.stdio, args: cancellationStrategy.getCommandLineArguments(), };