gradle / gradle

Adaptable, fast automation for all
https://gradle.org
Apache License 2.0
16.85k stars 4.71k forks source link

Error reporting on ResolvableConfiguration is not contextual #27029

Open lacasseio opened 11 months ago

lacasseio commented 11 months ago

Current Behavior

When declaring a dependency on ResolvableConfiguration, through Configuration#getDependencies(), the error should show in the "what went wrong" is incomplete, causing confusion to the user.

* What went wrong:
A problem occurred evaluating root project 'gradle-8.4-configuration'.
> Could not create domain object 'foooo' (ResolvableConfiguration)

Expected Behavior

When declaring a dependency on ResolvableConfiguration, either through Configuration#getDependencies() or DependencyHandler, the error should show in the "what went wrong":

* What went wrong:
A problem occurred evaluating root project 'gradle-8.4-configuration'.
> Could not create domain object 'foooo' (ResolvableConfiguration)
  > Dependencies can not be declared against the `foooo` configuration

Context (optional)

This is a classic mistake of a new Gradle developer of not declaring the exception thrown as Contextual. Note that "you can see the full stack through build scans" is not an acceptable solution.

Steps to Reproduce

Running help on the following build script will showcase the issue:

configurations.resolvable('foooo') {
    dependencies.add(project.dependencies.create('com.example:foo:4.2'))
}

Gradle version

8.5-rc-1 (but most likely affects all previous relevant Gradle version)

Build scan URL (optional)

No response

Your Environment (optional)

No response

cobexer commented 11 months ago

Thank you for providing a valid report.

The issue is in the backlog of the relevant team and is prioritized by them.

jvandort commented 2 weeks ago

I have submitted a PR to resolve this https://github.com/gradle/gradle/pull/30916

We use an InvalidUserCodeException, which is contextual, instead of a GradleException.

Though this only feels like a band-aid to me. There are 181 calls to create GradleException instances, so this is probably not the only place where we are failing to print context to the console.

Do you know why all exceptions are not treated as contextual? I feel like it is too easy to not use a contextual exception, or to write a new exception type and fail to mark it as contextual.