Vscode-java: Quick fix - Auto-Import doesn't work

Created on 23 Jul 2019  路  20Comments  路  Source: redhat-developer/vscode-java

Environment
  • Operating System: VS Code is installed on Windows 10 but I'm using the VS Code Remote Development extension on an Ubuntu 18.04
  • JDK version:
openjdk version "1.8.0_212"
OpenJDK Runtime Environment (build 1.8.0_212-8u212-b03-0ubuntu1.18.04.1-b03)
OpenJDK 64-Bit Server VM (build 25.212-b03, mixed mode)
  • Visual Studio Code version: 1.36.1
  • Java extension version: 0.47.0
Steps To Reproduce
  1. Create a basic Java file
  2. Try to use some Java type that is not yet imported in that file. Example: ArrayList<String> test = new ArrayList<String>();
  3. The type ArrayList will be marked red
  4. Hover your mouse of the ArrayList and click on "Quick fix..."
  5. All quick fix options will appear (they are correct and helpful)
  6. Click on the top-most line which is the correct suggestion to import the ArrayList type
    image
  7. Result: the error does not go away, the import for ArrayList is not automatically added to the Java file

You can manually add the import statement and it will fix the issue.

Message tracing between VS Code and the Java Language Server
It first shows a large JSON text that contains all quick fix options displayed (I skip that JSON here as the quick fix list seems correct)
Then it shows

[Trace - 12:11:15 PM] Sending request 'workspace/executeCommand - (631)'.
Params: {
"command": "java.resolvePath",
"arguments": [
"output:extension-output-%236"
]
}
[Trace - 12:11:15 PM] Received response 'workspace/executeCommand - (631)' in 3ms.
Result: []

vscode-java logs
image

Java Language Server's workspace logs
Extract:

!ENTRY org.eclipse.jdt.ls.core 1 0 2019-07-23 12:03:53.873
!MESSAGE >> workspace/executeCommand java.getPackageData

!ENTRY org.eclipse.jdt.ls.core 4 0 2019-07-23 12:03:53.874
!MESSAGE Problem with folding range for /aws/src/main/java/com/extremenetworks/hcm/aws/mgr/ResourcesWorker.java
!STACK 0
org.eclipse.jdt.core.compiler.InvalidInputException: Invalid_Char_In_String
at org.eclipse.jdt.internal.core.util.PublicScanner.getNextToken(PublicScanner.java:1470)
at org.eclipse.jdt.ls.core.internal.handlers.FoldingRangeHandler.computeMethodRanges(FoldingRangeHandler.java:209)
at org.eclipse.jdt.ls.core.internal.handlers.FoldingRangeHandler.computeTypeRanges(FoldingRangeHandler.java:143)
at org.eclipse.jdt.ls.core.internal.handlers.FoldingRangeHandler.computeTypeRootRanges(FoldingRangeHandler.java:131)
at org.eclipse.jdt.ls.core.internal.handlers.FoldingRangeHandler.computeFoldingRanges(FoldingRangeHandler.java:113)
at org.eclipse.jdt.ls.core.internal.handlers.FoldingRangeHandler.foldingRange(FoldingRangeHandler.java:61)
at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$25(JDTLanguageServer.java:781)
at org.eclipse.jdt.ls.core.internal.handlers.JDTLanguageServer.lambda$42(JDTLanguageServer.java:915)
at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602)
at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577)
at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:443)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

...

!ENTRY org.eclipse.jdt.ls.core 1 0 2019-07-23 12:15:14.827
!MESSAGE >> document/hover

!ENTRY org.eclipse.jdt.ls.core 1 0 2019-07-23 12:15:16.076
!MESSAGE >> workspace/executeCommand java.resolvePath

bug code action upstream

All 20 comments

Does it work locally?
I'm currently unable to test remote development setups. @testforstephen @jdneo is this something you can verify?

The folding range error seen in the logs is irrelevant to the current issue.

@fbricon yes, it works perfectly when using it locally. It doesn't even require me to use the quick fix functionality as it will automatically add the new import as I type the new class I need to use - very nice feature! So maybe it is related to the remote development feature...

Just tried from my side but not able to repro.

@ksemba Is the steps in the gif below correct?
demo

i also tried, the auto import in quick fix works for VS Code Dev Container.

I just setup a brand new project with a single file and it also fails. Be aware that I'm using the Remote Development VS Code extension and I'm coding this on a remote Ubuntu 18 VM (not a Docker container).
You can see that after I insert the code and save the file, it adds the import for ArrayList - but not for List. I then use the quick fix functionality to auto-add the import which doesn't do anything.
2019-07-24_13-27-35

Thanks

I have the same problem. "Add unimplemented methods", "Remove argument to match ...", "Create constructor" and "Add package declaration" are also not working. I'm not using remote development.

For me all "Quick fix" actions do not work (it simply does nothing). I have tried to disable all extensions except RedHat Java, but that does not help. I am using Spring Boot with Gradle. There are no errors in developer console.

@ksemba I was following your steps in my remote Ubuntu, not repro.
Screen Shot 2019-07-25 at 10 34 38 PM
Based on https://github.com/redhat-developer/vscode-java/issues/999#issuecomment-514926736 , I guess it's not related to vscode remote.

