Skip to content

Commit 1470e8a

Browse files
committed
fix #542
1 parent 27ce816 commit 1470e8a

21 files changed

+61
-42
lines changed

src/client/common/configSettings.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,26 @@ export interface JupyterSettings {
106106

107107
const IS_TEST_EXECUTION = process.env['PYTHON_DONJAYAMANNE_TEST'] === '1';
108108

109-
const systemVariables: SystemVariables = new SystemVariables();
110109
export class PythonSettings extends EventEmitter implements IPythonSettings {
111110
private static pythonSettings: PythonSettings = new PythonSettings();
111+
private disposables: vscode.Disposable[] = [];
112112
constructor() {
113113
super();
114114
if (PythonSettings.pythonSettings) {
115115
throw new Error('Singleton class, Use getInstance method');
116116
}
117-
vscode.workspace.onDidChangeConfiguration(() => {
117+
this.disposables.push(vscode.workspace.onDidChangeConfiguration(() => {
118118
this.initializeSettings();
119-
});
120-
119+
}));
120+
121121
this.initializeSettings();
122122
}
123123
public static getInstance(): PythonSettings {
124124
return PythonSettings.pythonSettings;
125125
}
126126
private initializeSettings() {
127-
const workspaceRoot = IS_TEST_EXECUTION ? __dirname : vscode.workspace.rootPath;
127+
const systemVariables: SystemVariables = new SystemVariables();
128+
const workspaceRoot = (IS_TEST_EXECUTION || typeof vscode.workspace.rootPath !== 'string') ? __dirname : vscode.workspace.rootPath;
128129
let pythonSettings = vscode.workspace.getConfiguration('python');
129130
this.pythonPath = systemVariables.resolveAny(pythonSettings.get<string>('pythonPath'));
130131
this.pythonPath = getAbsolutePath(this.pythonPath, IS_TEST_EXECUTION ? __dirname : workspaceRoot);
@@ -225,7 +226,7 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
225226
exclusionPatterns: [],
226227
rebuildOnFileSave: true,
227228
rebuildOnStart: true,
228-
tagFilePath: path.join(vscode.workspace.rootPath, "tags")
229+
tagFilePath: path.join(workspaceRoot, "tags")
229230
};
230231

231232
let unitTestSettings = systemVariables.resolveAny(pythonSettings.get<IUnitTestSettings>('unitTest'));
@@ -238,7 +239,6 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
238239
this.unitTest = { nosetestArgs: [], pyTestArgs: [], unittestArgs: [] } as IUnitTestSettings;
239240
}
240241
}
241-
this.emit('change');
242242

