Skip to content

Commit 0d01f3c

Browse files
fix: don't fail run when there are missing modulegraphs - ignore them and continue (#158)
If configs are missing moduleGraphs (e.g., because they are custom configs) we log them and ignore them, instead of failing the entire job. This allows more graceful handling of large mono repos which may have multiple custom configs.
1 parent 50ebdeb commit 0d01f3c

6 files changed

Lines changed: 54 additions & 6 deletions

File tree

scala/SnykSbtPlugin-0.1x.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,22 @@ object SnykSbtPlugin extends AutoPlugin {
9494
ConfigBlacklist.contains(c.name) || !c.isPublic
9595
}
9696
val filter = ScopeFilter(configurations = inConfigurations(thisProjectConfigs: _*))
97+
// Use `moduleGraph.?` so configurations that don't have `moduleGraph` defined (e.g. user-defined configs) are
98+
// skipped instead of failing the whole task graph with sbt.internal.util.Init$RuntimeUndefined.
9799
val configAndModuleGraph = Def.task {
98-
val graph = moduleGraph.value
99100
val configName = configuration.value.name
101+
val log = streams.value.log
100102

101-
configName -> graph
103+
moduleGraph.?.value match {
104+
case Some(graph) => Some(configName -> graph)
105+
case None =>
106+
log.warn(s"[snyk] Skipping configuration '$configName' - no moduleGraph defined")
107+
None
108+
}
102109
}
103110

104111
Def.task {
105-
val graphs = configAndModuleGraph.all(filter).value
112+
val graphs = configAndModuleGraph.all(filter).value.flatten
106113

107114
graphs.foldLeft(SnykProjectData(thisProjectId, Map.empty, Map.empty)) {
108115
case (projectData, (configName, graph)) =>

scala/SnykSbtPlugin-1.2x.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,21 @@ object SnykSbtPlugin extends AutoPlugin {
9090
ConfigBlacklist.contains(c.name) || !c.isPublic
9191
}
9292
val filter = ScopeFilter(configurations = inConfigurations(thisProjectConfigs: _*))
93+
// Use `moduleGraph.?` so configurations that don't have `moduleGraph` defined (e.g. user-defined configs) are
94+
// skipped instead of failing the whole task graph with sbt.internal.util.Init$RuntimeUndefined.
9395
val configAndModuleGraph = Def.task {
94-
val graph = moduleGraph.value
9596
val configName = configuration.value.name
96-
configName -> graph
97+
val log = streams.value.log
98+
moduleGraph.?.value match {
99+
case Some(graph) => Some(configName -> graph)
100+
case None =>
101+
log.warn(s"[snyk] Skipping configuration '$configName' - no moduleGraph defined")
102+
None
103+
}
97104
}
98105

99106
Def.task {
100-
val graphs = configAndModuleGraph.all(filter).value
107+
val graphs = configAndModuleGraph.all(filter).value.flatten
101108

102109
graphs.foldLeft(SnykProjectData(thisProjectId, Map.empty, Map.empty)) {
103110
case (projectData, (configName, graph)) =>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Regression fixture with a public, non-standard sbt configuration, specifically missing the `moduleGraph` task.
2+
//
3+
// The Protobuf config below is intentionally declared without DependencyTreeSettings so that
4+
// `Protobuf / moduleGraph` is undefined. The plugin should skip configs missing `moduleGraph` instead of failing the
5+
// whole task graph.
6+
lazy val Protobuf = config("protobuf")
7+
8+
lazy val root = (project in file("."))
9+
.configs(Protobuf)
10+
.settings(
11+
inThisBuild(List(
12+
organization := "com.example",
13+
scalaVersion := "2.12.16",
14+
version := "0.1.0-SNAPSHOT"
15+
)),
16+
name := "Hello",
17+
libraryDependencies += "axis" % "axis" % "1.4",
18+
inConfig(Protobuf)(Defaults.configSettings)
19+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.10.7
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.10.0-RC1")

test/system/plugin.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ test('run legacy inspect() on sbt 1.4.0 with sbt-dependency-graph new naming- ad
138138
expect(result.package.dependencies['axis:axis'].version).toBe('1.4');
139139
});
140140

141+
test('run inspect() on project with custom non-standard config (missing moduleGraph)', async () => {
142+
const result: any = await plugin.inspect(
143+
fixtureDir,
144+
'testproj-1.10-custom-config/build.sbt',
145+
{},
146+
);
147+
148+
// Plugin path should run to completion instead of falling back to legacy `bundled:sbt`.
149+
expect(result.plugin.name).toBe('snyk:sbt');
150+
expect(result.package.packageFormatVersion).toBe('mvn:0.0.1');
151+
expect(result.package.dependencies['axis:axis'].version).toBe('1.4');
152+
});
153+
141154
test('run inspect() proj with provided 1.7', async () => {
142155
const result = await plugin.inspect(
143156
fixtureDir,

0 commit comments

Comments
 (0)