Skip to content

Commit

Permalink
Dynamically import smol-toml package (#9463)
Browse files Browse the repository at this point in the history
* dynamically import smol-toml package

* fixed prettier

* wait for the module to be loaded explicitly

* fixed style

* addressing PR feedback
  • Loading branch information
heejaechang authored Nov 14, 2024
1 parent db2c9b0 commit 7b8ce24
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
*/

import * as JSONC from 'jsonc-parser';
import { parse } from 'smol-toml';
import { AbstractCancellationTokenSource, CancellationToken } from 'vscode-languageserver';
import { parse } from '../common/tomlUtils';

import { BackgroundAnalysisBase, RefreshOptions } from '../backgroundAnalysisBase';
import { CancellationProvider, DefaultCancellationProvider } from '../common/cancellationUtils';
Expand Down
19 changes: 19 additions & 0 deletions packages/pyright-internal/src/common/asyncInitialization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* shared.ts
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*
* helpers shared between multiple packages such as pyright-internal and pyright
*/

import { ensureTomlModuleLoaded } from './tomlUtils';

export async function initializeDependencies() {
// Ensure dynamic imports are loaded.
await ensureTomlModuleLoaded();

if (process.env.NODE_ENV === 'production') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('source-map-support').install();
}
}
34 changes: 34 additions & 0 deletions packages/pyright-internal/src/common/tomlUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* tomlUtils.ts
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*
* helpers related to TOML
*/

type TomlPrimitive =
| string
| number
| boolean
| {
[key: string]: TomlPrimitive;
}
| TomlPrimitive[];

// Dynamically load `smol-toml` to address module loading issues and
// maintain existing module resolution to support multiple environments.
let TOML: any;
const loadTomlModule = (async () => {
TOML = await import('smol-toml');
})();

export async function ensureTomlModuleLoaded() {
await loadTomlModule;
}

export const parse = (toml: string): Record<string, TomlPrimitive> => {
if (!TOML) {
throw new Error('TOML module not loaded');
}
return TOML.parse(toml);
};
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/nodeMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { ServiceProvider } from './common/serviceProvider';
import { run } from './nodeServer';
import { PyrightServer } from './server';

export function main(maxWorkers: number) {
run(
export async function main(maxWorkers: number) {
await run(
(conn) => new PyrightServer(conn, maxWorkers),
() => {
const runner = new BackgroundAnalysisRunner(new ServiceProvider());
Expand Down
8 changes: 3 additions & 5 deletions packages/pyright-internal/src/nodeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import { Connection, ConnectionOptions } from 'vscode-languageserver';
import { createConnection } from 'vscode-languageserver/node';
import { isMainThread } from 'worker_threads';

import { initializeDependencies } from './common/asyncInitialization';
import { getCancellationStrategyFromArgv } from './common/fileBasedCancellationUtils';

export function run(runServer: (connection: Connection) => void, runBackgroundThread: () => void) {
if (process.env.NODE_ENV === 'production') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('source-map-support').install();
}
export async function run(runServer: (connection: Connection) => void, runBackgroundThread: () => void) {
await initializeDependencies();

if (isMainThread) {
runServer(createConnection(getConnectionOptions()));
Expand Down
6 changes: 2 additions & 4 deletions packages/pyright-internal/src/pyright.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { PackageTypeVerifier } from './analyzer/packageTypeVerifier';
import { AnalyzerService } from './analyzer/service';
import { maxSourceFileSize } from './analyzer/sourceFile';
import { SourceFileInfo } from './analyzer/sourceFileInfo';
import { initializeDependencies } from './common/asyncInitialization';
import { ChokidarFileWatcherProvider } from './common/chokidarFileWatcherProvider';
import { CommandLineOptions as PyrightCommandLineOptions } from './common/commandLineOptions';
import { ConsoleInterface, LogLevel, StandardConsole, StderrConsole } from './common/console';
Expand Down Expand Up @@ -1326,10 +1327,7 @@ function parseThreadsArgValue(input: string | null): any {
Error.stackTraceLimit = 64;

export async function main() {
if (process.env.NODE_ENV === 'production') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('source-map-support').install();
}
await initializeDependencies();

// Is this a worker process for multi-threaded analysis?
if (process.argv[2] === 'worker') {
Expand Down
5 changes: 4 additions & 1 deletion packages/pyright-internal/src/tests/lsp/languageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { clearCache } from '../harness/vfs/factory';
import { BackgroundAnalysis, BackgroundAnalysisRunner } from '../../backgroundAnalysis';
import { BackgroundAnalysisBase } from '../../backgroundAnalysisBase';
import { serialize } from '../../backgroundThreadBase';
import { initializeDependencies } from '../../common/asyncInitialization';
import { FileSystem } from '../../common/fileSystem';
import { ServerSettings } from '../../common/languageServerInterface';
import { PythonVersion } from '../../common/pythonVersion';
Expand Down Expand Up @@ -388,7 +389,9 @@ async function runTestBackgroundThread() {
}
}

export function run() {
export async function run() {
await initializeDependencies();

// Start the background thread if this is not the first worker.
if (getEnvironmentData(WORKER_STARTED) === 'true') {
runTestBackgroundThread();
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es2019",
"module": "preserve",
"module": "node16",
"lib": [
"es2019"
],
Expand All @@ -10,7 +10,7 @@
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "Bundler",
"moduleResolution": "node16",
"resolveJsonModule": true,
"sourceMap": true,
"outDir": "./out",
Expand Down

0 comments on commit 7b8ce24

Please sign in to comment.