Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Commit

Permalink
Handle situations where a workspace root folder is undefined. Resolves
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
wingrunr21 committed Nov 20, 2019
1 parent 07901d5 commit a888116
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/language-server-ruby/src/Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion packages/language-server-ruby/src/Linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 4 additions & 4 deletions packages/language-server-ruby/src/linters/NullLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Diagnostic[]> {
console.error(`Lint: attempted to lint with unsupported linter: ${this.name}`);
console.error(`Lint: ${this.msg}`);
return of([]);
}
}
6 changes: 6 additions & 0 deletions packages/vscode-ruby-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

1 comment on commit a888116

@orthodoX
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wingrunr21 Will this be released any time soon? It's really necessary for us who use this extension very often, since the reload of vscode is necessary every time this issue occurs.

Please sign in to comment.