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

Highlight missing variables #489

Closed
hakanai opened this issue Oct 26, 2020 · 34 comments
Closed

Highlight missing variables #489

hakanai opened this issue Oct 26, 2020 · 34 comments
Assignees

Comments

@hakanai
Copy link

hakanai commented Oct 26, 2020

Suggestion

At a minimum, I would like to see missing variables highlighted with some kind of red underline to indicate the problem, as demonstrated by IntelliJ IDEA.

Use Cases

I'm writing Java code in VSCode, which does not yet have working code completion. I'm likely therefore to make mistakes in naming variables, which the editor could pick up on.

Examples

public class Greeter {
    public void sayHiTo(String targetId) {
        System.out.println("Hello, " + target + '!');
    }
}

Current behaviour:

image

@testforstephen
Copy link
Contributor

If you're running the Java extension in lightweight mode, then it only reports syntax errors.
If it's on standard mode, there should report all errors in your code.

You could check it on the status bar icon.
👍 is standard mode.
image
🚀 is lightweight mode.

@hakanai
Copy link
Author

hakanai commented Oct 27, 2020

I have neither of those.

image

Running v0.10.0.

@testforstephen
Copy link
Contributor

Could you share all Java extensions you installed?

@hakanai
Copy link
Author

hakanai commented Oct 28, 2020

Edit: Oh wait, half of them aren't installed, this can be done.

image

@testforstephen
Copy link
Contributor

Edit: Oh wait, half of them aren't installed, this can be done.

Do you mean it works after you installed Java extensions?

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

No, I meant I was able to fit the screenshot in.

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

If there are more I should install, I'm trying to figure out which ones will do it. I thought the vscode-java-pack was supposed to pull everything else in.

@testforstephen
Copy link
Contributor

That's the correct extension list we provided. Could you share the logs?

Java logs:
F1- > Java: Open Java language server log file

Output window:
image

Console:
F1 -> Developer: Toggle Developer Tools

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

Okay, I now have new information.

I created a new window, pasted the same code, but this time, I got an error popup telling me to check the output window for details.

The error is:

Error while activating Java: {
  "message":"Java 11 or more recent is required to run the Java extension. Please download and install a recent JDK. You can still compile your projects with older JDKs by configuring [`java.configuration.runtimes`](https://github.com/redhat-developer/vscode-java/wiki/JDK-Requirements#java.configuration.runtimes)",
  "label":"Get the Java Development Kit",
  "command":"vscode.open",
  "commandParam":{"$mid":1,"path":"/products/openjdk/download/","scheme":"https","authority":"developers.redhat.com","query":"sc_cid=701f2000000RWTnAAO"}
}. If vscode-java failed to activate, try these troubleshooting steps: https://github.com/redhat-developer/vscode-java/wiki/Troubleshooting

I have various Java installs though, including Java 11:

image

Is it possible there is an issue with how it's searching for a JDK?

@testforstephen
Copy link
Contributor

yes, this looks like duplicated with redhat-developer/vscode-java#1621.

Click Ctrl + , to open user setting, find java.home setting and add your jdk 11 home path such as C:\\Program Files\\Java\\jdk-11.0.3 to that setting.
image

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

It can't detect the JDKs available on the system and pick the right one by itself? It doesn't seem that hard.

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

Okay, now I have the thumbs up.

Still no error highlighting on the missing variable.

image

@testforstephen
Copy link
Contributor

Untitled file won't get the feature, you need save your file to a .java.

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

Simple reproduction:

  • Ctrl-Shift-N to open a new Window

  • Paste in the code:

      public class Greeter {
          public void sayHiTo(String targetId) {
              System.out.println("Hello, " + target + '!');
          }
      }
    

Result:

image

I stand by my original claim that the editor simply doesn't highlight missing variables yet.

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

Untitled file won't get the feature, you need save your file to a .java.

Why should that make a difference? The editor has recognised it as Java code. See the screenshot.

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

Saved the file as Greeter.java.

New result:

image

Still, target is not highlighted as a missing variable.

@testforstephen
Copy link
Contributor

Since you just open a non-project Java file in VS Code, it will only show syntax errors by default. Regarding to the reason, there is a long story about the design, if you're interested, you could read here redhat-developer/vscode-java#1270.

Currently you could turn on all compilation errors manually by yourself, see the screenshot below.

image

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

You're showing a different class file to mine. Show me what it does for you when you paste in the sample I gave.

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

For comparison, here's the experience in IntelliJ IDEA:

https://streamable.com/fxhdsu

@hakanai
Copy link
Author

hakanai commented Oct 30, 2020

I have been using VS Code for editing Java for the better part of a year now, across multiple Java projects, and I have never seen a red underline under a variable name. If you're telling me the feature is supported then it is very, very well hidden from the user.

@Eskibear
Copy link
Member

@trejkaz If you simply open a .java file outside current workspace, only syntax error will be reported. See my screenshot to understand current behaviors.

  1. unsaved java file
  2. .java file from current workspace (folder name is "empty"): works well
  3. .java file outside current workspace: only syntax error reported

Screen Shot 2020-10-31 at 2 40 27 PM

