# StackOverflowError when chaining modules for nf-test

**URL:** <https://community.seqera.io/t/stackoverflowerror-when-chaining-modules-for-nf-test/2211>\
**Category:** Ask for help\
**Tags:** nextflow, nf-core, nf-test\
**Created:** [June 23, 2025, 2:35am UTC](https://community.seqera.io/t/stackoverflowerror-when-chaining-modules-for-nf-test/2211 "2025-06-23T02:35:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcmero](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mcmero/32/1869_2.png) [@mcmero](https://community.seqera.io/u/mcmero)\
**Post date:** [June 23, 2025, 2:35am UTC](https://community.seqera.io/t/stackoverflowerror-when-chaining-modules-for-nf-test/2211/1 "2025-06-23T02:35:17Z")

</div>

I’m submitting a module for nf-core (see [PR](https://github.com/nf-core/modules/pull/8652)). My nf-test takes the output file from my first module `preparesegmentation` and uses it as input for my second module `runsegmentationontile`. I have checked the [nf-test docs on writing chained modules](https://nf-co.re/docs/tutorials/tests_and_test_data/nf-test_writing_tests) and am using a similar structure. Here is my test:

```nextflow
    test("nuclei - json, images") {

        setup {
            run("VIZGENPOSTPROCESSING_PREPARESEGMENTATION") {
                script "../../preparesegmentation/main.nf"
                params {
                    module_args = '--tile-size 200 --tile-overlap 20'
                }
                process {
                    """
                    // Create test directory and files before process execution
                    testDir = file("tmpdir", type: 'dir')
                    testDir.mkdir()

                    // Download source file
                    sourceUrl = 'https://github.com/nf-core/test-datasets/raw/modules/data/imaging/segmentation/nuclear_image.tif'
                    sourceFile = file(sourceUrl)

                    // Create renamed copies
                    sourceFile.copyTo(file("\${testDir}/mosaic_DAPI_z3.tif"))
                    sourceFile.copyTo(file("\${testDir}/mosaic_PolyT_z3.tif"))

                    // Create transform file
                    transformFile = file("\${testDir}/micron_to_mosaic_pixel_transform.csv")
                    transformFile.text = "2 0 0\\n0 2 0\\n0 0 1"

                    input[0] = [
                        [id:'test'],
                        testDir,
                        transformFile
                    ]
                    input[1] = file('https://raw.githubusercontent.com/Vizgen/vpt-plugin-cellpose2/refs/heads/develop/example_analysis_algorithm/cellpose2_nuclei.json', checkIfExists: true)
                    input[2] = "mosaic_(?P<stain>[\\\\w|-]+)_z(?P<z>[0-9]+).tif"
                    """
                }
            }
        }

        when {
            process {
                """
                input[0] = [
                    [id:'test'],
                    file("tmpdir", type: 'dir'),
                    VIZGENPOSTPROCESSING_PREPARESEGMENTATION.out.segmentation_files[0][1], // Segmentation parameters
                    0 // Tile index
                ]
                input[1] = file('https://raw.githubusercontent.com/Vizgen/vpt-plugin-cellpose2/refs/heads/develop/example_analysis_algorithm/cellpose2_nuclei.json', checkIfExists: true)
                input[2] = []
                """
            }
        }

```

However, I can’t seem to get the main nf-test to pass (see [here](https://github.com/nf-core/modules/actions/runs/15721130227/job/44302203908?pr=8652#step:6:846)):

```auto
Assertion failed: 

assert process.success
       | |
       | false
       VIZGENPOSTPROCESSING_RUNSEGMENTATIONONTILE

java.lang.RuntimeException: Process has no output channels. process.out can not be used.

```

and in my `nextflow.log`:

```auto
 ERROR ~ Unexpected error [StackOverflowError]

```

I can get the test to work by removing the setup block and using a config file on disk, so it is something to do with the module chaining. I know my first module works correctly and passes nf-test, creating the correct output file so I am quite puzzled by this error. I have also tried using `map` and `take` in case it was some issue with accessing the channel using `[0]`, but this leads to the same issue. Any ideas?

---

<div class="post-metadata">

**Author:** ![mribeirodantas](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mribeirodantas/32/235_2.png) [@mribeirodantas](https://community.seqera.io/u/mribeirodantas)\
**Post date:** [June 23, 2025, 1:24pm UTC](https://community.seqera.io/t/stackoverflowerror-when-chaining-modules-for-nf-test/2211/2 "2025-06-23T13:24:31Z")

</div>

This error suggests that the `when` block is not correct and as it’s not running successfully, there is no output channels to assert. As you said it works with actual files and without the setup, I’d say what you’re receving from your previous process (in the `setup` block) is probably different from what you’re feeding with the actual files. Could you please check that this is not the case?

---

<div class="post-metadata">

**Author:** ![mcmero](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/mcmero/32/1869_2.png) [@mcmero](https://community.seqera.io/u/mcmero)\
**Post date:** [June 24, 2025, 2:45am UTC](https://community.seqera.io/t/stackoverflowerror-when-chaining-modules-for-nf-test/2211/3 "2025-06-24T02:45:00Z")

</div>

Thanks, the problem was with the `when` block. Accessing `VIZGENPOSTPROCESSING_PREPARESEGMENTATION.out.segmentation_files[0]` seems to throw this error, although I am not sure exactly why. If I use channel operators to retrieve the output, as well as construct the whole segmentation input channel it works. Here’s my solution:

```nextflow
        when {
            process {
                """
                VIZGENPOSTPROCESSING_PREPARESEGMENTATION.out.segmentation_files
                    .flatten()
                    .last()
                    .set { segmentation_params_ch }

                Channel.of([
                    [id:'test'],
                    file("tmpdir", type: 'dir'),
                ])
                .combine(segmentation_params_ch) // segmentation params JSON file
                .combine(Channel.of(0)) // tile_index
                .set { seg_input_ch }

                input[0] = seg_input_ch
                input[1] = file('https://raw.githubusercontent.com/Vizgen/vpt-plugin-cellpose2/refs/heads/develop/example_analysis_algorithm/cellpose2_nuclei.json', checkIfExists: true)
                input[2] = []
                """
            }
        }

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [July 1, 2025, 2:45am UTC](https://community.seqera.io/t/stackoverflowerror-when-chaining-modules-for-nf-test/2211/4 "2025-07-01T02:45:48Z")

</div>

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