snyk / snyk-gradle-plugin

Basic Snyk CLI plugin for Gradle support
Other
25 stars 19 forks source link

Feature request: Gradle configuration cache compatibility #239

Closed trevjonez closed 11 months ago

trevjonez commented 1 year ago

Expected behaviour

Any configuration or plugin injected to gradle projects should be configuration cache compatible to ensure the snyk plugin does not become a developer productivity bottleneck.

Actual behaviour

Currently the init script that is injected causes issues with configuration cache enabled projects.

FAILURE: Build failed with an exception.

* Where:
Initialization script '/tmp/tmp-9973-iT3FzCHr8J4G--init.gradle' line: 308

* What went wrong:
Maximum number of configuration cache problems has been reached.
This behavior can be adjusted, see https://docs.gradle.org/7.5.1/userguide/configuration_cache.html#config_cache:usage:max_problems.

69 problems were found storing the configuration cache, 1 of which seems unique.
- Initialization script '/tmp/tmp-9973-iT3FzCHr8J4G--init.gradle': invocation of 'Task.project' at execution time is unsupported.
  See https://docs.gradle.org/7.5.1/userguide/configuration_cache.html#config_cache:requirements:use_project_during_execution

See the complete report at file:///home/**/project/build/reports/configuration-cache/**/configuration-cache-report.html
> Invocation of 'Task.project' by task ':snykResolvedDepsJson' at execution time is unsupported.

Steps to reproduce

add the following to a gradle project's gradle.properties

org.gradle.unsafe.configuration-cache=true
org.gradle.unsafe.configuration-cache-problems=warn
org.gradle.unsafe.configuration-cache.max-problems=5

Invoke the snyk added tasks with the init script added.

DEBUG="foo" ./gradlew --init-script gradle/init.d/snyk.gradle -Pconfiguration=^prodReleaseRuntimeClasspath$ -PonlySubProject=app snykResolvedDepsJson --stacktrace

Workaround for CI builds

add a sed command before invoking the snyk CLI to disable configuration caching.

sed -i"" -e "s/org.gradle.unsafe.configuration-cache=true/org.gradle.unsafe.configuration-cache=false/g" gradle.properties
Thinking out loud

As mentioned in https://github.com/snyk/snyk-gradle-plugin/issues/192#issuecomment-1240028727 I think it is likely time the snyk gradle integration is reworked to use a full pre-published gradle plugin. This would allow testing via gradleTestKit and potentially being able to simplify the main snyk cli by relocating the invocation configuration to a gradle DSL checked in by the consumer.

magdziarek commented 1 year ago

Hi @trevjonez, thanks for reporting, we appreciate your suggestions and explanations. For now, our suggested workaround is using the Gradle --no-configuration-cache flag which will switch off the feature even if the property is set to true in gradle.properties. To include the Gradle flag with snyk command, it has to be preceded by double dash, e.g. snyk test -- --no-configuration-cache. I have reported your request. Until we have the time to work on a long-term solution, we are planning to add this flag by default to the command we run in the init script.