emacs-lsp / lsp-java

lsp-mode :heart: java
https://emacs-lsp.github.io/lsp-java
GNU General Public License v3.0
647 stars 90 forks source link

Can't resolve type and common problems #59

Closed VisionaryAppDev closed 6 years ago

VisionaryAppDev commented 6 years ago

Just try to use Emacs and this plugin to develop Spring application and fact some problems.

  1. I am using Eclipse IDE to develop Spring project and after that I tried to import the Spring MVC project into Emacs and everything is working fine but when I open and close Emacs for about 10 times, the project start to get error. The error is that it can't resolve type of library. I have tried some command such as:

    • lsp-java-update-server
    • lsp-workspace-restart
    • lsp-java-build-project But it still can't fix the problem and the only way to solve this is to delete all downloaded files ( eclipse jdt ) and let it re-download all the file again. I don't know if there is a way to solve this problem effectively.
  2. Auto completion is working properly after import the project, but how about building the project into War or Jar file? I have tried to use lsp-java-build-project when the project is working but after calling this command, in the Message buffer, it shows error about can't resolve type.

  3. How can I manually refresh gradle like in eclipse so it will download needed dependency that's listed inside the build.gradle file?

  4. @yyoncho, will dap-java or dap-mode work with Spring MVC project using tomcat? I have tried to make it works but it does not. I am not sure how to configure it the right way or how to run/build the project. Please guide me how to run project and how to debug. I have tried using this example but might because I do not know how run the project or I do not understand this configuration. https://github.com/yyoncho/dap-mode#example

  5. How about something like console in Eclipse? I am trying to see the output message or log but I could not find it.

yyoncho commented 6 years ago

@VisionaryAppDev the ultimate goal of lsp-java is to provide IDE-like experience, some of the bits are missing and I am working on them(classpath browser, error explorer) and I hope that they will improve the UX.

Just try to use Emacs and this plugin to develop Spring application and fact some problems.

I am using Eclipse IDE to develop Spring project and after that I tried to import the Spring MVC project into Emacs and everything is working fine but when I open and close Emacs for about 10 times, the project start to get error. The error is that it can't resolve type of library. I have tried some command such as: lsp-java-update-server lsp-workspace-restart lsp-java-build-project But it still can't fix the problem and the only way to solve this is to delete all downloaded files ( eclipse jdt ) and let it re-download all the file again. I don't know if there is a way to solve this problem effectively.

Can you turn on lsp-print-io to t and sent me the content of messages buffer when the issue has been reproduced? In general I suspect that you might be setting lsp-java--workspace-folders after the initial JDT server startup.

Auto completion is working properly after import the project, but how about building the project into War or Jar file? I have tried to use lsp-java-build-project when the project is working but after calling this command, in the Message buffer, it shows error about can't resolve type.

lsp-java-build-project will only trigger project refresh of errors/warnings(similar to what is happening in eclipse). I am using projectile for issuing build commands like mvn clean install.

How can I manually refresh gradle like in eclipse so it will download needed dependency that's listed inside the build.gradle file?

I have tested this only with pom.xml but I guess it might work with gradle files as well: Go to gradle file and call lsp-java-enable and then lsp-java-update-project-configuration . I am working on improving that flow and making it more easier to follow. If this does not work you may close all java files and reopen which will force JDT ls server to reload project configuration files. I will update the FAQ.

@yyoncho, will dap-java or dap-mode work with Spring MVC project using tomcat? I have tried to make it works but it does not. I am not sure how to configure it the right way or how to run/build the project. Please guide me how to run project and how to debug. I have tried using this example but might because I do not know how run the project or I do not understand this configuration. https://github.com/yyoncho/dap-mode#example

Yes it should work. The example is only on how to add new debug provider(e. g. when adding support for another language), the provider for java is already in place. If you want to debug main class you just go to it and call dap-java-debug. If you are not in file that contains main class jdt server will scan for all of the main classes and ask you to select one. The alternative is to enable remote debugging on the tomcat and then call dap-debug and select Java attach from the drop down and point it to the proper host/port. You may use the configuration that is in lsp-java, it contains the steps needed to enable dap-mode/dap-java.

How about something like console in Eclipse? I am trying to see the output message or log but I could not find it.

See comment about lsp-print-io.

VisionaryAppDev commented 6 years ago

Hi @yyoncho Please check this: Error.txt

By the way, Cant resolve type problem can be fixed by delete ~/.emacs.d/workspace/ dir.

yyoncho commented 6 years ago

Your log is ok from lsp-java standpoint of view. It looks like an issue with jdt server(it fails to resolve the proper deps). Do you have a minimal project which can be used to reproduce the issue? Also, which version of java do you use? I have seen some issues when using openjdk that got resolved when switching to oraclejdk.

VisionaryAppDev commented 6 years ago

Hi @yyoncho, I will try to download a small project on the internet and will test it out. And I use Oracle JDK 1.8. I used to test it with Oracle JDK 11 but it got stuck and never open the project.

VisionaryAppDev commented 6 years ago