I do encounter the case that code action didn't do anything, for once or twice, in non-remote mode. But it simply got back to work when I retriggerred it, and I didn't have chance to provide any detailed information.

@Nukepayload2 If you can stably reproduce the issue, you can set java.trace to "verbose" and attach the log. There is detailed request and response, which would be helpful to find the culprit out.

@Eskibear
I have changed java.trace.server to verbose, but I can't share all of them. Because the trace log is too long and it contains sensitive information.

Steps:
Open quick fix on Arrays, and then click "Import 'Arrays' (java.util)".

Log:

[Trace - 11:01:39 AM] Received response 'textDocument/codeAction - (2325)' in 370ms.
Result: [
    {
        "title": "Import 'Arrays' (java.util)",
        "kind": "quickfix",
        "diagnostics": [
            {
                "range": {
                    "start": {
                        "line": 14,
                        "character": 9
                    },
                    "end": {
                        "line": 14,
                        "character": 15
                    }
                },
                "severity": 1,
                "code": "570425394",
                "source": "Java",
                "message": "Arrays cannot be resolved"
            }
        ],
        "command": {
            "title": "Import 'Arrays' (java.util)",
            "command": "java.apply.workspaceEdit",
            "arguments": [
                {
                    "changes": {},
                    "documentChanges": [
                        {
... too long to paste this trace information here



[Trace - 11:01:45 AM] Sending request 'textDocument/hover - (2326)'.
Params: {
    "textDocument": {
        "uri": "file:///Sensitive/File/Path.java"
    },
    "position": {
        "line": 3,
        "character": 52
    }
}


[Trace - 11:01:45 AM] Received response 'textDocument/hover - (2326)' in 3ms.
Result: {
    "contents": ""
}


[Trace - 11:01:48 AM] Sending request 'workspace/executeCommand - (2327)'.
Params: {
    "command": "java.resolvePath",
    "arguments": [
        "output:extension-output-%236"
    ]
}


[Trace - 11:01:48 AM] Received response 'workspace/executeCommand - (2327)' in 2ms.
Result: []

Focus on the codeAction response.
Below is my response of the import 'Arrays' code action. See if it has exactly the same structure (esp. command, documentChanges) with yours, and share the diff if there is.
If there's nothing wrong with the response, I think the culprit can only be either a) vscode issue, or b) command java.apply.workspaceEdit issue.


log


[Trace - 6:42:56 AM] Received response 'textDocument/codeAction - (83)' in 246ms.
Result: [
    {
        "title": "Import 'Arrays' (java.util)",
        "kind": "quickfix",
        "diagnostics": [
            {
                "range": {
                    "start": {
                        "line": 6,
                        "character": 8
                    },
                    "end": {
                        "line": 6,
                        "character": 14
                    }
                },
                "severity": 1,
                "code": "16777218",
                "source": "Java",
                "message": "Arrays cannot be resolved to a type"
            }
        ],
        "command": {
            "title": "Import 'Arrays' (java.util)",
            "command": "java.apply.workspaceEdit",
            "arguments": [
                {
                    "changes": {},
                    "documentChanges": [
                        {
                            "textDocument": {
                                "version": 0,
                                "uri": "file:///home/yanzh/test/Hello.java"
                            },
                            "edits": [
                                {
                                    "range": {
                                        "start": {
                                            "line": 0,
                                            "character": 0
                                        },
                                        "end": {
                                            "line": 0,
                                            "character": 0
                                        }
                                    },
                                    "newText": ""
                                }
                            ]
                        },
                        {
                            "textDocument": {
                                "version": 0,
                                "uri": "file:///home/yanzh/test/Hello.java"
                            },
                            "edits": [
                                {
                                    "range": {
                                        "start": {
                                            "line": 0,
                                            "character": 0
                                        },
                                        "end": {
                                            "line": 0,
                                            "character": 0
                                        }
                                    },
                                    "newText": "import java.util.Arrays;\n\n"
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    },

Using locally. Quick Fix -> Import does nothing. However Source Action -> Organize Imports works fine.

That's the same for me working locally.
Quick fix does nothing but clicking on the yellow light bulb and then click "Import ..." works.

I find it's probably a VS Code issue... and ~Windows-only~... See: https://github.com/microsoft/vscode/issues/77018

  • [Fail] Quick Fix -> Import XXX
  • [OK] Cursor on the line -> Click Yellow Bulb -> Import XXX

I am on Fedora 29 so it seems not to be Windowd-only.

@ppatierno I find QuickFix works well in Insider version. Guess it's fixed but not released yet?

Version: 1.37.0-insider (user setup)
Commit: 628bdedf671d3230fc76982fae845875a6f6c0da
Date: 2019-07-19T10:33:04.702Z
Electron: 4.2.5
Chrome: 69.0.3497.128
Node.js: 10.11.0
V8: 6.9.427.31-electron.0
OS: Windows_NT x64 10.0.18362

@Eskibear let's see :-)

1.37 version is released. Seems fixed now.

This problem is gone after installing the latest stable version.

Closing then

Was this page helpful?
1 / 5 - 1 ratings

Related issues

larixer picture larixer  路  3Comments

xriu picture xriu  路  4Comments

danielcalvogonzalez picture danielcalvogonzalez  路  3Comments

protoEvangelion picture protoEvangelion  路  3Comments

shawmanz32na picture shawmanz32na  路  3Comments