Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to interpreter switching and shebang detection #1257

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"onCommand:python.runtests",
"onCommand:python.debugtests",
"onCommand:python.setInterpreter",
"onCommand:python.setShebangInterpreter",
"onCommand:python.viewTestUI",
"onCommand:python.viewTestOutput",
"onCommand:python.selectAndRunTestMethod",
Expand Down Expand Up @@ -120,6 +121,11 @@
"title": "Select Workspace Interpreter",
"category": "Python"
},
{
"command": "python.setShebangInterpreter",
"title": "Set Interpreter to shebang",
"category": "Python"
},
{
"command": "python.updateSparkLibrary",
"title": "Update Workspace PySpark Libraries",
Expand Down Expand Up @@ -975,6 +981,19 @@
]
}
},
"python.disableShebangDetection": {
"type": "boolean",
"default": false,
"description": "Searches for a shebang and asks to set interpreter."
},
"python.ignoreShebang": {
"type": "array",
"description": "Files where shebang should be ignored.",
"default": [],
"items": {
"type": "string"
}
},
"python.linting.enabled": {
"type": "boolean",
"default": true,
Expand Down
6 changes: 5 additions & 1 deletion src/client/interpreter/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export class InterpreterManager implements Disposable {
pythonPath = path.join('${workspaceRoot}', path.relative(workspace.rootPath!, pythonPath));
}
const pythonConfig = workspace.getConfiguration('python');
pythonConfig.update('pythonPath', pythonPath).then(() => {
var configurationTarget = null;
if (typeof workspace.rootPath !== 'string') {
configurationTarget = true;
}
pythonConfig.update('pythonPath', pythonPath, configurationTarget).then(() => {
//Done
}, reason => {
window.showErrorMessage(`Failed to set 'pythonPath'. Error: ${reason.message}`);
Expand Down
106 changes: 103 additions & 3 deletions src/client/providers/setInterpreterProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ interface PythonPathQuickPickItem extends vscode.QuickPickItem {

export class SetInterpreterProvider implements vscode.Disposable {
private disposables: vscode.Disposable[] = [];
private ignoreShebangTemp: string[] = [];
constructor(private interpreterManager: InterpreterManager) {
this.disposables.push(vscode.commands.registerCommand("python.setInterpreter", this.setInterpreter.bind(this)));
this.disposables.push(vscode.commands.registerCommand("python.setShebangInterpreter", this.setShebangInterpreter.bind(this)));

vscode.workspace.onDidOpenTextDocument(this.detectShebangInterpreter.bind(this));
vscode.workspace.onDidSaveTextDocument(this.detectShebangInterpreter.bind(this));
vscode.workspace.onDidCloseTextDocument(this.removeFromIgnoreList.bind(this));
}
public dispose() {
this.disposables.forEach(disposable => disposable.dispose());
Expand Down Expand Up @@ -58,9 +64,103 @@ export class SetInterpreterProvider implements vscode.Disposable {
}

private setInterpreter() {
if (typeof vscode.workspace.rootPath !== 'string') {
return vscode.window.showErrorMessage('Please open a workspace to select the Python Interpreter');
}
this.presentQuickPick();
}

private setShebangInterpreter() {
const document = vscode.window.activeTextEditor.document;
let error = false;

var firstLine = document.lineAt(0);
if (firstLine.isEmptyOrWhitespace) {
error = true;
}

if (!error && "#!" === firstLine.text.substr(0, 2)) {
// Shebang detected
this.interpreterManager.setPythonPath(firstLine.text.substr(2).trim());
}

if (error) {
vscode.window.showErrorMessage("No shebang found.")
}
}

private removeFromIgnoreList(document: vscode.TextDocument) {
const index = this.ignoreShebangTemp.indexOf(document.fileName)
if (index > -1 ) {
this.ignoreShebangTemp.splice(index, 1);
}
}

private detectShebangInterpreter(document: vscode.TextDocument) {
if (document.languageId !== 'python' || typeof vscode.workspace.rootPath === "string") {
return;
}

var firstLine = document.lineAt(0);
if (firstLine.isEmptyOrWhitespace) {
return;
}

const pythonConfig = vscode.workspace.getConfiguration('python');

// check for Shebang
const selectedPythonPath = pythonConfig.get("pythonPath");
let intendedPythonPath = null;
if ("#!" === firstLine.text.substr(0, 2)) {
// Shebang detected
intendedPythonPath = firstLine.text.substr(2).trim();
}
else {
return;
}

// check, if automatic interpreter switch is globally disabled
const disableShebangDetection = pythonConfig.get('disableShebangDetection');
if (disableShebangDetection) {
return;
}

// check, if the automatic interpreter switch is disabled for current file
const filesIgnoreAlways = pythonConfig.get('ignoreShebang', [] as string[]);
if (filesIgnoreAlways.indexOf(document.fileName) > -1 || this.ignoreShebangTemp.indexOf(document.fileName) > -1) {
return;
}

// check, if current interpreter is already the right one
if (selectedPythonPath === intendedPythonPath) {
return;
}


const optionChange = 'Change interpreter';
const optionIgnoreUntilClose = 'Ignore until close';
const optionIgnoreAlways = 'Ignore always';
const optionNeverShowAgain = `Don't ask again`;
const options = [optionChange, optionIgnoreUntilClose, optionIgnoreAlways, optionNeverShowAgain];
const nThis = this;

vscode.window.showWarningMessage('Detected another interpreter for this file!', ...options).then(item => {
switch(item) {
case optionChange: {
nThis.interpreterManager.setPythonPath(intendedPythonPath);
return;
}
case optionIgnoreUntilClose: {
nThis.ignoreShebangTemp.push(document.fileName);
return;
}
case optionIgnoreAlways: {
filesIgnoreAlways.push(document.fileName);
pythonConfig.update('ignoreShebang', filesIgnoreAlways, true);
return;
}
case optionNeverShowAgain: {
pythonConfig.update('disableShebangDetection', true);
return;
}
}
});
}
}