From 03d8cbd63d5212237a0f48fb64582718d65f82f9 Mon Sep 17 00:00:00 2001 From: aghoward Date: Tue, 11 Feb 2025 20:46:48 -0500 Subject: [PATCH] feat: check module exist by importlib Sometimes importing a test framework such as pytest can trigger importing other code that has a longer runtime on import than allowed by the spawned process. This fixes the issue by checking that the import path can be found, which does not trigger importing the code. --- src/commands.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/commands.ts b/src/commands.ts index 1bd8aaf..7e92f06 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -11,7 +11,11 @@ const framework = workspace.getConfiguration('pyright').get('t function validPythonModule(pythonPath: string, moduleName: string) { try { - const pythonProcess = child_process.spawnSync(pythonPath, ['-m', moduleName, '--help'], { encoding: 'utf8' }); + const pythonProcess = child_process.spawnSync( + pythonPath, + ['-c', `from importlib.machinery import PathFinder; assert PathFinder.find_spec("${moduleName}") is not None`], + { encoding: 'utf8' }, + ); if (pythonProcess.error) return false; return pythonProcess.status === 0; } catch (ex) {