@hakanai
Copy link
Author

hakanai commented Oct 31, 2020

OK, so a lot of hoops to jump through to get basic behaviour working, but OK, it eventually does work.

I think it could have been a lot simpler than it was, looking back at all the things I had to try to get it working, compared to the video of doing the same thing in IDEA, where it just works first try.

I mean, I have been using the editor for almost a year, and have never seen it highlight a missing variable, even on all the projects I use it on. You can understand how I legitimately thought it was a missing feature, I'm sure.

@hakanai
Copy link
Author

hakanai commented Oct 31, 2020

Here's one inside a project, for example.

I add a new reference to gargs which does not exist, and it is not highlighted.

image

@hakanai
Copy link
Author

hakanai commented Oct 31, 2020

I think that really what this ticket is about is that the missing variable detection just doesn't work very well and should just be improved overall.

@hakanai
Copy link
Author

hakanai commented Oct 31, 2020

Additionally there seem to be some warnings here which are incorrect. There's a warning saying the cast to String is unnecessary from Sonarlint, but clearly it's necessary because arguments.get()[0] is an Object. I mention this merely as a sidenote though as I don't wish to get the ticket off track. Sonar I think is just bad at finding issues in code in general. Many a time I have seen it warn about perfectly reasonable code while not noticing extremely bad code right next to it.

@Eskibear
Copy link
Member

It's more than an issue of "missing varible detection". The issue is about project recognition. The root cause is that your project was not correctly recognize by language server, thus it could not provide any further intellisense. Well, I have to say, this ticket could have been clearer if you provided the sample project instead of pasting screenshots with limited information 🤷. Anyway, from the last screenshot, I find your project is probably https://github.com/ephemeral-laboratories/dozenal-mod3 (correct me if wrong). This project is a Kotlin Gradle project. AFAIK, Kotlin is not supported in VS Code Java extension, see redhat-developer/vscode-java#632 . build.gradle.kts was not recognized by Java extension, and the project was merely recognized as a folder containing some badly organized .java files.

For the moment, to get it working, I suggest you to manage your project with build.gradle instead of the build.gradle.kts.

@hakanai
Copy link
Author

hakanai commented Nov 2, 2020

I use Kotlin in my Gradle projects because I like type safety.

So yeah, that's definitely the issue with dozenal-mod3, but also with red-rocket, and with a couple of other recent projects of mine, basically everything created since Kotlin became the recommended language for Gradle build scripts. For that I just have to wait for tools to catch up and support Kotlin, which might be a while by the look of things. :(

Before Gradle got Kotlin support in build scripts, I was avoiding Gradle because I disliked Groovy, so I was using Buildr in preference. But Buildr has never seen widespread support in tools, so I can't really blame anyone for that.

The project at work is still using Groovy for the Gradle build, and I've never seen syntax highlighting of errors in there either, but the build there is also too complicated to really share in its current form. I'd have to cut it down to a smaller reproducible sample, which is something I've not yet had time to do. But other devs at work have made the same comment, that they avoid using VSCode to some extent because it doesn't seem to understand the structure of Java projects. On the other hand, ever since the lockdowns started, IntelliJ IDEA has not been ideal for us either, because it lacks support for opening projects remotely. So we currently use a mix of the two depending on how much autorefactoring we plan to use on a given day.

What about MinecraftForge? That's using build.gradle and I just typed some new code into an existing file in that, and get the same result.

https://github.com/MinecraftForge/MinecraftForge

image

@hakanai
Copy link
Author

hakanai commented Nov 2, 2020

Or here's Storage Drawers:

https://github.com/jaquadro/StorageDrawers

image

@hakanai
Copy link
Author

hakanai commented Nov 2, 2020

Or here's Botania.

https://github.com/Vazkii/Botania

image

I think that's a bit of variety across three different developers' codebases. All Minecraft-related but that does happen to be what I've been working on a lot lately.

@hakanai
Copy link
Author

hakanai commented Nov 2, 2020

Okay, I finally found one project where the feature does work as intended. https://github.com/redhat-developer/intellij-rsp

I have to confess that I went searching through that repository only because I thought it would be hilarious if I also reproduced it first try on one of your own projects. But nope, the red underline works fine there, and so now I've seen it working as intended for the first time ever.

image

So what's so different about that project, compared to say, Storage Drawers, which has the same Java version shown in the status bar and the same thumbs up icon?

@hakanai
Copy link
Author

hakanai commented Nov 11, 2020

I did some investigation over on the work project which is also using Groovy, and reported that error in issue #497.

@0dinD
Copy link

0dinD commented Nov 18, 2020

@trejkaz I've investigated the Minecraft projects you linked, and found both StorageDrawers and Botania to work, given that you wait for the extension to import the project. Without importing the projects, the Java language server can't provide the full feature set such as intellisense or highlighting missing variables. One thing I noted was that it took a while (maybe a minute or so) after the thumbs up icon until the project was fully imported and I could get a warning about the missing variable. This is likely caused by the ForgeGradle tasks that run for projects using the Forge MDK, which may prevent the language server from fully importing the project even though you have a thumbs up.

