-
Notifications
You must be signed in to change notification settings - Fork 445
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
Fix dependency handling in JlinkPlugin (+ general improvements) #1226
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 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 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 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 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 |
---|---|---|
|
@@ -29,7 +29,9 @@ import com.typesafe.sbt.packager.universal.UniversalPlugin | |
*/ | ||
object JlinkPlugin extends AutoPlugin { | ||
|
||
object autoImport extends JlinkKeys | ||
object autoImport extends JlinkKeys { | ||
val JlinkIgnore = JlinkPlugin.Ignore | ||
} | ||
|
||
import autoImport._ | ||
|
||
|
@@ -39,15 +41,61 @@ object JlinkPlugin extends AutoPlugin { | |
target in jlinkBuildImage := target.value / "jlink" / "output", | ||
jlinkBundledJvmLocation := "jre", | ||
bundledJvmLocation := Some(jlinkBundledJvmLocation.value), | ||
jlinkOptions := (jlinkOptions ?? Nil).value, | ||
jlinkOptions ++= { | ||
jlinkIgnoreMissingDependency := | ||
(jlinkIgnoreMissingDependency ?? JlinkIgnore.nothing).value, | ||
// Don't use `fullClasspath in Compile` directly - this way we can inject | ||
// custom classpath elements for the scan. | ||
fullClasspath in jlinkBuildImage := (fullClasspath in Compile).value, | ||
jlinkModules := (jlinkModules ?? Nil).value, | ||
jlinkModules ++= { | ||
val log = streams.value.log | ||
val run = runJavaTool(javaHome.in(jlinkBuildImage).value, log) _ | ||
val paths = fullClasspath.in(jlinkBuildImage).value.map(_.data.getPath) | ||
val shouldIgnore = jlinkIgnoreMissingDependency.value | ||
|
||
// Jdeps has a few convenient options (like --print-module-deps), but those | ||
// are not flexible enough - we need to parse the full output. | ||
val output = run("jdeps", "-R" +: paths) !! log | ||
|
||
val deps = output.linesIterator | ||
// There are headers in some of the lines - ignore those. | ||
.flatMap(PackageDependency.parse(_).iterator) | ||
.toSeq | ||
|
||
// Check that we don't have any dangling dependencies that were not | ||
// explicitly ignored. | ||
val missingDeps = deps | ||
.collect { | ||
case PackageDependency(dependent, dependee, PackageDependency.NotFound) => | ||
(dependent, dependee) | ||
} | ||
.filterNot(shouldIgnore) | ||
.distinct | ||
|
||
if (missingDeps.nonEmpty) { | ||
log.error( | ||
"Dependee packages not found in classpath. You can use jlinkIgnoreMissingDependency to silence these." | ||
) | ||
missingDeps.foreach { | ||
case (a, b) => | ||
log.error(s" $a -> $b") | ||
} | ||
sys.error("Missing package dependencies") | ||
} | ||
|
||
// Collect all the found modules | ||
deps.collect { | ||
case PackageDependency(_, _, PackageDependency.Module(module)) => | ||
module | ||
}.distinct | ||
}, | ||
jlinkOptions := (jlinkOptions ?? Nil).value, | ||
jlinkOptions ++= { | ||
val modules = jlinkModules.value | ||
|
||
val paths = fullClasspath.in(Compile).value.map(_.data.getPath) | ||
val modules = | ||
(run("jdeps", "-R" +: "--print-module-deps" +: paths) !! log).trim | ||
.split(",") | ||
if (modules.isEmpty) { | ||
sys.error("jlinkModules is empty") | ||
} | ||
|
||
JlinkOptions(addModules = modules, output = Some(target.in(jlinkBuildImage).value)) | ||
}, | ||
|
@@ -102,4 +150,43 @@ object JlinkPlugin extends AutoPlugin { | |
private def list(arg: String, values: Seq[String]): Seq[String] = | ||
if (values.nonEmpty) Seq(arg, values.mkString(",")) else Nil | ||
} | ||
|
||
// Jdeps output row | ||
private final case class PackageDependency(dependent: String, dependee: String, source: PackageDependency.Source) | ||
|
||
private final object PackageDependency { | ||
sealed trait Source | ||
|
||
object Source { | ||
def parse(s: String): Source = s match { | ||
case "not found" => NotFound | ||
// We have no foolproof way to separate jars from modules here, so | ||
// we have to do something flaky. | ||
case name | ||
if name.toLowerCase.endsWith(".jar") || | ||
!name.contains('.') || | ||
name.contains(' ') => | ||
JarOrDir(name) | ||
case name => Module(name) | ||
} | ||
} | ||
|
||
case object NotFound extends Source | ||
final case class Module(name: String) extends Source | ||
final case class JarOrDir(name: String) extends Source | ||
|
||
private val pattern = """^\s+([^\s]+)\s+->\s+([^\s]+)\s+([^\s].*?)\s*$""".r | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add examples for some common patterns that occur? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
def parse(s: String): Option[PackageDependency] = s match { | ||
case pattern(dependent, dependee, source) => | ||
Some(PackageDependency(dependent, dependee, Source.parse(source))) | ||
case _ => None | ||
} | ||
} | ||
|
||
object Ignore { | ||
val nothing: ((String, String)) => Boolean = Function.const(false) | ||
val everything: ((String, String)) => Boolean = Function.const(true) | ||
def only(dependencies: (String, String)*): ((String, String)) => Boolean = dependencies.toSet.contains | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/sbt-test/jlink/test-jlink-missing-deps/bar/src/main/java/bar/Bar.java
This file contains 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,3 @@ | ||
package bar; | ||
|
||
public class Bar {} |
This file contains 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 @@ | ||
// Tests jlink behavior with missing dependencies. | ||
|
||
import scala.sys.process.Process | ||
import com.typesafe.sbt.packager.Compat._ | ||
|
||
|
||
// Exclude Scala to simplify the test | ||
autoScalaLibrary in ThisBuild := false | ||
|
||
// Simulate a missing dependency (foo -> bar) | ||
lazy val foo = project.dependsOn(bar % "provided") | ||
lazy val bar = project | ||
|
||
lazy val withoutIgnore = project.dependsOn(foo) | ||
.enablePlugins(JlinkPlugin) | ||
|
||
lazy val withIgnore = project.dependsOn(foo) | ||
.enablePlugins(JlinkPlugin) | ||
.settings( | ||
jlinkIgnoreMissingDependency := JlinkIgnore.only("foo" -> "bar") | ||
) |
7 changes: 7 additions & 0 deletions
7
src/sbt-test/jlink/test-jlink-missing-deps/foo/src/main/java/foo/Foo.java
This file contains 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,7 @@ | ||
package foo; | ||
|
||
public class Foo { | ||
public Foo() { | ||
new bar.Bar(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/sbt-test/jlink/test-jlink-missing-deps/project/plugins.sbt
This file contains 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,8 @@ | ||
{ | ||
val pluginVersion = sys.props("project.version") | ||
if (pluginVersion == null) | ||
throw new RuntimeException("""|The system property 'project.version' is not defined. | ||
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin) | ||
else | ||
addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % sys.props("project.version")) | ||
} |
This file contains 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,5 @@ | ||
> compile | ||
# Should fail since we have a missing dependency. | ||
-> withoutIgnore/jlinkBuildImage | ||
# Should work OK since the issue is silenced | ||
> withIgnore/jlinkBuildImage |
5 changes: 5 additions & 0 deletions
5
src/sbt-test/jlink/test-jlink-missing-deps/withIgnore/src/main/java/WithIgnore.java
This file contains 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,5 @@ | ||
class WithIgnore { | ||
public WithIgnore() { | ||
new foo.Foo(); | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
src/sbt-test/jlink/test-jlink-missing-deps/withoutIgnore/src/main/java/WithoutIgnore.java
This file contains 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,5 @@ | ||
class WithoutIgnore { | ||
public WithoutIgnore() { | ||
new foo.Foo(); | ||
} | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some docs what the the function types represent in `(String, String) => Boolean)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.