Skip to content

Commit

Permalink
fix crashes when instantiating background threads
Browse files Browse the repository at this point in the history
  • Loading branch information
DetachHead committed Jul 30, 2024
1 parent b1372d5 commit 047da5d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/pyright-internal/src/backgroundAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export class BackgroundAnalysisRunner extends BackgroundAnalysisRunnerBase {
);
}

protected override createRealTempFile = () => new RealTempFile();
protected override createRealTempFile() {
return new RealTempFile();
}

protected override createHost(): Host {
return new FullAccessHost(this.getServiceProvider());
Expand Down
19 changes: 7 additions & 12 deletions packages/pyright-internal/src/backgroundThreadBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { ConsoleInterface, LogLevel } from './common/console';
import { Disposable, isThenable } from './common/core';
import * as debug from './common/debug';
import { FileSystem, TempFile } from './common/fileSystem';
import { PyrightFileSystem } from './pyrightFileSystem';
import { PythonVersion } from './common/pythonVersion';
import { ServiceKeys } from './common/serviceKeys';
import { ServiceProvider } from './common/serviceProvider';
Expand Down Expand Up @@ -60,21 +59,12 @@ export abstract class BackgroundThreadBase {
// Exposed for browser filesystem operations.
// In future these should go via fs.
protected realFs: FileSystem;
protected fs: FileSystem;
protected readonly serviceProvider: ServiceProvider;

constructor(protected parentPort: MessagePort | null, data: InitializationData, serviceProvider?: ServiceProvider) {
setCancellationFolderName(data.cancellationFolderName);

// Make sure there's a file system and a console interface.
this.serviceProvider = serviceProvider ?? new ServiceProvider();

// Stash the base directory into a global variable.
(global as any).__rootDirectory = Uri.parse(data.rootUri, this.serviceProvider).getFilePath();

this.realFs = this.createRealFileSystem();
this.fs = new PyrightFileSystem(this.realFs);

if (!this.serviceProvider.tryGet(ServiceKeys.console)) {
this.serviceProvider.add(ServiceKeys.console, new BackgroundConsole(this.parentPort));
}
Expand All @@ -84,17 +74,22 @@ export abstract class BackgroundThreadBase {
tempFile = this.createRealTempFile();
this.serviceProvider.add(ServiceKeys.tempFile, tempFile);
}

if (!this.serviceProvider.tryGet(ServiceKeys.caseSensitivityDetector)) {
this.serviceProvider.add(ServiceKeys.caseSensitivityDetector, tempFile ?? this.createRealTempFile());
}

this.realFs = this.createRealFileSystem();
if (!this.serviceProvider.tryGet(ServiceKeys.fs)) {
this.serviceProvider.add(ServiceKeys.fs, this.realFs);
}
if (!this.serviceProvider.tryGet(ServiceKeys.cacheManager)) {
this.serviceProvider.add(ServiceKeys.cacheManager, new CacheManager());
}
// Stash the base directory into a global variable.
(global as any).__rootDirectory = Uri.parse(data.rootUri, this.serviceProvider).getFilePath();
}

protected get fs() {
return this.serviceProvider.fs();
}

// Hooks for Browser vs NodeJS file system.
Expand Down
4 changes: 4 additions & 0 deletions packages/pyright-internal/src/tests/lsp/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* Provides the main entrypoint to the test server when running in Node.
*/

import { NodeWorkersHost } from '../../common/nodeWorkersHost';
import { initializeWorkersHost } from '../../common/workersHost';
import { run } from './languageServer';

initializeWorkersHost(new NodeWorkersHost());

run();

0 comments on commit 047da5d

Please sign in to comment.