Minecraft Forge itself has a very complicated Gradle setup, which is why you won't be able to import it right away by opening the folder (in fact, this does not work in Eclipse or IntelliJ either). See their instructions on how to import the project, the Eclipse instructions roughly apply to VS Code. As you can see, not only do you need to run a Gradle task, but also import the projects sub-folder instead of the root project folder. Somewhere along this process it does seem like something causes the Java language server to break, throwing the following error:

Error message
!ENTRY org.eclipse.jdt.ls.core 1 0 2020-11-18 15:27:12.232
!MESSAGE >> workspace/executeCommand java.resolvePath

!ENTRY org.eclipse.jdt.ls.core 4 2 2020-11-18 15:27:12.241
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.jdt.ls.core".
!STACK 1
Java Model Exception: Java Model Status [java/net/minecraftforge/client [in forge] is not on its project's build path]
	at org.eclipse.jdt.internal.core.JavaElement.newJavaModelException(JavaElement.java:583)
	at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:256)
	at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:596)
	at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:326)
	at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:312)
	at org.eclipse.jdt.internal.core.PackageFragmentRoot.getKind(PackageFragmentRoot.java:530)
	at com.microsoft.jdtls.ext.core.model.PackageNode.createNodeForPackageFragmentRoot(PackageNode.java:199)
	at com.microsoft.jdtls.ext.core.PackageCommand.resolvePath(PackageCommand.java:139)
	at com.microsoft.jdtls.ext.core.CommandHandler.executeCommand(CommandHandler.java:33)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:215)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:205)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$3(JDTLanguageServer.java:495)
	at org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:75)
	at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:642)
	at java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:479)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
!SUBENTRY 1 org.eclipse.jdt.core 4 1006 2020-11-18 15:27:12.242
!MESSAGE java/net/minecraftforge/client [in forge] is not on its project's build path
!SUBENTRY 1 org.eclipse.jdt.core 4 1006 2020-11-18 15:27:12.242
!MESSAGE java/net/minecraftforge/client [in forge] is not on its project's build path

!ENTRY org.eclipse.jdt.ls.core 4 0 2020-11-18 15:27:12.243
!MESSAGE Error in calling delegate command handler
!STACK 1
Java Model Exception: Java Model Status [java/net/minecraftforge/client [in forge] is not on its project's build path]
	at org.eclipse.jdt.internal.core.JavaElement.newJavaModelException(JavaElement.java:583)
	at org.eclipse.jdt.internal.core.Openable.generateInfos(Openable.java:256)
	at org.eclipse.jdt.internal.core.JavaElement.openWhenClosed(JavaElement.java:596)
	at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:326)
	at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(JavaElement.java:312)
	at org.eclipse.jdt.internal.core.PackageFragmentRoot.getKind(PackageFragmentRoot.java:530)
	at com.microsoft.jdtls.ext.core.model.PackageNode.createNodeForPackageFragmentRoot(PackageNode.java:199)
	at com.microsoft.jdtls.ext.core.PackageCommand.resolvePath(PackageCommand.java:139)
	at com.microsoft.jdtls.ext.core.CommandHandler.executeCommand(CommandHandler.java:33)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler$1.run(WorkspaceExecuteCommandHandler.java:215)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
	at org.eclipse.jdt.ls.core.internal.handlers.WorkspaceExecuteCommandHandler.executeCommand(WorkspaceExecuteCommandHandler.java:205)
	at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$3(JDTLanguageServer.java:495)
	at org.eclipse.jdt.ls.core.internal.BaseJDTLanguageServer.lambda$0(BaseJDTLanguageServer.java:75)
	at java.base/java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:642)
	at java.base/java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:479)
	at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
	at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
	at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
	at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
	at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:177)
!SUBENTRY 1 org.eclipse.jdt.core 4 1006 2020-11-18 15:27:12.243
!MESSAGE java/net/minecraftforge/client [in forge] is not on its project's build path

Curiously, intellisense and semantic highlighting is working fine, but the error for missing variables do not. I'm guessing it has something to do with the error above. My guess is that the Java language server doesn't handle the special build setup of the Forge project very well, seeing as the source file for FluidContainerColorer.java resides under src in the root project directory, which is not open in VS Code since you need to instead open the projects subdirectory. This works in the Eclipse IDE, but not in VS Code.

If you actually care about working on MinecraftForge specifically, I would submit an issue over at https://github.com/redhat-developer/vscode-java, with more log files and steps to reproduce. But this problem seems very specific to the Gradle setup of MinecraftForge, normal Gradle projects (such as the mods you linked) work fine after waiting for them to import.

@akaroml
Copy link
Member

akaroml commented Dec 4, 2020

@Eskibear @testforstephen do we have a conclusion now?

@hakanai
Copy link
Author

hakanai commented Dec 4, 2020

Yeah, I originally had filed this because I thought I was requesting a new feature, but now it's clear that the feature exists, so I might as well close the ticket. Some day I hope to actually see this on one of my projects, but that's being sorted out elsewhere.

@hakanai hakanai closed this as completed Dec 4, 2020
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

5 participants