I have tried to import a few small projects at once using lsp-java--workspace-folders and the result is worse because it stuck when trying to open a project file. It giving the message "lsp-java has exited (exited abnormally with code 13)" and Emacs is stopped working. So after delete ~/.emacs.d/workspace, everything start to work again.

And yes, working with a small project seem to work fine and I only fact the above problem once. I am not sure why but it just like that.

By the way, how to stop company-mode from complete arg into my method? For example, when I stringVariable.substring(${1:0}, ${2:0}) they auto complete ${1:0}, ${2:0} inside the bracket. I have tried to turn lsp-java-completion-guess-arguments to nil already but nothing is working.

yyoncho commented 6 years ago

You have to turn on yasnippet mode for the completion problem.

"lsp-java has exited (exited abnormally with code 13)" this is really odd. The only thing that comes to my mind is lsp-mode causing corrupted workspace. In general, you should wait for the initial import, but again JDT server should not crash. Can you send me the projects that are causing the problem? I will try to reproduce the problem. The issue should be something odd since this looks like the pretty standard flow which was tested a lot of times.

yyoncho commented 6 years ago

And the other option is to debug JDT server. You can download the jdt server sources from https://github.com/eclipse/eclipse.jdt.ls , set the following flags and then attach from Eclipse.

  (setq lsp-java-vmargs '("-noverify" "-Xmx1G" "-XX:+UseG1GC" "-XX:+UseStringDeduplication" "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044,quiet=y"))
VisionaryAppDev commented 6 years ago

Hi, I have tried to delete project file to find out the problem and I think there should have something to do with git when there are too many committed files. Anyway, you can try it yourself. Additionally, when you open this project file so many times, it only show dependency import error and not crush. And the crush one I could not produce that error again too.

You have to turn on yasnippet mode for the completion problem.

Do you mean to fix the error then I have to turn this mode on? or to turn it off? As when I tab or enter to complete or select "substring" method from the suggestion dialog, then these ${1:0}, ${2:0} is added to the bracket automatically. I think if lsp-java-completion-guess-arguments is working and not complete that above value then it would be nice.

And for the second comment you mention, I don't get it actually as my skill is still limit.

sample project link you request

yyoncho commented 6 years ago

You should have yas-minor-mode on. I will test with the attached project. What OS do you use? Also, are you a docker user? In case you are I will prepare a docker image with emacs + lsp-java.

VisionaryAppDev commented 6 years ago

I use Linux actually, Arch Linux. And just do it, i will try to use docker too.

VisionaryAppDev commented 6 years ago

@yyoncho Hello, I fact another problem now. (Not sure if I need to open another issue) Actually, it almost unusable on a lower end device because it is too slow and Emacs get paused for 1 to 2 second before auto completion suggestion is shown. (Every char I type, it need a few hundred ms before I can go further. It's not responsive like in Eclipse or Intellij). After select a suggestion word, it take a few more second before it respond to my command (eg, moving cursor or space). I think it is quite slow compare to Eclipse IDE. Just curious if it is using sync or asyn process to fetch and communicate to JDT?

Note:

Profiler report file

yyoncho commented 6 years ago

There are sync/async requests in both lsp-java and lsp-mode. There is one big performance issue - I have PR for lsp-mode. In general we are looking for a ways to optimize lsp-mode by faster json handling, etc.

I checked the performance profile, which version of emacs do you use? It is expected to be at least 25.3.

VisionaryAppDev commented 6 years ago

I think it is the latest version now. See the below info:

GNU Emacs 26.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.22.30) of 2018-07-05

yyoncho commented 6 years ago

I tested this project several times and I am unable to reproduce. I checked the error.txt once more, it looks like you have deleted some of the diagnostics. I Believe that there is diag error that points out what is the issue and it is probably related to being unable to resolve a particular dependency. Can you reproduce with lsp-print-io set to true? Also, after observing an error can you go to the gradle file and to lsp-java-enable & lsp-java-update-project-configuration which will force eclipse jdt server to recalculate the project deps. Hopefully, this will show what is the underlying issue. If this doesn't help the only options seems to be you to debug the jdt server or report the issue upstream.

VisionaryAppDev commented 6 years ago

I have reproduced the error again please check the below link. I have created three files.

Actually, the instruction is working but after close Emacs and re-open it, then project start to get import error again so I have to follow that instruction again to make it working.

report log

yyoncho commented 6 years ago

Actually, the instruction is working but after close Emacs and re-open it, then project start to get import error again so I have to follow that instruction again to make it working.

I believe that you could simply close all java files which will restart lsp server and you could use that as a workaround for the issue. Also, you may play with the new methods - lsp-workspace-folders-add and lsp-workspace-folders-remove to check how this will affect the dependency resolving.

You may also add the following line in your config:

(delete-directory lsp-java-workspace-dir t)

which will force reimporting the projects when you have started the lsp-java.

In conclusion, I would say that it seems like there is nothing I can do on emacs side. It looks like jdt just forgets all of its settings for no reason.

VisionaryAppDev commented 6 years ago

I have tried (delete-directory lsp-java-workspace-dir t) with lsp-java--workspace-folders and it's working well.

And I also tried lsp-workspace-folders-add and lsp-workspace-folders-remove and it also works like charm but it quite more difficult to use then the first option because in the first option, I can predefine my workspace in .emacs but in the second option, I have to open project file first and then add workspace.

yyoncho commented 6 years ago

Ok, I will mark the issue as closed and continue the investigation if it gets reproduced.

VisionaryAppDev commented 6 years ago

Hi @yyoncho,

Now I face another and similar problem related to workspace folder. When I use lsp-java-workspace-dir "~/.emacs.d/.appdata/.g_lsp_java_workspace" option then it stop working again. Completion suggestion is not working, importing is not working, and yes everything is stop working. I have tried to enabled only this option to make sure if this one is the cause, and turn out when I enable this option, then workspace is not recognize anymore. Please check the below debug file for detail.

debug file

yyoncho commented 6 years ago

From what I can see you have set that also as a workspace folder:

  "workspaceFolders": [
        "file:///home/.emacs.d/.appdata/.g_lsp_java_workspace"
      ]

I suspect that this is causing the problems.

VisionaryAppDev commented 5 years ago

But lsp-java-workspace-dir is used to set workspace folder isn't it? I am not sure if it is conflict with lsp-workspace-folder-add and how to use both of them correctly.

yyoncho commented 5 years ago

lsp-java-workspace-dir is a folder which contains service data. It should not be a folder which contains java-project.

VisionaryAppDev commented 5 years ago

So it should be correct because I use lsp-java-workspace-dir to set workspace directory. It should be liked eclipse workspace dir, a place to store workspace setting and other data while lsp-workspace-folder-add is used to add the java-project dir.

yyoncho commented 5 years ago

According to logs:

 "workspaceFolders": [
        "file:///home/.emacs.d/.appdata/.g_lsp_java_workspace"
      ]

and

   "rootPath": "/home/.emacs.d/.appdata/.g_lsp_java_workspace",

both point to the same location. You may try to delete the workspace directory and retry.

VisionaryAppDev commented 5 years ago

I have tried to delete workspace folder and then trying to restart Emacs. After that I tried to open java file and it took awhile before showing message in the echo area "BeanFile.java is not part of any project. Select action: "

I have tried each of the following option:

Import project "/home/.emacs.d/workspace/."
Import project by selecting root directory interactively.

After each option is selected, the file was open inside Emacs but when I tried to write anything, there is an error message that will display: lsp-ui--workspace-path: Wrong type argument: arrayp, nil

Lastly I have chosen 'Do nothing...' and then the file was opened inside the Emacs and then I call lsp-workspace-folder-add to add the project root directory. But none is working.

Note: before I tried any option, I have deleted the workspace folder before test those options.

yyoncho commented 5 years ago

The java file should NOT be under lsp-java-workspace-dir but in another directory. In this directory, you should have either maven or gradle or eclipse project. In order this to work in this directory you should be able to issue successful build command like mvn clean install .

VisionaryAppDev commented 5 years ago

Yes, I select the root directory of the project not the source java file dir. I add '/test/edu' folder using lsp-java-workspace-dir command and '/test/edu' is containing the following folder:

bin  
build.gradle  
edu.iml  
gradle  
gradlew  
gradlew.bat  
mysql.sql  
mysql.sql~  
read.me  
settings.gradle  
src

While java file is living inside src.../...java

Correct me if I am wrong.

yyoncho commented 5 years ago

Your setup should be something like:

lsp-java-workspace-dir equals to some empty folder which is not part of your project, e. g. /your-home/.workspace-dir/

Then you should open a file from your project and then when you are asked whether to import the project you should point it to the folder containing gradle build file. If this is not working please attach the content of the message buffer with lsp-print-io set to t and the project folder if possible.

VisionaryAppDev commented 5 years ago

Your setup should be something like:...

Here is my config:

(setq lsp-java-workspace-dir "~/.emacs.d/.appdata/.g_lsp_java_workspace")

then when you are asked whether to import the project you should point it to the folder containing gradle build then file.

When I open the file that is living inside ~/test/SpringJDBCExample/.../....java there are 4 options to choose so I choose the forth option and then I choose ~/test/SpringJDBCExample as the root directory. And in the end, there is nothing working.

  1. Do not ask more for the current project(add "/home/.emacs.d/.appdata/.g_lsp_java_workspace" to lsp-project-blacklist)
  2. Do nothing.
  3. Import project "/home/.emacs.d/.appdata/.g_lsp_java_workspace."
  4. Import project by selecting root directory interactively.

error.txt

yyoncho commented 5 years ago

From what I can see there are tons of compilation errors, did you try to fix them, e. g: "Syntax error on token(s), misplaced construct(s)" and so on? You could see all of the errors by calling lsp-ui-flycheck-list.

VisionaryAppDev commented 5 years ago

I know there are errors there but I don't think it could be the cause because when I am trying to call another class in the same package, it was not recognize by company-complete. But without changing the dir of the workspace, it could recognize everything well even there is or there are errors in the current class.