Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lsp-go: Add possibility to wrap gopls binary (mostly for nix) #4211

Merged
merged 2 commits into from
Nov 7, 2023

Conversation

ibizaman
Copy link

@ibizaman ibizaman commented Oct 30, 2023

Fixes #1933 for lsp-go.

This allows for example to pick the go version that's provided in a repository's flake.nix or by home-manager or NixOS. This follows the example of lsp-haskell.

In my init.el I have:

(use-package nix-sandbox)

(defun my/nix--lsp-go-wrapper (args)
  (if-let ((sandbox (nix-current-sandbox)))
      (apply 'nix-shell-command sandbox args)
    args))

(setq lsp-go-server-path "gopls"
      lsp-go-server-wrapper-function 'my/nix--lsp-go-wrapper)

One of my repos provides the go binary with such a flake.nix (abridged version):

{
  outputs = { ... }: {
    devShells.default = pkgs.mkShell {
      packages = [ pkgs.go ];
    };
  };
}

And a shell.nix (needed for the nix-sandbox emacs package):

(import (
  let
    lock = builtins.fromJSON (builtins.readFile ./flake.lock);
  in fetchTarball {
    url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
    sha256 = lock.nodes.flake-compat.locked.narHash;
  }
) {
  src =  ./.;
}).shellNix

And also, the gopls binary is provided by home-manager:

$ type gopls
gopls is $HOME/.nix-profile/bin/gopls
$ type go
go not found
$ nix develop

bash-5.2$ type gopls
gopls is $HOME/.nix-profile/bin/gopls
bash-5.2$ type go
go is /nix/store/5p6bk8gmc8m8sm0766nb5qmxwp3f64gj-go/bin/go

Now, if I open a file in that repo, I get the go binary from the flake.nix:

(nix-executable-find (nix-current-sandbox) "go")
"/nix/store/5p6bk8gmc8m8sm0766nb5qmxwp3f64gj-go/bin/go"

(nix-executable-find (nix-current-sandbox) "gopls")
"/Users/pierre/.nix-profile/bin/gopls"

This allows for example to pick the go version that's provided in a repository's flake.nix

Follows the example of lsp-haskell.
@github-actions github-actions bot added the client One or more of lsp-mode language clients label Oct 30, 2023
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection
(lambda () (cons lsp-go-gopls-server-path lsp-go-gopls-server-args)))
(make-lsp-client :new-connection (lsp-stdio-connection (lambda () (lsp-go--server-command)))
Copy link
Member

Choose a reason for hiding this comment

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

(lambda () (lsp-go--server-command)) this could be: lsp-go--server-command

@ibizaman ibizaman requested a review from yyoncho November 7, 2023 18:19
@yyoncho yyoncho merged commit 6b79afa into emacs-lsp:master Nov 7, 2023
11 of 14 checks passed
@yyoncho
Copy link
Member

yyoncho commented Nov 7, 2023

Thank you for contributing to lsp-mode! I will do some restructuring to allow the use of this OOTB without additional config.

@ibizaman
Copy link
Author

ibizaman commented Nov 7, 2023

That was a pleasure. Thanks for accepting the PR :)

"Command and arguments for launching the inferior language server process.
These are assembled from the customizable variables `lsp-go-server-path'
and `lsp-go-server-wrapper-function'."
(funcall lsp-go-server-wrapper-function (append (list lsp-go-server-path) lsp-go-gopls-server-args)))
Copy link

Choose a reason for hiding this comment

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

Shouldn't lsp-go-server-path be lsp-go-gopls-server-path? Without manually setting lsp-go-server-path lsp-mode doesn't even try to start gopls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
client One or more of lsp-mode language clients
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose per-server wrapper functions for use with nix-shell and guix
4 participants