Skip to content

Commit

Permalink
Mount pwd in the container when work dir is a symlink
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Oct 27, 2022
1 parent 321486d commit ca39718
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import nextflow.container.ContainerBuilder
import nextflow.container.DockerBuilder
import nextflow.container.SingularityBuilder
import nextflow.exception.ProcessException
import nextflow.file.FileHelper
import nextflow.processor.TaskBean
import nextflow.processor.TaskProcessor
import nextflow.processor.TaskRun
Expand Down Expand Up @@ -562,7 +563,7 @@ class BashWrapperBuilder {
// The current work directory should be mounted only when
// the task is executed in a temporary scratch directory (ie changeDir != null)
// See https://github.com/nextflow-io/nextflow/issues/1710
builder.addMountWorkDir( changeDir as boolean )
builder.addMountWorkDir( changeDir as boolean || FileHelper.getWorkDirIsSymlink() )

builder.build()
return builder
Expand Down
25 changes: 25 additions & 0 deletions modules/nf-commons/src/main/nextflow/file/FileHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,31 @@ class FileHelper {
isPathNFS(Global.session.workDir)
}

/**
* @return
* {@code true} when the current session working directory is a symlink path
* {@code false otherwise}
*/
static boolean getWorkDirIsSymlink() {
isPathSymlink(Global.session.workDir)
}

@Memoized
static boolean isPathSymlink(Path path) {
if( path.fileSystem!=FileSystems.default )
return false
try {
return path != path.toRealPath()
}
catch (NoSuchFileException e) {
return false
}
catch (IOException e) {
log.debug "Unable to determine symlink status for path: $path - cause: ${e.message}"
return false
}
}

/**
* Experimental. Check if a file exists making a second try on NFS mount
* to avoid false negative.
Expand Down
20 changes: 20 additions & 0 deletions modules/nf-commons/src/test/nextflow/file/FileHelperTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -983,4 +983,24 @@ class FileHelperTest extends Specification {
[:] | [:] | [max_error_retry: '5']
}

def 'should check symlink status'() {
given:
def folder = Files.createTempDirectory('test')
def dirReal = folder.resolve('x/y/z'); dirReal.mkdirs()
def link = Files.createSymbolicLink(folder.resolve('link'), folder.resolve('x'))

expect:
!FileHelper.isPathSymlink(Path.of('/bin'))
!FileHelper.isPathSymlink(Path.of('/unknown'))
and:
Files.exists(link)
Files.isSymbolicLink(link)
FileHelper.isPathSymlink(link)
and:
Files.exists(link.resolve('y/z'))
FileHelper.isPathSymlink(link.resolve('y/z'))

cleanup:
folder?.deleteDir()
}
}

0 comments on commit ca39718

Please sign in to comment.