From a888116381a6f66f49b1cca3d57db29cad977d01 Mon Sep 17 00:00:00 2001 From: Stafford Brunk Date: Wed, 20 Nov 2019 16:27:43 -0600 Subject: [PATCH] Handle situations where a workspace root folder is undefined. Resolves #533 This can occur when new files are created and assigned as Ruby files. As the file does not exist on the file system, the workspace root folder cannot be determined. First, fall back to the whole project "root" if that's available. Otherwise, allow the language server to just do nothing. --- packages/language-server-ruby/src/Formatter.ts | 3 ++- packages/language-server-ruby/src/Linter.ts | 7 ++++++- packages/language-server-ruby/src/linters/NullLinter.ts | 8 ++++---- packages/vscode-ruby-client/src/client.ts | 6 ++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/packages/language-server-ruby/src/Formatter.ts b/packages/language-server-ruby/src/Formatter.ts index 8e3803e2d..fdb56af5b 100644 --- a/packages/language-server-ruby/src/Formatter.ts +++ b/packages/language-server-ruby/src/Formatter.ts @@ -32,7 +32,8 @@ function getFormatter( config: RubyConfiguration, range?: Range ): IFormatter { - if (typeof config.format === 'string') { + // Only format if we have a formatter to use and an execution root + if (typeof config.format === 'string' && config.workspaceFolderUri) { const formatterConfig: FormatterConfig = { env, executionRoot: URI.parse(config.workspaceFolderUri).fsPath, diff --git a/packages/language-server-ruby/src/Linter.ts b/packages/language-server-ruby/src/Linter.ts index af65f15d7..fe2fd4490 100644 --- a/packages/language-server-ruby/src/Linter.ts +++ b/packages/language-server-ruby/src/Linter.ts @@ -30,8 +30,13 @@ function getLinter( env: RubyEnvironment, config: RubyConfiguration ): ILinter { + if (!config.workspaceFolderUri) { + return new NullLinter( + `unable to lint ${document.uri} with ${name} as its workspace root folder could not be determined` + ); + } const linter = LINTER_MAP[name]; - if (!linter) return new NullLinter(name); + if (!linter) return new NullLinter(`attempted to lint with unsupported linter: ${name}`); const lintConfig: RubyCommandConfiguration = typeof config.lint[name] === 'object' ? config.lint[name] : {}; const linterConfig: LinterConfig = { diff --git a/packages/language-server-ruby/src/linters/NullLinter.ts b/packages/language-server-ruby/src/linters/NullLinter.ts index 9cb072d80..88a55483f 100644 --- a/packages/language-server-ruby/src/linters/NullLinter.ts +++ b/packages/language-server-ruby/src/linters/NullLinter.ts @@ -3,14 +3,14 @@ import { Observable, of } from 'rxjs'; import { Diagnostic } from 'vscode-languageserver'; export default class NullLinter implements ILinter { - private name: string; + private msg: string; - constructor(name: string) { - this.name = name; + constructor(msg: string) { + this.msg = msg; } lint(): Observable { - console.error(`Lint: attempted to lint with unsupported linter: ${this.name}`); + console.error(`Lint: ${this.msg}`); return of([]); } } diff --git a/packages/vscode-ruby-client/src/client.ts b/packages/vscode-ruby-client/src/client.ts index 79c06285a..dcdd4fc74 100644 --- a/packages/vscode-ruby-client/src/client.ts +++ b/packages/vscode-ruby-client/src/client.ts @@ -63,6 +63,12 @@ export function activate(context: ExtensionContext): void { } let resource = client.protocol2CodeConverter.asUri(scopeUri); let workspaceFolder = workspace.getWorkspaceFolder(resource); + + // If the resource doesn't have a workspace folder, fall back to the root if available + if (!workspaceFolder && workspace.workspaceFolders) { + workspaceFolder = workspace.workspaceFolders[0]; + } + if (workspaceFolder) { // Save the file's workspace folder const protocolUri = client.code2ProtocolConverter.asUri(workspaceFolder.uri);