Rmd report got written into `${rmd_file}` source location because of symlink

background:
I use Rmarkdown to get a report for one of my process.

I kept running into the error where the report got written into ${rmd_file} source location. Because ${rmd_file} is stage in as a softlink in workdir.

I was able to solve it by
knit_root_dir = ‘pwd
I didn’t like it.

Then I dig around and I found this old issue in biostars link
That worked. But it is against the spirit of nextflow (Working with files — Nextflow documentation)

    script:
    """
    # cp to avoid rmarkdown known issue with softlinked Rmd files
    cp -L ${rmd_file} notebook.Rmd
    R -e "rmarkdown::render(
        'notebook.Rmd', 
        output_format = 'html_document', 
        output_file = 'output.html',
        params = list(meta = '$metadata', data = '$countData')
        )"
    """

Anyone else out there working rmarkdown in nextflow have some suggestions?
Thank you.

The nf-core module is doing what you’re doing: modules/modules/nf-core/rmarkdownnotebook/main.nf at 25ddc0bb25292280923eed07e6351789a671e86a · nf-core/modules · GitHub

One thing that should work is to change the stageInMode of the process to 'copy', which copies the inputs rather than symlinks them.

process {
    withName: 'RMARKDOWN_PROCESS' {
        stageInMode = 'copy'
    }
}

This used to be a thing with Quarto too, but I think that might have changed since.

Another thing to try is:

output_dir = getwd()

but I believe this is problematic if you’re running on S3 buckets and such.

@mahesh.binzerpanchal Thank you.
I saw these the stageInMode and getwd() also.
Very nice summary of all the possible word around.