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-dart-get-sdk-dir doesn't find the sdk dir on nixos in flutter projects #202

Open
NANASHI0X74 opened this issue Sep 13, 2023 · 2 comments

Comments

@NANASHI0X74
Copy link

Describe the bug
I've previously had similar issues- #157
Now by debugging (lsp-dart-get-sdk-dir) I've determined the cause of my issues.
(NB that flutter is added to my $PATH via a nix-shell and envrc)

Debugger entered--Lisp error: (error "Dart SDK not found inside flutter cache dir /nix/s...")
  error("Dart SDK not found inside flutter cache dir %s.  C..." "/nix/store/i8r6cbgmrcpzhk3w4fqvvx18qm1y645q-flutte...")
  (if (file-exists-p dart-sdk) dart-sdk (error "Dart SDK not found inside flutter cache dir %s.  C..." dart-sdk))
  (let ((dart-sdk (expand-file-name "bin/cache/dart-sdk/" (lsp-dart-get-flutter-sdk-dir)))) (if (file-exists-p dart-sdk) dart-sdk (error "Dart SDK not found inside flutter cache dir %s.  C..." dart-sdk)))
  (progn (let ((dart-sdk (expand-file-name "bin/cache/dart-sdk/" (lsp-dart-get-flutter-sdk-dir)))) (if (file-exists-p dart-sdk) dart-sdk (error "Dart SDK not found inside flutter cache dir %s.  C..." dart-sdk))))
  (if (lsp-dart-flutter-project-p) (progn (let ((dart-sdk (expand-file-name "bin/cache/dart-sdk/" (lsp-dart-get-flutter-sdk-dir)))) (if (file-exists-p dart-sdk) dart-sdk (error "Dart SDK not found inside flutter cache dir %s.  C..." dart-sdk)))))
  (or lsp-dart-sdk-dir (if (lsp-dart-flutter-project-p) (progn (let ((dart-sdk (expand-file-name "bin/cache/dart-sdk/" (lsp-dart-get-flutter-sdk-dir)))) (if (file-exists-p dart-sdk) dart-sdk (error "Dart SDK not found inside flutter cache dir %s.  C..." dart-sdk))))) (and (lsp-dart-flutter-snap-install-p) "~/snap/flutter/common/flutter/bin/cache/dart-sdk") (let ((result (let ((result (let ... ...))) (if result (progn (locate-dominating-file result "bin")))))) (if result (progn (file-truename result)))))
  lsp-dart-get-sdk-dir()
  eval-expression((lsp-dart-get-sdk-dir) nil nil 127)
  funcall-interactively(eval-expression (lsp-dart-get-sdk-dir) nil nil 127)
  command-execute(eval-expression)

So- if lsp-dart detects that the project root is a flutter project, it tries to find the dart sdk in (lsp-dart-get-flutter-sdk-dir)/bin/cache/dart-sdk. Due to how flutter is packaged in nixpkgs though, the dart sdk is not found there.
In my case, (lsp-dart-get-flutter-sdk-dir) evaluates to "/nix/store/i8r6cbgmrcpzhk3w4fqvvx18qm1y645q-flutter-wrapped/".
So in that case an error will be returned and lsp-dart won't try to find the dart sdk from $PATH
What complicated my issue is that one of my project repos didn't contain one flutter project, but a monorepo with multiple flutter projects in subdirectories. In that case, lsp-dart found the dart sdk from my $PATH instead. I was very confused about this, because I didn't get why the analysis server was started in one project but not the other.

Is there anything that can be done without unreasonable effort from lsp-dart side to support nixos better or is this on me as a user to fix?
one workaround is to find the dart sdk dir manually and set it in .dir-locals.el, but that seems quite involved. One reasonably simple way to find this would be to create a dart project and run (lsp-dart-get-sdk-dir) inside it.

To Reproduce
in a directory containing a nix-shell, .envrc and flutter project:

dir
|--shell.nix
|--.envrc
|--project
   |--pubspec.yaml
   |--lib/main.dart
   |--...

with a shell.nix like the following:

{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell {
  buildInputs = with pkgs;
    [
      flutter
      dart
    ];
 }

in emacs, using envrc and lsp-dart
(I use doom emacs with :lang (dart +flutter +lsp) and :tools (direnv))
open dir/project/lib/main.dart (activating dart-mode)
Expected behavior
dart_analysis_server starts in flutter root

Version
[LSP Dart] 1.24.2 at 2023.09.13 @ Emacs 29.1
[Dart SDK] Dart SDK version: 3.1.0 (stable) (Tue Aug 15 21:33:36 2023 +0000) on "linux_x64"

[Flutter SDK] /nix/store/i8r6cbgmrcpzhk3w4fqvvx18qm1y645q-flutter-wrapped/
[Flutter project] true
[Project entrypoint] /home/nanashi/Documents/projects//lib/main.dart

@NANASHI0X74
Copy link
Author

I thought about setting FLUTTER_ROOT, but that also doesn't work because lsp-dart-get-sdk-dir also doesn't check that variable if it finds the flutter sdk in $PATH

@xinyifly
Copy link

I can get lsp-dart itself work correctly by using buildInputs = [ flutter.unwrapped ]

But lsp-dart-run didn't work, the daemon process is running but no window popped up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants