diff --git a/package.json b/package.json index 9420e50..0ceef57 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,9 @@ "installTestPath": "./.rst/Server" } ], + "featureFlags": { + "usingNewInterpreterStorage": true + }, "license": "SEE LICENSE IN LICENSE.txt", "homepage": "https://www.restructuredtext.net", "categories": [ diff --git a/src/features/utils/configuration.ts b/src/features/utils/configuration.ts index 2bf4026..04d2d4f 100644 --- a/src/features/utils/configuration.ts +++ b/src/features/utils/configuration.ts @@ -1,10 +1,11 @@ 'use strict'; import { - Uri, workspace, WorkspaceFolder, + Uri, workspace, WorkspaceFolder, extensions, WorkspaceConfiguration } from 'vscode'; import * as fs from 'fs'; import * as path from 'path'; +import { Constants } from "./constants"; export class Configuration { @@ -52,10 +53,40 @@ export class Configuration { return Configuration.loadAnySetting('linter.run', 'onType', resource); } - public static getPythonPath(resource: Uri = null): string { + public static async getPythonPath(resource: Uri = null): Promise { + try { + const extension = extensions.getExtension("ms-python.python"); + if (!extension) { + return Constants.python; + } + const usingNewInterpreterStorage = extension.packageJSON?.featureFlags?.usingNewInterpreterStorage; + if (usingNewInterpreterStorage) { + if (!extension.isActive) { + await extension.activate(); + } + const pythonPath = extension.exports.settings.getExecutionDetails(resource).execCommand[0]; + return pythonPath; + } else { + return this.getConfiguration("python", resource).get("pythonPath"); + } + } catch (error) { + return Constants.python; + } + } + + public static getConfiguration(section?: string, resource: Uri = null ): WorkspaceConfiguration { + if (resource) { + return workspace.getConfiguration(section, resource); + } else { + return workspace.getConfiguration(section); + } + } + + public static getPythonPath2(resource: Uri = null): string { // IMPORTANT: python3 does not work, so the default comes from Python extension. const primary = Configuration.loadSetting('pythonPath', 'python3', resource, true, 'python'); - // assume pythonPath is relative to workspace root. + // the user setting python.defaultInterpreterPath must be used to invoke the interpreter from the + // VSCode internal storage if (primary) { const workspaceRoot = Configuration.GetRootPath(resource); if (workspaceRoot) { diff --git a/src/features/utils/constants.ts b/src/features/utils/constants.ts new file mode 100644 index 0000000..022ed67 --- /dev/null +++ b/src/features/utils/constants.ts @@ -0,0 +1,5 @@ +"use strict"; + +export class Constants { + public static readonly python = "python"; +} \ No newline at end of file diff --git a/src/python.ts b/src/python.ts index 6f436ac..173d1d1 100644 --- a/src/python.ts +++ b/src/python.ts @@ -26,7 +26,7 @@ export class Python { } public async checkPython(resource: vscode.Uri, showInformation: boolean = true): Promise { - const path = Configuration.getPythonPath(resource); + const path = await Configuration.getPythonPath(resource); if (path) { this.pythonPath = `"${path}"`; if (await this.getVersion()) { diff --git a/src/rstEngine.ts b/src/rstEngine.ts index eff5021..724314f 100644 --- a/src/rstEngine.ts +++ b/src/rstEngine.ts @@ -62,7 +62,7 @@ export class RSTEngine { let build = Configuration.getSphinxPath(uri); if (build == null) { - const python = Configuration.getPythonPath(uri); + const python = await Configuration.getPythonPath(uri); if (python) { build = '"' + python + '" -m sphinx'; }