Skip to content

Commit

Permalink
Handle potential null-stream in buildinfo plugin (#2838)
Browse files Browse the repository at this point in the history
The current Scala implementation does not load the expected values
from the resource file, when the resource file can not be found on the
classpath. The Java implementation already throws a `RuntimeException`
(of an `IOException`) in this case. The Scala implementation does not.

I think missing a generated resource file at runtime is most likely an
runtime issue and we should also throw such exception in the Scala
implementation to exit early, before a missing value can cause greater
harm later. Hence, we now also throw a `RuntimeException` when the
resource file/stream could not be found in the Scala implementation.

Pull request: #2838
  • Loading branch information
lefou authored Oct 12, 2023
1 parent 160c96e commit 5c34391
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions contrib/buildinfo/src/mill/contrib/buildinfo/BuildInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ object BuildInfo {
|public class $buildInfoObjectName {
| $bindingsCode
|
| public static java.util.Map<String, String> toMap(){
| public static java.util.Map<String, String> toMap() {
| Map<String, String> map = new HashMap<String, String>();
| $mapEntries
| return map;
Expand Down Expand Up @@ -173,13 +173,17 @@ object BuildInfo {
|object $buildInfoObjectName {
| private[this] val buildInfoProperties: java.util.Properties = new java.util.Properties()
|
| private[this] val buildInfoInputStream = getClass
| {
| val buildInfoInputStream = getClass
| .getResourceAsStream("${buildInfoObjectName}.buildinfo.properties")
|
| try {
| buildInfoProperties.load(buildInfoInputStream)
| } finally {
| buildInfoInputStream.close()
| if(buildInfoInputStream == null)
| throw new RuntimeException("Could not load resource ${buildInfoObjectName}.buildinfo.properties")
| else try {
| buildInfoProperties.load(buildInfoInputStream)
| } finally {
| buildInfoInputStream.close()
| }
| }
|
| $bindingsCode
Expand Down

0 comments on commit 5c34391

Please sign in to comment.