Skip to content

Commit 6b22903

Browse files
committed
Don't generate empty snapshots with config-cache reuse
With configuration-cache enabled, no dependencies will be resolved when the configuration is loaded from the cache. In this case, we avoid generating a dependency graph snapshot so that we don't incorrectly report an empty graph. Fixes #96 Fixes #98
1 parent 5d3976f commit 6b22903

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/BaseExtractorTest.groovy

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.networknt.schema.*
66
import groovy.json.JsonSlurper
77
import groovy.transform.CompileDynamic
88
import groovy.transform.CompileStatic
9-
import groovy.transform.Memoized
109
import org.gradle.github.dependencygraph.fixture.TestConfig
1110
import org.gradle.internal.hash.Hashing
1211
import org.gradle.test.fixtures.SimpleGradleExecuter
@@ -49,11 +48,14 @@ abstract class BaseExtractorTest extends Specification {
4948

5049
SimpleGradleExecuter createExecuter() {
5150
// Create a new JsonManifestLoader for each invocation of the executer
52-
File manifestFile = reportDir.file("dummy-job-correlator.json")
53-
loader = new JsonRepositorySnapshotLoader(manifestFile)
51+
loader = new JsonRepositorySnapshotLoader(dependencyGraphFile)
5452
return createExecuter(testGradleVersion)
5553
}
5654

55+
File getDependencyGraphFile() {
56+
reportDir.file("dummy-job-correlator.json")
57+
}
58+
5759
static String getTestGradleVersion() {
5860
return System.getProperty("testGradleVersion", GradleVersion.current().version)
5961
}
@@ -219,20 +221,20 @@ abstract class BaseExtractorTest extends Specification {
219221
@CompileStatic
220222
protected static class JsonRepositorySnapshotLoader {
221223
private static final String SCHEMA = "schema/github-repository-snapshot-schema.json"
222-
private final File manifestFile
224+
private final File dependencyGraphFile
223225

224-
JsonRepositorySnapshotLoader(File manifestFile) {
225-
this.manifestFile = manifestFile
226+
JsonRepositorySnapshotLoader(File dependencyGraphFile) {
227+
this.dependencyGraphFile = dependencyGraphFile
226228
}
227229

228230
protected Map jsonRepositorySnapshot() {
229231
def jsonSlurper = new JsonSlurper()
230-
println(manifestFile.text)
232+
println(dependencyGraphFile.text)
231233
JsonSchema schema = createSchemaValidator()
232234
ObjectMapper mapper = new ObjectMapper()
233-
JsonNode node = mapper.readTree(manifestFile)
235+
JsonNode node = mapper.readTree(dependencyGraphFile)
234236
validateAgainstJsonSchema(schema, node)
235-
return jsonSlurper.parse(manifestFile) as Map
237+
return jsonSlurper.parse(dependencyGraphFile) as Map
236238
}
237239

238240
private static void validateAgainstJsonSchema(JsonSchema schema, JsonNode json) {

plugin-test/src/test/groovy/org/gradle/github/dependencygraph/DependencyExtractorConfigTest.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ class DependencyExtractorConfigTest extends BaseExtractorTest {
7676
manifest.assertResolved([
7777
"org.test:foo:1.0": [package_url: purlFor(foo)]
7878
])
79+
80+
// Execute again to test loading from config-cache
81+
when:
82+
dependencyGraphFile.delete()
83+
executer.withArgument("--configuration-cache")
84+
def buildResult = run()
85+
86+
then:
87+
buildResult.output.contains("Gradle build state was reused from the configuration-cache: Dependency Graph file will not be generated.")
88+
!dependencyGraphFile.exists()
7989
}
8090

8191
def "fails gracefully if configuration values not set"() {

plugin/src/main/kotlin/org/gradle/dependencygraph/extractor/DependencyExtractor.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ abstract class DependencyExtractor :
2929

3030
private val pluginParameters = PluginParameters()
3131

32+
private var settingsEvaluated = false
33+
3234
private val resolvedConfigurations = Collections.synchronizedList(mutableListOf<ResolvedConfiguration>())
3335

3436
private val thrownExceptions = Collections.synchronizedList(mutableListOf<Throwable>())
@@ -112,6 +114,7 @@ abstract class DependencyExtractor :
112114
open fun extractSettings(
113115
details: EvaluateSettingsBuildOperationType.Details
114116
) {
117+
settingsEvaluated = true
115118
val settingsFile = details.settingsFile
116119
if (settingsFile != null) {
117120
buildLayout.addSettings(details.buildPath, settingsFile)
@@ -296,6 +299,15 @@ abstract class DependencyExtractor :
296299
thrownExceptions
297300
)
298301
}
302+
303+
// We use the absence of Settings Evaluated to determine if the build was loaded from the configuration-cache
304+
if (!settingsEvaluated) {
305+
LOGGER.lifecycle(
306+
"Gradle build state was reused from the configuration-cache: " +
307+
"Dependency Graph file will not be generated."
308+
)
309+
return
310+
}
299311
try {
300312
writeDependencyGraph()
301313
} catch (e: RuntimeException) {

0 commit comments

Comments
 (0)