Skip to content

lsp-dart-get-sdk-dir doesn't find the sdk dir on nixos in flutter projects #202

@NANASHI0X74

Description

@NANASHI0X74

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions