nextflow-io / nextflow

A DSL for data-driven computational pipelines
http://nextflow.io
Apache License 2.0
2.61k stars 606 forks source link

ERROR ~ BUG! exception in phase 'conversion' in source unit 'Script_773b20f0a9f08db0' unexpected NullPointerException #5039

Closed julibeg closed 1 month ago

julibeg commented 1 month ago

Bug report

Expected behavior and actual behavior

When using an if-clause in map and not emitting anything, this just results in an empty channel

workflow {
    Channel.of(1, 2, 3)
    | map { if (it < 0) it }
    | ifEmpty ( "empty" )
    | view
}
// this prints "empty"

I'd expect multiMap to behave the same, but the below

workflow {
    res = Channel.of(1, 2, 3)
    | multiMap {
        squared: it ** 2
        squared_negative_only: if (it < 0) it ** 2
    }
    res.squared_negative_only
    | ifEmpty("empty")
    | view
}

fails with

ERROR ~ BUG! exception in phase 'conversion' in source unit 'Script_40e50a1fd5c204e0' unexpected NullPointerException

Environment

bentsherman commented 1 month ago

It's not really semantically correct because you aren't returning a value for the else branch. I think Groovy implicitly returns null but it's an edge case I wouldn't rely on. Instead I would do this:

workflow {
  res = Channel.of(1, 2, 3)
  res_squared = res | map { it * 2 }
  res_squared_negative = res_squared | filter { it < 0 }

  res.squared_negative
    | ifEmpty("empty")
    | view
}