Shopify / ruby-lsp

An opinionated language server for Ruby
https://shopify.github.io/ruby-lsp/
MIT License
1.52k stars 144 forks source link

Ruby LSP always attempts to index the current working directory #2382

Closed etherbob closed 1 month ago

etherbob commented 1 month ago

Description

Reproduction steps

  1. On MacOS
  2. Start the Ruby LSP using BBEdit's lsp support, replacing solargraph with ruby-lsp
  3. run tail -f $HOME/Library/Containers/com.barebones.bbedit/Data/Library/Logs/BBEdit/LanguageServerProtocol-Ruby.txt
  4. Open a Ruby file
  5. ruby-lsp will attempt to index your home directory regardless of the workspace config BBEdit passes in at initialization time or when opening code in various working directories.

Code snippet or error message

2024-07-24 21:06:50.722: Server message (error): Error while indexing: Operation not permitted - /Users/<username>/Library/Application Support/com.apple.LaunchServicesTemplateApp.dv
vinistock commented 1 month ago

Thank you for the bug report!

Let me double-check if I understand the issue correctly. We do want to index all of the files in the workspace you are working on (otherwise your project declarations would not be available for go to definition, hover, etc).

What you're noticing is that BBEdit spawns the language server process using a cwd that isn't the same as the path to your workspace. Is that correct?

If my understanding is right, then we need to pass down the workspace URI used to activate the server during the initialization request to the indexer so that we rely on that instead of Dir.pwd.

etherbob commented 1 month ago

I think so. This is what BBEdit sends on startup.

2024-07-26 15:40:06.478: Initialization parameters sent to server: {
    capabilities =     {
        textDocument =         {
            codeAction =             {
                codeActionLiteralSupport =                 {
                    codeActionKind =                     {
                        valueSet =                         (
                            info,
                            quickfix,
                            refactor,
                            source
                        );
                    };
                };
            };
            completion =             {
                completionItem =                 {
                    deprecatedSupport = 1;
                    documentationFormat =                     (
                        markdown,
                        plaintext
                    );
                    insertReplaceSupport = 1;
                    insertTextModeSupport =                     {
                        valueSet =                         (
                            1,
                            2
                        );
                    };
                    preselectSupport = 1;
                    snippetSupport = 1;
                };
            };
            documentSymbol =             {
                hierarchicalDocumentSymbolSupport = 1;
                labelSupport = 1;
            };
            hover =             {
                contentFormat =                 (
                    markdown,
                    plaintext
                );
            };
            onTypeFormatting =             {
            };
            publishDiagnostics =             {
                categorySupport = 1;
                codeActionsInline = 1;
                codeDescription = 1;
                dataSupport = 1;
                relatedInformation = 1;
            };
            rename =             {
                prepareSupport = 1;
                prepareSupportDefaultBehavior = 1;
            };
            signatureHelp =             {
                signatureInformation =                 {
                    activeParameterSupport = 1;
                    documentationFormat =                     (
                        markdown,
                        plaintext
                    );
                    parameterInformation =                     {
                        labelOffsetSupport = 1;
                    };
                };
            };
            synchronization =             {
                didSave = 1;
                willSave = 1;
            };
        };
        workspace =         {
            applyEdit = 1;
            configuration = 1;
            workspaceEdit =             {
                documentChanges = 0;
                failureHandling = abort;
            };
            workspaceFolders = 1;
        };
    };
    clientInfo =     {
        name = BBEdit;
        version = "15.1.2";
    };
    initializationOptions =     {
        diagnostics = 1;
    };
    rootUri = "file:///Users/username/dev/project/";
    trace = verbose;
    workspaceFolders =     (
                {
            name = "project";
            uri = "file:///Users/username/dev/project/";
        }
    );
}
vinistock commented 1 month ago

Okay, so the workspace URI is "file:///Users/username/dev/project/", which surely is the project where you're opening the editor.

To confirm our understanding, can you please edit the Ruby LSP's code in place and print the Dir.pwd? If our understanding is right and Dir.pwd does not match the workspace URI, then we identified the issue and I know what the fix is.

  1. BUNDLE_GEMFILE=.ruby-lsp/Gemfile bundle open ruby-lsp (open the Ruby LSP for editing)
  2. In top of the internal file, which is our initial require, add the following (note: it must be printed to stderr because printing to stdout breaks the editor/server communication)
$stderr.puts("PWD: #{Dir.pwd}")
  1. Go to VS Code's Output tab under the Ruby LSP channel and search for PWD:
etherbob commented 1 month ago

Probably worth noting this is not in VSCode 2024-08-08 16:54:06.335: stderr output from server: PWD: /Users/username

vinistock commented 1 month ago

Sorry, yeah, I said VS Code, but I just cared about the output. Indeed, our understanding was confirmed an BBEdit launches the language server process in a directory that isn't the workspace you are working on.

https://github.com/Shopify/ruby-lsp/pull/2424 will improve the situation, but there is a caveat explanation in the PR.