Skip to content

Commit

Permalink
create a copy of the language server exe when it startgs to prevent i…
Browse files Browse the repository at this point in the history
…t from being locked when updating/uninstalling the package
  • Loading branch information
DetachHead committed Feb 19, 2024
1 parent 83b3e1c commit 64c2fed
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions packages/vscode-pyright/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
};
Expand Down

0 comments on commit 64c2fed

Please sign in to comment.