Skip to content

Commit

Permalink
Remove dependency on the pythonPath setting
Browse files Browse the repository at this point in the history
According to the announcement made by the vscode-python extension,
it is not possible to use the pythonPath setting anymore.
Therefore, it is necessary to make use of the API provided by the
vscode-python extension
microsoft/vscode-python#12596

Fix vscode-restructuredtext#222
  • Loading branch information
jaguarfi committed Aug 4, 2020
1 parent 33a0775 commit 927b98c
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
"installTestPath": "./.rst/Server"
}
],
"featureFlags": {
"usingNewInterpreterStorage": true
},
"license": "SEE LICENSE IN LICENSE.txt",
"homepage": "https://www.restructuredtext.net",
"categories": [
Expand Down
37 changes: 34 additions & 3 deletions src/features/utils/configuration.ts
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -52,10 +53,40 @@ export class Configuration {
return Configuration.loadAnySetting<string>('linter.run', 'onType', resource);
}

public static getPythonPath(resource: Uri = null): string {
public static async getPythonPath(resource: Uri = null): Promise<string> {
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<string>("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) {
Expand Down
5 changes: 5 additions & 0 deletions src/features/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"use strict";

export class Constants {
public static readonly python = "python";
}
2 changes: 1 addition & 1 deletion src/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class Python {
}

public async checkPython(resource: vscode.Uri, showInformation: boolean = true): Promise<boolean> {
const path = Configuration.getPythonPath(resource);
const path = await Configuration.getPythonPath(resource);
if (path) {
this.pythonPath = `"${path}"`;
if (await this.getVersion()) {
Expand Down
2 changes: 1 addition & 1 deletion src/rstEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down

0 comments on commit 927b98c

Please sign in to comment.