Repository navigation
Document supported WorkParameters types - #38952
Conversation
Signed-off-by: hej090224 <fc49854985@gmail.com>
lkasso
left a comment
There was a problem hiding this comment.
Thanks for tackling this — the information is genuinely useful, and the NamedDomainObjectContainer warning in particular is a common gotcha worth documenting. A few things I'd like us to adjust before merging:
Placement. The new subsection sits between "Step 2. Convert to the Worker API" and "Step 3. Change the isolation mode," which interrupts the numbered walkthrough. It reads better as a peer top-level section between "Worker Daemons" and "Cancellation and timeouts."
Isolation mechanism (line 173). The current wording — "copied for noIsolation(), and are serialized and sent to the worker for classLoaderIsolation() and processIsolation()" — isn't quite right. All three modes take an isolated copy of the parameters via IsolatableFactory.isolate(params). Only processIsolation() additionally serializes them for IPC to the worker daemon (see DefaultActionExecutionSpecFactory#newIsolatedSpec and #newTransportableSpec).
Provider<T> in the managed-types list (line 180). A raw Provider<T> getter on a WorkParameters interface isn't wired up as a managed property — AbstractClassGenerator#isPropertyType only recognizes Property, HasMultipleValues, and MapProperty. Recommending Property<T> is the safer, more idiomatic guidance.
NamedDomainObjectContainer failure mode (line 192). "Container type is not preserved during isolation" implies partial success. The real failure is at serialization time: containers aren't Serializable, so the fallback path fails outright.
Shape. The list is exhaustive but doesn't tell readers what to reach for by default. It reads more usefully with a small example interface at the top, a "prefer managed types" section with a type-mapping table and the why (lazy, CC-friendly, fingerprinted), and the Serializable fallback framed as a last resort with explicit costs rather than as a peer option.
Here's a suggested rewrite. Two steps: (1) delete the section from its current location in the walkthrough, (2) add this new top-level section between "Worker Daemons" and "Cancellation and timeouts":
[[work_parameter_types]]
== Work parameter types
A `WorkParameters` type is an interface that declares a getter for each value the work action needs.
A typical parameters interface looks like this:
[source,java]
----
public interface MyWorkParameters extends WorkParameters {
Property<String> getMessage();
RegularFileProperty getOutputFile();
ConfigurableFileCollection getInputFiles();
ListProperty<String> getFlags();
}
----
Before executing the work, Gradle takes an isolated copy of the parameters.
The work action reads from that copy, so changes made to the parameter objects after `submit()` do not affect the running work.
For `processIsolation()`, Gradle also serializes the parameters to transport them to the worker daemon process.
=== Prefer Gradle managed types
Use Gradle managed types for parameter values wherever possible.
Managed types are lazy, isolatable, and compatible with the <<configuration_cache.adoc#config_cache,configuration cache>>, and they let Gradle fingerprint parameter values for up-to-date checks and the build cache.
[cols="1,2"]
|===
|Use |For
|link:{javadocPath}/org/gradle/api/provider/Property.html[`Property<T>`]
|A single scalar value
|link:{javadocPath}/org/gradle/api/file/RegularFileProperty.html[`RegularFileProperty`], link:{javadocPath}/org/gradle/api/file/DirectoryProperty.html[`DirectoryProperty`]
|A file or directory location
|link:{javadocPath}/org/gradle/api/file/ConfigurableFileCollection.html[`ConfigurableFileCollection`]
|A collection of files
|link:{javadocPath}/org/gradle/api/provider/ListProperty.html[`ListProperty<T>`], link:{javadocPath}/org/gradle/api/provider/SetProperty.html[`SetProperty<T>`], link:{javadocPath}/org/gradle/api/provider/MapProperty.html[`MapProperty<K, V>`]
|A collection of values
|link:{javadocPath}/org/gradle/api/Named.html[`Named`] objects, <<build_services.adoc#build_services,build services>>
|Named or shared state
|===
Plain scalar values (`String`, `Boolean`, boxed primitives, enums, `File`, `null`) are also isolatable and work as parameter values, but wrapping them in a `Property<T>` defers value resolution and lets Gradle track the input.
=== Types to avoid
Gradle model containers such as link:{javadocPath}/org/gradle/api/NamedDomainObjectContainer.html[`NamedDomainObjectContainer`] are not supported as work parameters.
They are not `Serializable`, so isolation fails when the work is submitted.
Represent their contents with a collection property such as `SetProperty<T>` or `ListProperty<T>`, or `MapProperty<String, T>` if you need to preserve names.
=== Java serialization fallback
Values that do not match the types above fall back to Java serialization if they implement `java.io.Serializable`.
Avoid this path when possible: serialized values are opaque to Gradle's input fingerprinting and lazy evaluation, so they weaken up-to-date checks and configuration cache reuse.
An unsupported or non-serializable value causes parameter isolation to fail when the work is submitted.I verified the rewrite against the code (AbstractValueProcessor, DefaultActionExecutionSpecFactory, AbstractClassGenerator) and ran ./gradlew :docs:checkDeadInternalLinks locally — all cross-references resolve. Thanks again for the contribution!
Signed-off-by: hej090224 <fc49854985@gmail.com>
|
Thanks for the detailed review and suggested structure. I moved the content into a standalone top-level section between Worker Daemons and Cancellation and timeouts, corrected the isolation and process-isolation serialization explanation, and reorganized the guidance around managed types with a type-mapping table. I also clarified the unsupported Validation passed:
The PR is ready for another look. |
Fixes #38765
Summary
WorkParametersand distinguish this from the additional IPC serialization required byprocessIsolation().NamedDomainObjectContainerare unsupported and that Java serialization is a last-resort fallback with input tracking and configuration-cache trade-offs.Verification
./gradlew.bat :docs:userguideMultiPage— passed; the task reported existing configuration-cache problems from the documentation/Asciidoctor dependency graph and discarded that cache entry../gradlew.bat :workers:embeddedIntegTest --tests org.gradle.workers.internal.WorkerExecutorParametersIntegrationTest --tests org.gradle.workers.internal.WorkerExecutorParametersKotlinIntegrationTest --console=plain— passed.DefaultIsolatableFactoryTestand the WorkerParameters integration tests..\gradlew.bat :docs:checkDeadInternalLinks --console=plain— passed; the documentation link check completed successfully..\gradlew.bat :docs:userguideMultiPage --console=plain— passed; Gradle reported 18 existing configuration-cache problems from the documentation/Asciidoctor dependency graph and discarded that cache entry..\gradlew.bat :workers:embeddedIntegTest --tests org.gradle.workers.internal.WorkerExecutorParametersIntegrationTest --tests org.gradle.workers.internal.WorkerExecutorParametersKotlinIntegrationTest --rerun-tasks --console=plain— passed; both Worker parameters integration test classes executed successfully.git diff --check— passed..\gradlew.bat :docs:checkDeadInternalLinks --no-configuration-cache --console=plain— could not run because this checkout enables Isolated Projects, which does not allow disabling the configuration cache; the same check passed with the repository default configuration.