emacs-lsp / lsp-dart

lsp-mode :heart: dart
https://emacs-lsp.github.io/lsp-dart
GNU General Public License v3.0
201 stars 24 forks source link

lsp-dart-run can't find a java executable or JAVA_HOME #194

Open toniz4 opened 1 year ago

toniz4 commented 1 year ago

Describe the bug

When trying to run lsp-dart-run, using envrc to populate the env variables based on a shell.nix. The debbuger or runner exits with the following message:

Launching lib/main.dart on sdk gphone64 x86 64 in debug mode...

ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

Exception: Gradle task assembleDebug failed with exit code 1

The weird thing is, the flutter and dart executables and sdks and the jdk are being set up in the same way, at the same time. So, if the flutter command is avaliable to emacs, I don't know why it only fails with the java command.

To Reproduce

Have JAVA_HOME and the java executable path set by envrc. Run dap-debug or lsp-dart-run in a flutter project.

Expected behavior That lsp-dart-run executes my app

Version

[LSP Dart] 1.24.1 at 2023.03.19 @ Emacs 28.2
[Dart SDK] Dart SDK version: 2.18.4 (stable) (Tue Nov 1 15:15:07 2022 +0000) on "linux_x64"

[Flutter SDK] /nix/store/zdvb6d47hnmgp6ccsm7n6jr60q3yczfb-flutter-3.3.8-unwrapped
[Flutter project] true
[Project entrypoint] /home/cassio/src/namer_app/lib/main.dart

What I think is hapening is that somewere, the process-environment and exec-path are being reseted to the default value, I tried to identify where, but my skills to debug elisp are not the best. But I found those possible related issues:

https://github.com/greghendershott/racket-mode/issues/539 https://github.com/rust-lang/rust-mode/pull/447 https://github.com/magit/magit/issues/4160 https://github.com/purcell/envrc/issues/31

ericdallo commented 1 year ago

I suspect is related with the shell process spawned by dart to run java is using a "default" shell which is not the same emacs or your terminal is using

toniz4 commented 1 year ago

I suspect is related with the shell process spawned by dart to run java is using a "default" shell which is not the same emacs or your terminal is using

I think that is the poblem too. It's not inheriting the "main buffer" (the one i'm executing lsp-dart-run) process-environment and exec-path, so it can't find the java executable or JAVA_HOME. But it's able to run flutter for some reason.

Do you know where this process is being started?

ericdallo commented 1 year ago

I don't know, but I use doom and they use https://github.com/purcell/exec-path-from-shell IIRC which fix most errors like that

toniz4 commented 1 year ago

I don't know, but I use doom and they use https://github.com/purcell/exec-path-from-shell IIRC which fix most errors like that

I use doom here too, but I found a possible solution. If I add

  (dap--put-if-absent conf :environment-variables '(("GRADLE_OPTS" . "-Dorg.gradle.project.android.aapt2FromMavenOverride=/nix/store/3svzmqsal084m2wffsj7drqk2kzi514c-android-sdk-env/share/android-sdk/build-tools/33.0.2/aapt2")))

To lsp-dart-dap--base-debugger-args, I'm able to run flutter just fine. There's a less intrusive way that I can do that? If there's not, if it's applicable, I could make a PR adding a variable to add custom debugger args.

ericdallo commented 1 year ago

@toniz4 you can create your own debugging template to customize that via:

(dap-register-debug-template "Flutter :: Debug with custom env vars"
                             (list :type "flutter"
                                   :environment-variables '(("GRADLE_OPTS" . "-Dorg.gradle.project.android.aapt2FromMavenOverride=/nix/store/3svzmqsal084m2wffsj7drqk2kzi514c-android-sdk-env/share/android-sdk/build-tools/33.0.2/aapt2"))))