gradle / gradle-completion

Gradle tab completion for bash and zsh
MIT License
1.02k stars 131 forks source link

Problem with multiproject without `build.gradle` in root. #137

Open alcarraz opened 1 year ago

alcarraz commented 1 year ago

Hi, this was working wonderfully for me until now, thank you for the great work.

I started a multi project build according to Structuring and Building a Software Component with Gradle and it doesn't have a build.gradle in the project root. The gradle init command doesn't generate a build.gradle in the project root, either.

In that case, the gradle-completion doesn't find the build file and defaults to assuming it is not an initialized gradle project, giving only the common options.

Maybe it can be assumed that if the settings file is found, the project is initialized, even if the build file is not found? I'm willing to add a PR, or a patch, if I manage to solve this and it works.

I found a workaround for this creating an empty build.gradle, but before that, well I'll tell the story because it may be worthy to add a warning or something in the README.md about that.

There is also the possibility that the script works, and somehow I did something to break it, in that case, please excuse me and let me know.

To reproduce, try this:

mkdir test-project
cd test-project
gradle init # select application, java, **yes** on multiple subprojects, groovy.

And then try to autocomplete gradle there. It will only show the default tasks.

vvvlasov commented 2 months ago

Hi! We're facing the same issue and would like to see this fixed. Do you need any help with your pull request?

vvvlasov commented 2 months ago

Ok, actually, what I found out is that the culprit is this command

$("$gradle_cmd" -b "$gradle_build_file" --daemon --no-scan --console=plain -q tasks --all)

tasks --all only lists the tasks for sub-projects. If one wants the root project tasks tasks without --all args is what you need. However tasks doesn't list sub-project tasks. What a surprising behaviour! Basically all I did to make it work is concatenated two outputs.

  if [[ ! -z "$("$gradle_cmd" --status 2>/dev/null | grep IDLE)" ]]; then
      gradle_tasks_output="$("$gradle_cmd" -b "$gradle_build_file" --daemon --no-scan --console=plain -q tasks --all) << $("$gradle_cmd" -b "$gradle_build_file" --daemon --no-scan --console=plain -q tasks)"
  else
      gradle_tasks_output="$("$gradle_cmd" -b "$gradle_build_file" --no-daemon --no-scan --console=plain -q tasks --all) << $("$gradle_cmd" -b "$gradle_build_file" --no-daemon --no-scan --console=plain -q tasks)"
  fi
alcarraz commented 2 months ago

Thank you, @vvvlasov, I actually made a PR (#138), showing a possible naive solution, but I didn't get any feedback on that. But I'm not sure if this is the same issue.