Skip to content

lsp-mode for dart does not guess correct project root #576

@bradyt

Description

@bradyt

The prompts created by lsp--find-root-interactively and lsp--suggest-project-root will quite likely try to pass a directory having a .git directory, or similar. This may frequently be incorrect, as I believe the dart_analysis_server should be passed a directory at or above a directory with pubspec.yaml (or BUILD). I suspect we can in fact pass any directory above such, in particular, the directory of current file.

This way, the prompts will be more helpful for user, and even, user can better use lsp-auto-guess-root.

The following diff helps ensure the right directory is passed to dart_language_server. (I'm making a lot of assumptions about how dart_language_server, LSP, and lsp-mode all work together.)

@@ -1266,6 +1266,11 @@ disappearing, unset all the variables related to it."
 (defun lsp--suggest-project-root ()
   "Get project root."
   (or
+   (when (eq major-mode 'dart-mode)
+     (let ((file (buffer-file-name (current-buffer))))
+       (or (locate-dominating-file file "pubspec.yaml")
+           (locate-dominating-file file "BUILD")
+           (file-name-directory file))))
    (when (featurep 'projectile) (condition-case nil
                                     (projectile-project-root)
                                   (error nil)))

My assumptions are based on the following lines from https://github.com/natebosch/dart_language_server/blob/master/lib/src/utils/package_dir_detection.dart#L19-L21, and some notes about rootUri in the changelog at same project.

bool _isPackageRoot(Directory dir) => dir.listSync().any((f) =>
    f.path.endsWith('${Platform.pathSeparator}pubspec.yaml') ||
    f.path.endsWith('${Platform.pathSeparator}BUILD'));

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