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);