Broken service injection example in docs

Hello,

the service injection documentation contains an example for injecting ObjectFactory to retrieve projectDirectory. This one: https://docs.gradle.org/8.14.2/userguide/service_injection.html#objectfactory (scroll down to the third code box)

I copied the example into my test project but it fails with:

> ./gradlew myInjectedObjectFactoryTask
Calculating task graph as no cached configuration is available for tasks: myInjectedObjectFactoryTask

> Task :myInjectedObjectFactoryTask
C:\src\gradlebuildsrcplugin

[Incubating] Problems report is available at: file:///C:/src/gradlebuildsrcplugin/build/reports/problems/problems-report.html

FAILURE: Build failed with an exception.

* What went wrong:
Configuration cache problems found in this build.

1 problem was found storing the configuration cache.
- Task `:myInjectedObjectFactoryTask` of type `MyObjectFactoryTask`: invocation of 'Task.project' at execution time is unsupported.
  See https://docs.gradle.org/8.14.2/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution

See the complete report at file:///C:/src/gradlebuildsrcplugin/build/reports/configuration-cache/a74tracg56isg6m6pz6u892yq/3fx1yerwxnjg1158ppqqi5yxl/configuration-cache-report.html
> Invocation of 'Task.project' by task ':myInjectedObjectFactoryTask' at execution time is unsupported.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 1s
8 actionable tasks: 1 executed, 7 up-to-date
Configuration cache entry discarded with 1 problem.

My test project can be found here: https://codeberg.org/cwansart/gradle-buildsrc-plugin-test/src/branch/service_injection/buildSrc/src/main/groovy/my-object-factory-task.gradle

Is this a broken example or did I make a mistake?

Well, the example is not exactly broken, it should work “fine” if you do not enable configuration cache as with that using project at task execution time is only deprecated.

With configuration cache enabled, it is already a hard error though, not a deprecation warning anymore.

The example (and also others like the first one in that section) should definitely be updated to not use deprecated features and not fail with configuration cache, especially with Gradle 9 starting to recommend configuration cache for all builds afair, not sure whether it will already be the new default from the top of my head.

So you should definitely report a documentation issue on GitHub.
That example should probably also inject ProjectLayout instead of using project.layout to get it.

1 Like

Great, thanks, I wasn’t sure. “Broken” is the wrong word, deprecated is more appropriate.

Edit: GitHub-Issue: Outdated documentation · Issue #34012 · gradle/gradle · GitHub

1 Like