243243
// Support for travis
244244
this.unitTest = this.unitTest ? this.unitTest : {
@@ -278,6 +278,8 @@ export class PythonSettings extends EventEmitter implements IPythonSettings {
278278
this.jupyter = this.jupyter ? this.jupyter : {
279279
appendResults: true, defaultKernel: '', startupCode: []
280280
};
281+
282+
this.emit('change');
281283
}
282284

283285
public pythonPath: string;

src/client/common/systemVariables.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class SystemVariables extends AbstractSystemVariables {
137137

138138
constructor() {
139139
super();
140-
this._workspaceRoot = vscode.workspace.rootPath;
140+
this._workspaceRoot = typeof vscode.workspace.rootPath === 'string' ? vscode.workspace.rootPath : __dirname;;
141141
this._workspaceRootFolderName = Path.basename(this._workspaceRoot);
142142
Object.keys(process.env).forEach(key => {
143143
this[`env.${key}`] = process.env[key];

src/client/extension.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export function activate(context: vscode.ExtensionContext) {
5555
context.subscriptions.push(...activateExecInTerminalProvider());
5656
context.subscriptions.push(activateUpdateSparkLibraryProvider());
5757
activateSimplePythonRefactorProvider(context, formatOutChannel);
58-
context.subscriptions.push(activateFormatOnSaveProvider(PYTHON, settings.PythonSettings.getInstance(), formatOutChannel, vscode.workspace.rootPath));
58+
context.subscriptions.push(activateFormatOnSaveProvider(PYTHON, settings.PythonSettings.getInstance(), formatOutChannel));
5959

6060
// Enable indentAction
6161
vscode.languages.setLanguageConfiguration(PYTHON.language, {
@@ -84,17 +84,17 @@ export function activate(context: vscode.ExtensionContext) {
8484
if (pythonSettings.devOptions.indexOf('DISABLE_SIGNATURE') === -1) {
8585
context.subscriptions.push(vscode.languages.registerSignatureHelpProvider(PYTHON, new PythonSignatureProvider(context, jediProx), '(', ','));
8686
}
87-
const formatProvider = new PythonFormattingEditProvider(context, formatOutChannel, pythonSettings, vscode.workspace.rootPath);
87+
const formatProvider = new PythonFormattingEditProvider(context, formatOutChannel, pythonSettings);
8888
context.subscriptions.push(vscode.languages.registerDocumentFormattingEditProvider(PYTHON, formatProvider));
8989
context.subscriptions.push(vscode.languages.registerDocumentRangeFormattingEditProvider(PYTHON, formatProvider));
9090

9191

92-
jupMain = new jup.Jupyter(lintingOutChannel, vscode.workspace.rootPath);
92+
jupMain = new jup.Jupyter(lintingOutChannel);
9393
const documentHasJupyterCodeCells = jupMain.hasCodeCells.bind(jupMain);
9494
jupMain.activate();
9595
context.subscriptions.push(jupMain);
9696

97-
context.subscriptions.push(new LintProvider(context, lintingOutChannel, vscode.workspace.rootPath, documentHasJupyterCodeCells));
97+
context.subscriptions.push(new LintProvider(context, lintingOutChannel, documentHasJupyterCodeCells));
9898
tests.activate(context, unitTestOutChannel);
9999

100100
context.subscriptions.push(new WorkspaceSymbols(lintingOutChannel));

src/client/formatters/autoPep8Formatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as settings from '../common/configSettings';
66
import { Product } from '../common/installer';
77

88
export class AutoPep8Formatter extends BaseFormatter {
9-
constructor(protected outputChannel: vscode.OutputChannel, protected pythonSettings: settings.IPythonSettings, protected workspaceRootPath: string) {
9+
constructor(outputChannel: vscode.OutputChannel, pythonSettings: settings.IPythonSettings, workspaceRootPath?: string) {
1010
super('autopep8', Product.autopep8, outputChannel, pythonSettings, workspaceRootPath);
1111
}
1212

src/client/formatters/baseFormatter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Installer, Product } from '../common/installer';
1010

1111
export abstract class BaseFormatter {
1212
private installer: Installer;
13-
constructor(public Id: string, private product: Product, protected outputChannel: vscode.OutputChannel, protected pythonSettings: settings.IPythonSettings, protected workspaceRootPath: string) {
13+
constructor(public Id: string, private product: Product, protected outputChannel: vscode.OutputChannel, protected pythonSettings: settings.IPythonSettings, protected workspaceRootPath?: string) {
1414
this.installer = new Installer();
1515
}
1616

@@ -29,7 +29,8 @@ export abstract class BaseFormatter {
2929
if (token && token.isCancellationRequested) {
3030
return [filePath, ''];
3131
}
32-
return Promise.all<string>([Promise.resolve(filePath), execPythonFile(command, args.concat([filePath]), this.workspaceRootPath)]);
32+
const workspaceRoot = this.workspaceRootPath ? this.workspaceRootPath : vscode.workspace.rootPath;
33+
return Promise.all<string>([Promise.resolve(filePath), execPythonFile(command, args.concat([filePath]), workspaceRoot)]);
3334
}).then(data => {
3435
// Delete the temporary file created
3536
if (tmpFileCreated) {

src/client/formatters/yapfFormatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as settings from './../common/configSettings';
66
import { Product } from '../common/installer';
77

88
export class YapfFormatter extends BaseFormatter {
9-
constructor(protected outputChannel: vscode.OutputChannel, protected pythonSettings: settings.IPythonSettings, protected workspaceRootPath: string) {
9+
constructor(outputChannel: vscode.OutputChannel, pythonSettings: settings.IPythonSettings, workspaceRootPath?: string) {
1010
super('yapf', Product.yapf, outputChannel, pythonSettings, workspaceRootPath);
1111
}
1212

src/client/jupyter/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class Jupyter extends vscode.Disposable {
2828
private codeLensProvider: JupyterCodeLensProvider;
2929
private lastUsedPythonPath: string;
3030
private codeHelper: CodeHelper;
31-
constructor(private outputChannel: vscode.OutputChannel, private rootPath: string) {
31+
constructor(private outputChannel: vscode.OutputChannel) {
3232
super(() => { });
3333
this.disposables = [];
3434
this.registerCommands();
@@ -49,7 +49,7 @@ export class Jupyter extends vscode.Disposable {
4949
this.disposables.forEach(d => d.dispose());
5050
}
5151
private createKernelManager() {
52-
const jupyterClient = new main.JupyterClientAdapter(this.outputChannel, this.rootPath);
52+
const jupyterClient = new main.JupyterClientAdapter(this.outputChannel, vscode.workspace.rootPath);
5353
this.kernelManager = new KernelManagerImpl(this.outputChannel, jupyterClient);
5454

5555
// This happend when user changes it from status bar

src/client/linters/baseLinter.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ export abstract class BaseLinter {
5252
public Id: string;
5353
private installer: Installer;
5454
protected pythonSettings: settings.IPythonSettings;
55-
constructor(id: string, private product: Product, protected outputChannel: OutputChannel, protected workspaceRootPath: string) {
55+
private _workspaceRootPath: string;
56+
protected get workspaceRootPath(): string {
57+
return typeof this._workspaceRootPath === 'string' ? this._workspaceRootPath : vscode.workspace.rootPath;
58+
}
59+
constructor(id: string, private product: Product, protected outputChannel: OutputChannel, workspaceRootPath: string) {
5660
this.Id = id;
5761
this.installer = new Installer();
62+
this._workspaceRootPath = workspaceRootPath;
5863
this.pythonSettings = settings.PythonSettings.getInstance();
5964
}
6065
public abstract isEnabled(): Boolean;

src/client/linters/flake8.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {OutputChannel} from 'vscode';
55
import { Product } from '../common/installer';
66

77
export class Linter extends baseLinter.BaseLinter {
8-
constructor(outputChannel: OutputChannel, workspaceRootPath: string) {
8+
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
99
super('flake8', Product.flake8, outputChannel, workspaceRootPath);
1010
}
1111

src/client/linters/mypy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Product } from '../common/installer';
77
const REGEX = '(?<file>.py):(?<line>\\d+): (?<type>\\w+): (?<message>.*)\\r?(\\n|$)';
88

99
export class Linter extends baseLinter.BaseLinter {
10-
constructor(outputChannel: OutputChannel, workspaceRootPath: string) {
10+
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
1111
super('mypy', Product.mypy, outputChannel, workspaceRootPath);
1212
}
1313
private parseMessagesSeverity(category: string): baseLinter.LintMessageSeverity {

src/client/linters/pep8Linter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {OutputChannel} from 'vscode';
55
import { Product } from '../common/installer';
66

77
export class Linter extends baseLinter.BaseLinter {
8-
constructor(outputChannel: OutputChannel, workspaceRootPath: string) {
8+
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
99
super('pep8', Product.pep8, outputChannel, workspaceRootPath);
1010
}
1111

src/client/linters/prospector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ interface IProspectorLocation {
2323
}
2424

2525
export class Linter extends baseLinter.BaseLinter {
26-
constructor(outputChannel: OutputChannel, workspaceRootPath: string) {
26+
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
2727
super('prospector', Product.prospector, outputChannel, workspaceRootPath);
2828
}
2929

src/client/linters/pydocstyle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { execPythonFile, IS_WINDOWS } from './../common/utils';
88
import { Product } from '../common/installer';
99

1010
export class Linter extends baseLinter.BaseLinter {
11-
constructor(outputChannel: OutputChannel, workspaceRootPath: string) {
11+
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
1212
super('pydocstyle', Product.pydocstyle, outputChannel, workspaceRootPath);
1313
}
1414

src/client/linters/pylama.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Product } from '../common/installer';
77
const REGEX = '(?<file>.py):(?<line>\\d+):(?<column>\\d+): \\[(?<type>\\w+)\\] (?<code>\\w\\d+):? (?<message>.*)\\r?(\\n|$)';
88

99
export class Linter extends baseLinter.BaseLinter {
10-
constructor(outputChannel: OutputChannel, workspaceRootPath: string) {
10+
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
1111
super('pylama', Product.pylama, outputChannel, workspaceRootPath);
1212
}
1313

src/client/linters/pylint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {OutputChannel} from 'vscode';
55
import { Product } from '../common/installer';
66

77
export class Linter extends baseLinter.BaseLinter {
8-
constructor(outputChannel: OutputChannel, workspaceRootPath: string) {
8+
constructor(outputChannel: OutputChannel, workspaceRootPath?: string) {
99
super('pylint', Product.pylint, outputChannel, workspaceRootPath);
1010
}
1111

src/client/providers/formatOnSaveProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as settings from "./../common/configSettings";
1010
import * as telemetryHelper from "../common/telemetry";
1111
import * as telemetryContracts from "../common/telemetryContracts";
1212

13-
export function activateFormatOnSaveProvider(languageFilter: vscode.DocumentFilter, settings: settings.IPythonSettings, outputChannel: vscode.OutputChannel, workspaceRootPath: string): vscode.Disposable {
13+
export function activateFormatOnSaveProvider(languageFilter: vscode.DocumentFilter, settings: settings.IPythonSettings, outputChannel: vscode.OutputChannel, workspaceRootPath?: string): vscode.Disposable {
1414
let formatters = new Map<string, BaseFormatter>();
1515
let pythonSettings = settings;
1616

src/client/providers/formatProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import * as telemetryContracts from '../common/telemetryContracts';
1212
export class PythonFormattingEditProvider implements vscode.DocumentFormattingEditProvider, vscode.DocumentRangeFormattingEditProvider {
1313
private formatters = new Map<string, BaseFormatter>();
1414

15-
public constructor(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel, private settings: settings.IPythonSettings, workspaceRootPath: string) {
16-
let yapfFormatter = new YapfFormatter(outputChannel, settings, workspaceRootPath);
17-
let autoPep8 = new AutoPep8Formatter(outputChannel, settings, workspaceRootPath);
15+
public constructor(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel, private settings: settings.IPythonSettings) {
16+
let yapfFormatter = new YapfFormatter(outputChannel, settings);
17+
let autoPep8 = new AutoPep8Formatter(outputChannel, settings);
1818
this.formatters.set(yapfFormatter.Id, yapfFormatter);
1919
this.formatters.set(autoPep8.Id, autoPep8);
2020
}

src/client/providers/jediProxy.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,21 @@ function getConfig() {
495495
if (path.isAbsolute(extraPath)) {
496496
return extraPath;
497497
}
498+
if (typeof vscode.workspace.rootPath !== 'string') {
499+
return '';
500+
}
498501
return path.join(vscode.workspace.rootPath, extraPath);
499502
});
500503

501504
// Always add workspace path into extra paths
502-
extraPaths.unshift(vscode.workspace.rootPath);
505+
if (typeof vscode.workspace.rootPath === 'string') {
506+
extraPaths.unshift(vscode.workspace.rootPath);
507+
}
508+
509+
let distinctExtraPaths = extraPaths.concat(additionalAutoCopletePaths)
510+
.filter(value => value.length > 0)
511+
.filter((value, index, self) => self.indexOf(value) === index);
503512

504-
let distinctExtraPaths = extraPaths.concat(additionalAutoCopletePaths).filter((value, index, self) => self.indexOf(value) === index);
505513
return {
506514
extraPaths: distinctExtraPaths,
507515
useSnippets: false,

src/client/providers/lintProvider.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class LintProvider extends vscode.Disposable {
5353
private disposables: vscode.Disposable[];
5454
private ignoreMinmatches: { match: (fname: string) => boolean }[];
5555
public constructor(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel,
56-
private workspaceRootPath: string, private documentHasJupyterCodeCells: DocumentHasJupyterCodeCells) {
56+
private documentHasJupyterCodeCells: DocumentHasJupyterCodeCells) {
5757
super(() => { });
5858
this.outputChannel = outputChannel;
5959
this.context = context;
@@ -82,13 +82,13 @@ export class LintProvider extends vscode.Disposable {
8282
private initialize() {
8383
this.diagnosticCollection = vscode.languages.createDiagnosticCollection('python');
8484

85-
this.linters.push(new prospector.Linter(this.outputChannel, this.workspaceRootPath));
86-
this.linters.push(new pylint.Linter(this.outputChannel, this.workspaceRootPath));
87-
this.linters.push(new pep8.Linter(this.outputChannel, this.workspaceRootPath));
88-
this.linters.push(new pylama.Linter(this.outputChannel, this.workspaceRootPath));
89-
this.linters.push(new flake8.Linter(this.outputChannel, this.workspaceRootPath));
90-
this.linters.push(new pydocstyle.Linter(this.outputChannel, this.workspaceRootPath));
91-
this.linters.push(new mypy.Linter(this.outputChannel, this.workspaceRootPath));
85+
this.linters.push(new prospector.Linter(this.outputChannel));
86+
this.linters.push(new pylint.Linter(this.outputChannel));
87+
this.linters.push(new pep8.Linter(this.outputChannel));
88+
this.linters.push(new pylama.Linter(this.outputChannel));
89+
this.linters.push(new flake8.Linter(this.outputChannel));
90+
this.linters.push(new pydocstyle.Linter(this.outputChannel));
91+
this.linters.push(new mypy.Linter(this.outputChannel));
9292

9393
let disposable = vscode.workspace.onDidSaveTextDocument((e) => {
9494
if (e.languageId !== 'python' || !this.settings.linting.enabled || !this.settings.linting.lintOnSave) {
@@ -138,7 +138,7 @@ export class LintProvider extends vscode.Disposable {
138138

139139
private onLintDocument(document: vscode.TextDocument, documentUri: vscode.Uri, documentLines: string[]): void {
140140
// Check if we need to lint this document
141-
const relativeFileName = path.relative(vscode.workspace.rootPath, document.fileName);
141+
const relativeFileName = typeof vscode.workspace.rootPath === 'string' ? path.relative(vscode.workspace.rootPath, document.fileName) : document.fileName;
142142
if (this.ignoreMinmatches.some(matcher => matcher.match(document.fileName) || matcher.match(relativeFileName))) {
143143
return;
144144
}

src/client/providers/setInterpreterProvider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,8 @@ function presentQuickPickOfSuggestedPythonPaths() {
241241
}
242242

243243
function setInterpreter() {
244+
if (typeof vscode.workspace.rootPath !== 'string'){
245+
return vscode.window.showErrorMessage('Please open a workspace to select the Python Interpreter');
246+
}
244247
presentQuickPickOfSuggestedPythonPaths();
245248
}

src/client/unittests/common/runner.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function runTestInTerminal(file: string, args: string[], cwd: string): Promise<a
2323
else {
2424
commands.push(`export ${PATH_VARIABLE_NAME}=$${PATH_VARIABLE_NAME}:${pyPath}`);
2525
}
26-
if (cwd !== workspace.rootPath) {
26+
if (cwd !== workspace.rootPath && typeof cwd === 'string') {
2727
commands.push(`cd ${cwd}`);
2828
}
2929
commands.push(`${file} ${args.join(' ')}`);

0 commit comments

Comments
 (0)