Cross-probe/CTRL+Click from build.gradle into Gradle Source

Answered

I work on and support a lot of Gradle builds. When working on these builds I prefer to use IDEA and I will just open the folder containing the settings.gradle file and generally things work well.

However, I’ve noticed about 50% of the time I can cross-probe/Strl+Click on Gradle DSL and it will decompile and take me to the Gradle source. In fact, if I change the wrapper to define that it uses the -all version of the wrapper then it will be proper source.

Unfortunately, the other 50% of the time all of the Gradle DSL is grayed out and cross-probe doesn’t work at all. In these failure cases when I inspect the IDEA Project Structure (Strl+Shift+Alt+S) the Gradle modules are completely missing. Even if I go and manually add the Gradle module from the cache folder it still doesn’t work correctly.

What is the best/easiest way to fix these cases or what needs to be changed in these projects to enable the cross-probe into Gradle source from build.gradle DSL?

See also: https://discuss.gradle.org/t/cross-probe-ctrl-click-from-build-gradle-into-gradle-source/39989

8 comments
Comment actions Permalink

>the other 50% of the time all of the Gradle DSL is grayed out and cross-probe doesn’t work at all.

Can you give an examples of this? Please provide screenshots as well to see the problem.

>In these failure cases when I inspect the IDEA Project Structure (CTRL+Shift+ALT+S) the Gradle modules are completely missing.

What Gradle modules do you mean? Thanks.

0
Comment actions Permalink

Expected Behavior

IDEA should automatically (or provide an option to) add the Gradle JAR from IDEA's Gradle cache. This JAR is downloaded when IDEA 'syncs' Gradle. In the cases where cross-probe works the IDEA Project Structure has the Gradle modules present. 

Observed Behavior

Andrey Dernov, here is an example for the 50% of the time where it fails:

Notice that cross-probe/Ctrl+Click won't work here and the symbols in the Gradle DSL are grayed out.

Looking at the IDEA Project Structure (Ctrl+Shift+Alt+S) the Gradle modules are completely missing: 

You can reproduce with:

git clone https://github.com/ncimino/transform-extract-mvce
idea.sh transform-extract-mvce

The other 50% of the time where this works, the projects will look like this when opened:

Now you can see the Gradle DSL symbols are being highlighted correctly and Ctrl+Click on sourceCompatiblity goes to the Gradle source:

Looking at the IDEA Project Structure (Ctrl+Shift+Alt+S) I see the Gradle modules:

 

0
Comment actions Permalink

I think I may have found what causes the differences. The times where cross-probing works, the environment variable GRADLE_USER_HOME seems to be set and some ./gradlew command was executed i.e. the Gradle JAR was downloaded to:

$GRADLE_USER_HOME/wrapper/dists/gradle-*/**/*.jar

This is bad, because it means behavior in IDEA is dependent on two things:

  1. GRADLE_USER_HOME environment variable
  2. That a Gradle command was executed to have populated the Gradle user home with a Gradle JAR

Proposed Solutions

I see two potential solutions:

  1. Since IDEA is already downloading the Gradle JAR to the IDEA cache location, then why can't IDEA just add this to the project similar to how IDEA behaves when GRADLE_USER_HOME is set and populated
  2. Have IDEA use the GRADLE_USER_HOME variable (if it is set) for it's cache so that if the user has already set this environment variable and ran commands at command line then that same cache will be shared with IDEA, but if this variable is not set then something like solution number one would still need to be present
0
Comment actions Permalink

To reproduce failure:

git clone https://github.com/ncimino/transform-extract-mvce
idea.sh transform-extract-mvce

NOTICE: Ctrl+Click does not work.

To reproduce working behavior:

git clone https://github.com/ncimino/transform-extract-mvce
setenv GRADLE_USER_HOME $PWD/.gradle-user-home
cd transform-extract-mvce
./gradlew :tasks
cd ..
idea.sh transform-extract-mvce

NOTICE: Ctrl+Click does work.

0
Comment actions Permalink

In IDEA as soon as I changed the:

  • File | Settings | Build, Execution, Deployment | Build Tools | Gradle
  • jetbrains://Idea/settings?name=Build%2C+Execution%2C+Deployment--Build+Tools--Gradle 
    • Gradle user home to a folder on disk

The cross-probing/Ctrl+Click started working along with the pop-up started to show:

  • You can configure Gradle wrapper to use the distribution with sources. It will provide IDE with Gradle… > Okay Apply Suggestions

Why does this have to be set in order for it to work?

0
Comment actions Permalink

IDE uses Gradle behaviour when resolving GRADLE_USER_HOME variable - which is if not defined explicitly results into `~/.gradle` directory, see Gradle user home directory. But user may redefine it explicitly in IDE via Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Gradle user home field.

So if Gradle User Home has been re-defined in IDE and differs from the OS Environment 9command line) gradle User Home - then IDE will not use command line directory. After the Reload action in gradle tool window it should work. The project work for me once I open it in IDE.

>IDEA should automatically (or provide an option to) add the Gradle JAR from IDEA's Gradle cache

The classpath of the build scripts is not the classpath of your application which is added in the Project Structure for modules. Please vote and follow the related request: https://youtrack.jetbrains.com/issue/IDEA-133066 it is about proper build scripts classpath support.

0
Comment actions Permalink

Thanks Andrey Dernov I will follow this ticket.

It seems that perhaps IDEA needs to decouple the build and application classpaths and then automatically add the IDEA Gradle cache JARs to the build classpath so that auto-complete, cross-probing, and syntax highlighting will work out of the box for Gradle projects as soon as 'sync' completes. This would even make it work for plugins.

0

Please sign in to leave a comment.