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

[java-gradle-plugin] Gradle API not resolved when developing Gradle plugins with Java #3260

Closed
JoseLion opened this issue Aug 29, 2023 · 10 comments

Comments

@JoseLion
Copy link

When developing Gradle plugins, the Gradle API is added by the java-gradle-plugin. Even though the project compiles with no problem, VSCode reports The import org.gradle cannot be resolved Java(268435846) on every import of the org.gradle.* package. Weird enough, it seems VSCode does have access to any class imported by this package because if we try to go to the definition of the class (e.g. with Ctrl + Click), a decompiled .class file opens.

image
image
image

Environment
  • Operating System: Windows 11 Home 22H2 | 22621.2215
  • JDK version: v20.0.2
  • Visual Studio Code version: v1.81.1
  • Java extension version: v1.21.0
Steps To Reproduce
  1. Create a new Gradle Plugin using Gradle Init:
    • Run gradle init command
    • Make sure to select 4: Gradle plugin in the first step
    • Name the plugin as desired (using Foo for these steps)
    • Complete the init steps
  2. Open the file plugin\src\main\java\foo\FooPlugin.java
  3. The issue will show on the imports of org.gradle.* package
Current Result

VSCode shows The import org.gradle cannot be resolved on org.gradle.* imports

Expected Result

VSCode should resolve org.gradle.* imports when the java-gradle-plugin plugin is applied

Additional Informations

It seems this issue was already reported in the vscode-gradle repository more than a year ago, but was never moved here:
microsoft/vscode-gradle#1257

@jdneo
Copy link
Collaborator

jdneo commented Aug 31, 2023

I didn't observe this with a fresh new created Gradle plugin project. Could you share your project?

image

BTW, have you installed other Java language support extension?

@JoseLion
Copy link
Author

Thanks for the response, @jdneo! Okay, so as soon as I saw it worked for you, I cleaned as much as possible to isolate the problem. So I removed all other extensions related to Java, deleted all .gradle folders, deleted the VSCode workspace cache, etc.

In the end, I found the real issue 😅 it seems the problem appears only when using the new API. To be exact, when the following line is used:
image

As soon as line 54 is commented out, the problem disappears. To reproduce the issue, you need to be sure to answer yes when the question below is prompted:

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no]

I'm also attaching a fresh newly generated project using the API: new-api-plugin.zip

@jdneo
Copy link
Collaborator

jdneo commented Aug 31, 2023

Thank you for sharing the project! Actually, I have some new findings with your attached project. And now it seems more like a bug of the extension, because of two reasons:

Even if we do not comment out gradlePlugin.testSourceSets.add(sourceSets.functionalTest)

  • I can still build the project via gradle build
  • The project can be successfully imported in IntelliJ, without any unresolved type error.

It's only our extension that is not working with that.

@jdneo
Copy link
Collaborator

jdneo commented Aug 31, 2023

Another update is that we are recently exploring a new way to support Gradle project. I tested it on my machine and it works. Maybe you can try it as well if you are interested in 😄. Below are the steps:

  • install the latest pre-release of Gradle for Java extension
    Picture1
  • (Optional) If you are using VS Code stable, set the setting "java.gradle.buildServer.enabled" to "on". Skip this if you are using Insiders
  • Run Java: Clean Java Language Server Workspace > Reload and Delete
  • After reload, if you still see
    image
    in the Java Projects explorer, it indicates that the project is still imported by Buildship, you need to clean and reload again. (This is caused by some issues that the bundle is not registered for the first time in a new workspace)
  • You should see logs
    image
    in those two output channel if the new importer is working.

@JoseLion
Copy link
Author

Awesome! I confirm the steps to use the new Gradle support work perfectly, and the issue has also disappeared. I'd love to keep using this new Gradle support as I mostly use Gradle on my projects. Is it ok to use the pre-release version until a stable one is released? Are there any "gotchas" I should try to be aware of? 🙂

@jdneo
Copy link
Collaborator

jdneo commented Aug 31, 2023

It's ok to keep using the pre-release to get the latest enhancement and bug fix.

If you meet any problem, you can file issue at https://github.com/microsoft/vscode-gradle/issues.

In short, the new approach will delegate the build job to Gradle. The java extension is not responsible to generate .class files anymore. Gradle build tool will be used to do the compilation. In other word, we can say bye to the 'bin' folders. 😄

@jdneo
Copy link
Collaborator

jdneo commented Aug 31, 2023

I'm closing this issue, feel free to raising questions or report bugs in https://github.com/microsoft/vscode-gradle/issues when you are using this new approach.

@jdneo jdneo closed this as completed Aug 31, 2023
@nickzhums
Copy link
Contributor

nickzhums commented Aug 31, 2023

@JoseLion please feel free to use this new feature in the pre-release version and let us know if there is any feedback. We aim to use this new feature and make Gradle experience as smooth as possible and love your feedback

@JoseLion
Copy link
Author

Awesome! I think this new approach is great, much better dev experience, and getting rid of the "bin" folders is big win too IMO. I'll keep using the pre-release with all my projects and I'll be more than happy to provide as much feedback as I can.

Thanks for the help and for the great insight on this new feature! 🙂🎉

@nickzhums
Copy link
Contributor

Awesome! I think this new approach is great, much better dev experience, and getting rid of the "bin" folders is big win too IMO. I'll keep using the pre-release with all my projects and I'll be more than happy to provide as much feedback as I can.

Thanks for the help and for the great insight on this new feature! 🙂🎉

Thanks! Feel free to let us any time

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

3 participants