Variables trapped in 'if' statment

TLDR: when a variable is set within an ‘if’ block a syntax error might be raised when that variable is accessed outside of this block.

Really appreciating the new features afforded by the update to the Nextflow VS code extension. I’m working through some if the syntax errors in a few nf-core pipeline to get a better understanding of its features and looking at how the stricter syntax is adhered to. One of the syntax errors I can’t see a fix for is when a variable is conditionally set within an if statement. An example of this can be seen on L:423 of bacass/workflow/bacass.nf where the channel ch_to_quast_byrefseq is set within a conditional, and then a few lines later where it is a passed to a process, the syntax error: ch_to_quast_byrefseq is not defined is shown.
After updating to v1.0.2, I have looked around for other similar examples and this might now be limited to only when .set{} is used, so maybe it is as simple as not using .set{} inside if?

Thanks for reporting. I think the issue is that the set operator is being treated like a variable declared with def whereas it should be treated as a variable declared without def.

What’s the difference? Well, if you declare without def then the variable is scoped to the entire workflow definition. Otherwise it is scoped to the nearest enclosing “block” which in this case is the if statement. No particular reason, it’s just how it works out in Groovy.

Anyway, I should be able to fix this in the next release. You can also work around it by replacing set with an assignment:

        ch_to_quast_byrefseq = ch_consensus_byrefseq
            .map {
                refmeta, meta, consensus, ref_fna, ref_gff ->
                    return tuple(refmeta, consensus.flatten(), ref_fna, ref_gff)
            }

Thanks for the explanation @bentsherman, can confirm that assigning the channel variable like that works.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.