-
Notifications
You must be signed in to change notification settings - Fork 243
Add WorkflowCheck Gradle plugin module #2931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robzienert
wants to merge
2
commits into
temporalio:main
Choose a base branch
from
robzienert:add-workflowcheck-gradle-plugin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Temporal WorkflowCheck Gradle Plugin | ||
|
|
||
| A Gradle plugin that runs the [`temporal-workflowcheck`](../temporal-workflowcheck) bytecode analyzer to detect non-deterministic calls in Temporal Workflow code. | ||
|
|
||
| ## Usage | ||
|
|
||
| Apply the plugin to a Java project that depends on the Temporal Java SDK: | ||
|
|
||
| ```groovy | ||
| plugins { | ||
| id 'java' | ||
| id 'io.temporal.workflowcheck' version '1.39.0' | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation 'io.temporal:temporal-sdk:<version>' | ||
| } | ||
| ``` | ||
|
|
||
| The plugin requires the Temporal Java SDK 1.33.0 or later. | ||
|
|
||
| The plugin automatically: | ||
|
|
||
| - Detects the Temporal SDK version from `runtimeClasspath`. | ||
|
robzienert marked this conversation as resolved.
|
||
| - Downloads the matching `io.temporal:temporal-workflowcheck` analyzer. | ||
| - Registers a `workflowCheck` verification task. | ||
| - Wires `workflowCheck` into the `check` lifecycle. | ||
| - Forks a dedicated JVM to run the analysis, isolated from the Gradle daemon. | ||
|
|
||
| ## Configuration | ||
|
|
||
| All options are configured through the `workflowCheck` extension: | ||
|
|
||
| ```groovy | ||
| workflowCheck { | ||
| // Source sets to analyze. Default: ['main'] | ||
| sourceSets = ['main'] | ||
|
|
||
| // Fail the build on violations. Default: true | ||
| failOnViolation = true | ||
|
|
||
| // Show valid workflow methods alongside invalid ones. Default: false | ||
| showValid = false | ||
|
|
||
| // Skip the default workflowcheck configuration. Default: false | ||
| noDefaultConfig = false | ||
|
|
||
| // Show each analyzed classpath entry at lifecycle log level. When false, entries | ||
| // are logged at info level, visible with `./gradlew workflowCheck --info`. Default: false | ||
| showClasspath = false | ||
|
|
||
| // Additional .properties config files merged on top of defaults | ||
| configFiles.from(file('workflowcheck-overrides.properties')) | ||
|
|
||
| // Override the ASM version used by temporal-workflowcheck, for example for newer JDK support | ||
| asmVersion = '9.9.1' | ||
|
|
||
| // Max heap size for the forked JVM. Default: unset (JVM ergonomics) | ||
| maxHeapSize = '1g' | ||
| } | ||
| ``` | ||
|
|
||
| ## Command-line options | ||
|
|
||
| ```bash | ||
| # Show valid methods in the output | ||
| ./gradlew workflowCheck --show-valid | ||
|
|
||
| # Skip the default workflowcheck config | ||
| ./gradlew workflowCheck --no-default-config | ||
| ``` | ||
|
|
||
| ## Report | ||
|
|
||
| The check report is written to `build/reports/workflowcheck/report.txt`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| plugins { | ||
| id 'java-gradle-plugin' | ||
| } | ||
|
|
||
| description = 'Temporal Java WorkflowCheck Gradle Plugin' | ||
|
|
||
| gradlePlugin { | ||
| plugins { | ||
| workflowCheck { | ||
| id = 'io.temporal.workflowcheck' | ||
| implementationClass = 'io.temporal.workflowcheck.gradle.WorkflowCheckPlugin' | ||
| displayName = 'Temporal WorkflowCheck Plugin' | ||
| description = 'Runs temporal-workflowcheck bytecode analyzer to detect non-deterministic calls in Temporal workflow code' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| testImplementation gradleTestKit() | ||
| testImplementation "junit:junit:${junitVersion}" | ||
| } |
38 changes: 38 additions & 0 deletions
38
...-gradle-plugin/src/main/java/io/temporal/workflowcheck/gradle/WorkflowCheckExtension.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package io.temporal.workflowcheck.gradle; | ||
|
|
||
| import org.gradle.api.file.ConfigurableFileCollection; | ||
| import org.gradle.api.provider.ListProperty; | ||
| import org.gradle.api.provider.Property; | ||
|
|
||
| /** Configuration for the {@code workflowCheck} task. */ | ||
| public abstract class WorkflowCheckExtension { | ||
|
|
||
| public static final String NAME = "workflowCheck"; | ||
|
|
||
| /** Source set names to check. Default: {@code ["main"]}. */ | ||
| public abstract ListProperty<String> getSourceSets(); | ||
|
|
||
| /** Additional {@code .properties} config files merged on top of defaults. */ | ||
| public abstract ConfigurableFileCollection getConfigFiles(); | ||
|
|
||
| /** Fail the build on violations. Default: {@code true}. */ | ||
| public abstract Property<Boolean> getFailOnViolation(); | ||
|
|
||
| /** Show valid workflow methods alongside invalid ones. Default: {@code false}. */ | ||
| public abstract Property<Boolean> getShowValid(); | ||
|
|
||
| /** Skip the default bundled workflowcheck config. Default: {@code false}. */ | ||
| public abstract Property<Boolean> getNoDefaultConfig(); | ||
|
|
||
| /** | ||
| * Show each analyzed classpath entry at lifecycle log level. When {@code false}, entries are | ||
| * still logged at info level, visible with {@code gradle --info}. Default: {@code false}. | ||
| */ | ||
| public abstract Property<Boolean> getShowClasspath(); | ||
|
|
||
| /** Override the ASM version used by temporal-workflowcheck. */ | ||
| public abstract Property<String> getAsmVersion(); | ||
|
|
||
| /** Max heap size for the forked JVM. Default: unset, which uses JVM ergonomics. */ | ||
| public abstract Property<String> getMaxHeapSize(); | ||
| } |
185 changes: 185 additions & 0 deletions
185
...eck-gradle-plugin/src/main/java/io/temporal/workflowcheck/gradle/WorkflowCheckPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| package io.temporal.workflowcheck.gradle; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.stream.Collectors; | ||
| import org.gradle.api.Plugin; | ||
| import org.gradle.api.Project; | ||
| import org.gradle.api.artifacts.Configuration; | ||
| import org.gradle.api.artifacts.ModuleVersionIdentifier; | ||
| import org.gradle.api.artifacts.ResolvedDependency; | ||
| import org.gradle.api.tasks.SourceSet; | ||
| import org.gradle.api.tasks.SourceSetContainer; | ||
| import org.gradle.api.tasks.TaskProvider; | ||
|
|
||
| /** Gradle plugin that runs the temporal-workflowcheck bytecode analyzer. */ | ||
| public class WorkflowCheckPlugin implements Plugin<Project> { | ||
|
|
||
| public static final String CONFIGURATION_NAME = "workflowcheck"; | ||
|
|
||
| @Override | ||
| public void apply(Project project) { | ||
| project | ||
| .getPluginManager() | ||
| .withPlugin( | ||
| "java", | ||
| appliedPlugin -> { | ||
| WorkflowCheckExtension extension = | ||
| project | ||
| .getExtensions() | ||
| .create(WorkflowCheckExtension.NAME, WorkflowCheckExtension.class); | ||
|
|
||
| extension.getSourceSets().convention(Collections.singletonList("main")); | ||
| extension.getFailOnViolation().convention(true); | ||
| extension.getShowValid().convention(false); | ||
| extension.getNoDefaultConfig().convention(false); | ||
| extension.getShowClasspath().convention(false); | ||
|
|
||
| Configuration toolClasspath = | ||
| project | ||
| .getConfigurations() | ||
| .create( | ||
| CONFIGURATION_NAME, | ||
| configuration -> { | ||
| configuration.setCanBeConsumed(false); | ||
| configuration.setCanBeResolved(true); | ||
| configuration.setVisible(false); | ||
| configuration.setDescription( | ||
| "Classpath for the temporal-workflowcheck bytecode analyzer"); | ||
| }); | ||
|
|
||
| SourceSetContainer sourceSets = | ||
| project.getExtensions().getByType(SourceSetContainer.class); | ||
|
|
||
| toolClasspath.defaultDependencies( | ||
| dependencies -> { | ||
| String sdkVersion = | ||
| findTemporalSdkVersion( | ||
| project, sourceSets, extension.getSourceSets().get()); | ||
| if (sdkVersion != null) { | ||
| dependencies.add( | ||
| project | ||
| .getDependencies() | ||
| .create("io.temporal:temporal-workflowcheck:" + sdkVersion)); | ||
| } | ||
| }); | ||
|
|
||
| toolClasspath | ||
| .getResolutionStrategy() | ||
| .eachDependency( | ||
| details -> { | ||
| if ("org.ow2.asm".equals(details.getRequested().getGroup()) | ||
| && extension.getAsmVersion().isPresent()) { | ||
| details.useVersion(extension.getAsmVersion().get()); | ||
| details.because( | ||
| "Overridden by workflowCheck.asmVersion for JDK compatibility"); | ||
| } | ||
| }); | ||
|
|
||
| TaskProvider<WorkflowCheckTask> workflowCheckTask = | ||
| project | ||
| .getTasks() | ||
| .register( | ||
| WorkflowCheckTask.TASK_NAME, | ||
| WorkflowCheckTask.class, | ||
| task -> { | ||
| task.setDescription( | ||
| "Checks Temporal workflow code for determinism violations"); | ||
| task.setGroup("verification"); | ||
|
|
||
| task.getToolClasspath().from(toolClasspath); | ||
| task.getClasspath() | ||
| .from( | ||
| extension | ||
| .getSourceSets() | ||
| .map( | ||
| names -> | ||
| names.stream() | ||
| .map(sourceSets::getByName) | ||
| .map(SourceSet::getRuntimeClasspath) | ||
| .collect(Collectors.toList()))); | ||
| task.dependsOn( | ||
| extension | ||
| .getSourceSets() | ||
| .map( | ||
| names -> | ||
| names.stream() | ||
| .map(sourceSets::getByName) | ||
| .map(SourceSet::getClassesTaskName) | ||
| .collect(Collectors.toList()))); | ||
|
|
||
| task.getConfigFiles().from(extension.getConfigFiles()); | ||
| task.getConfigFileOrder() | ||
| .set( | ||
| project.provider( | ||
| () -> | ||
| extension.getConfigFiles().getFiles().stream() | ||
| .map(project::relativePath) | ||
| .collect(Collectors.toList()))); | ||
| task.getFailOnViolation().set(extension.getFailOnViolation()); | ||
| task.getShowValid().set(extension.getShowValid()); | ||
| task.getNoDefaultConfig().set(extension.getNoDefaultConfig()); | ||
| task.getShowClasspath().set(extension.getShowClasspath()); | ||
| task.getMaxHeapSize().set(extension.getMaxHeapSize()); | ||
| task.getReportFile() | ||
| .set( | ||
| project | ||
| .getLayout() | ||
| .getBuildDirectory() | ||
| .file("reports/workflowcheck/report.txt")); | ||
| }); | ||
|
|
||
| project | ||
| .getTasks() | ||
| .named("check") | ||
| .configure(check -> check.dependsOn(workflowCheckTask)); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Walks resolved runtime dependencies for the configured source sets to find {@code | ||
| * io.temporal:temporal-sdk} and returns its version. | ||
| */ | ||
| static String findTemporalSdkVersion( | ||
| Project project, SourceSetContainer sourceSets, Iterable<String> sourceSetNames) { | ||
| for (String sourceSetName : sourceSetNames) { | ||
| try { | ||
| SourceSet sourceSet = sourceSets.getByName(sourceSetName); | ||
| Configuration runtimeClasspath = | ||
| project.getConfigurations().getByName(sourceSet.getRuntimeClasspathConfigurationName()); | ||
| for (ResolvedDependency dependency : | ||
| runtimeClasspath.getResolvedConfiguration().getFirstLevelModuleDependencies()) { | ||
| String version = findSdkVersionRecursive(dependency); | ||
| if (version != null) { | ||
| return version; | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| project | ||
| .getLogger() | ||
| .warn( | ||
| "WorkflowCheck: Failed to resolve Temporal SDK version from source set {}: {}", | ||
| sourceSetName, | ||
| e.getMessage()); | ||
| } | ||
| } | ||
| project | ||
| .getLogger() | ||
| .warn( | ||
| "WorkflowCheck: No io.temporal:temporal-sdk found on configured source set runtime classpaths. Task will be skipped."); | ||
| return null; | ||
| } | ||
|
|
||
| private static String findSdkVersionRecursive(ResolvedDependency dependency) { | ||
| ModuleVersionIdentifier id = dependency.getModule().getId(); | ||
| if ("io.temporal".equals(id.getGroup()) && "temporal-sdk".equals(id.getName())) { | ||
| return id.getVersion(); | ||
| } | ||
| for (ResolvedDependency child : dependency.getChildren()) { | ||
| String found = findSdkVersionRecursive(child); | ||
| if (found != null) { | ||
| return found; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.