Repository: common-workflow-language/cwltool

Issue:
singularity option does not work correctly 2 step workflow
## Expected Behavior

I execute CWL workflow that has 2 step with `--singularity` .

I setup `CWL_SINGULARITY_CACHE`

```console
$ echo $CWL_SINGULARITY_CACHE
/home/manabu/work/toil/singularity_cache
```

I execute with

```
cwltool --debug --singularity headhead-wf.cwl --source foobar.txt --nlines 1
```


## Actual Behavior

First step works fine cwltool using singularity cached.
Second step does not work, because cwltool try to find wrong place.
It looks `CWL_SINGULARITY_CACHE` twice.

## Workflow Code

here is CommandLineTool

```head-container.cwl
cwlVersion: v1.0
class: CommandLineTool
requirements:
  - class: DockerRequirement
    dockerPull: ubuntu:16.04

baseCommand: [head]
arguments: [-n$(inputs.nlines), $(inputs.source)]
inputs:
  - id: source
    type: File
  - id: nlines
    type: int
outputs:
  - id: out
    type: stdout
stdout: output.txt
```

here is Workflow

```headhead-wf.cwl
cwlVersion: v1.0
class: Workflow

inputs:
  - id: source
    type: File
  - id: nlines
    type: int
steps:
  - id: head1
    run: head-container.cwl
    in:
      - id:  source
        source: source
      - id:  nlines
        source: nlines
    out: [out]
  - id: head2
    run: head-container.cwl
    in:
      - id:  source
        source: head1/out
      - id:  nlines
        source: nlines
    out: [out]


outputs:
  - id: headhead
    type: File
    outputSource: head2/out
```


## Full Traceback
```pytb
INFO /bwfefs/home/manabu/work/toil/toilgithub/bin/cwltool 1.0.20190815141648
INFO Resolved 'headhead-wf.cwl' to 'file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl'
DEBUG Parsed job order from command line: {
    "id": "headhead-wf.cwl",
    "source": {
        "class": "File",
        "location": "file:///bwfefs/home/manabu/work/toil/zatsu/foobar.txt"
    },
    "nlines": 1
}
DEBUG [workflow ] initialized from file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl
INFO [workflow ] start
DEBUG [workflow ] {
    "source": {
        "class": "File",
        "location": "file:///bwfefs/home/manabu/work/toil/zatsu/foobar.txt",
        "size": 21,
        "basename": "foobar.txt",
        "nameroot": "foobar",
        "nameext": ".txt"
    },
    "nlines": 1
}
INFO [workflow ] starting step head1
DEBUG [job step head1] job input {
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head1/source": {
        "class": "File",
        "location": "file:///bwfefs/home/manabu/work/toil/zatsu/foobar.txt",
        "size": 21,
        "basename": "foobar.txt",
        "nameroot": "foobar",
        "nameext": ".txt"
    },
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head1/nlines": 1
}
DEBUG [job step head1] evaluated job input to {
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head1/source": {
        "class": "File",
        "location": "file:///bwfefs/home/manabu/work/toil/zatsu/foobar.txt",
        "size": 21,
        "basename": "foobar.txt",
        "nameroot": "foobar",
        "nameext": ".txt"
    },
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head1/nlines": 1
}
INFO [step head1] start
DEBUG [job head1] initializing from file:///bwfefs/home/manabu/work/toil/zatsu/head-container.cwl as part of step head1
DEBUG [job head1] {
    "source": {
        "class": "File",
        "location": "file:///bwfefs/home/manabu/work/toil/zatsu/foobar.txt",
        "size": 21,
        "basename": "foobar.txt",
        "nameroot": "foobar",
        "nameext": ".txt"
    },
    "nlines": 1
}
DEBUG [job head1] path mappings is {
    "file:///bwfefs/home/manabu/work/toil/zatsu/foobar.txt": [
        "/bwfefs/home/manabu/work/toil/zatsu/foobar.txt",
        "/var/lib/cwl/stg1f0431c2-4670-4ac0-bc05-967f6eb037ae/foobar.txt",
        "File",
        true
    ]
}
DEBUG [job head1] command line bindings is [
    {
        "position": [
            -1000000,
            0
        ],
        "datum": "head"
    },
    {
        "position": [
            0,
            0
        ],
        "valueFrom": "-n$(inputs.nlines)"
    },
    {
        "position": [
            0,
            1
        ],
        "valueFrom": "$(inputs.source)"
    }
]
INFO Using local copy of Singularity image found in /home/manabu/work/toil/singularity_cache
DEBUG [job head1] initial work dir {}
INFO [job head1] /tmp/maaoy1nc$ singularity \
    --quiet \
    exec \
    --contain \
    --pid \
    --ipc \
    --home \
    /tmp/maaoy1nc:/lRgfpn \
    --bind \
    /tmp/0ag_x7fj:/tmp:rw \
    --bind \
    /bwfefs/home/manabu/work/toil/zatsu/foobar.txt:/var/lib/cwl/stg1f0431c2-4670-4ac0-bc05-967f6eb037ae/foobar.txt:ro \
    --pwd \
    /lRgfpn \
    /home/manabu/work/toil/singularity_cache/ubuntu:16.04.sif \
    head \
    -n1 \
    /var/lib/cwl/stg1f0431c2-4670-4ac0-bc05-967f6eb037ae/foobar.txt > /tmp/maaoy1nc/output.txt
INFO [job head1] completed success
DEBUG [job head1] {
    "out": {
        "location": "file:///tmp/maaoy1nc/output.txt",
        "basename": "output.txt",
        "nameroot": "output",
        "nameext": ".txt",
        "class": "File",
        "checksum": "sha1$e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
        "size": 2,
        "http://commonwl.org/cwltool#generation": 0
    }
}
DEBUG [step head1] produced output {
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head1/out": {
        "location": "file:///tmp/maaoy1nc/output.txt",
        "basename": "output.txt",
        "nameroot": "output",
        "nameext": ".txt",
        "class": "File",
        "checksum": "sha1$e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
        "size": 2,
        "http://commonwl.org/cwltool#generation": 0
    }
}
INFO [step head1] completed success
DEBUG [job head1] Removing input staging directory /tmp/o1klvarn
DEBUG [job head1] Removing temporary directory /tmp/0ag_x7fj
INFO [workflow ] starting step head2
DEBUG [job step head2] job input {
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head2/source": {
        "location": "file:///tmp/maaoy1nc/output.txt",
        "basename": "output.txt",
        "nameroot": "output",
        "nameext": ".txt",
        "class": "File",
        "checksum": "sha1$e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
        "size": 2,
        "http://commonwl.org/cwltool#generation": 0
    },
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head2/nlines": 1
}
DEBUG [job step head2] evaluated job input to {
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head2/source": {
        "location": "file:///tmp/maaoy1nc/output.txt",
        "basename": "output.txt",
        "nameroot": "output",
        "nameext": ".txt",
        "class": "File",
        "checksum": "sha1$e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
        "size": 2,
        "http://commonwl.org/cwltool#generation": 0
    },
    "file:///bwfefs/home/manabu/work/toil/zatsu/headhead-wf.cwl#head2/nlines": 1
}
INFO [step head2] start
DEBUG [job head2] initializing from file:///bwfefs/home/manabu/work/toil/zatsu/head-container.cwl as part of step head2
DEBUG [job head2] {
    "source": {
        "location": "file:///tmp/maaoy1nc/output.txt",
        "basename": "output.txt",
        "nameroot": "output",
        "nameext": ".txt",
        "class": "File",
        "checksum": "sha1$e5fa44f2b31c1fb553b6021e7360d07d5d91ff5e",
        "size": 2,
        "http://commonwl.org/cwltool#generation": 0
    },
    "nlines": 1
}
DEBUG [job head2] path mappings is {
    "file:///tmp/maaoy1nc/output.txt": [
        "/tmp/maaoy1nc/output.txt",
        "/var/lib/cwl/stga8be8f3f-962c-470c-8e46-57e7411f5ab2/output.txt",
        "File",
        true
    ]
}
DEBUG [job head2] command line bindings is [
    {
        "position": [
            -1000000,
            0
        ],
        "datum": "head"
    },
    {
        "position": [
            0,
            0
        ],
        "valueFrom": "-n$(inputs.nlines)"
    },
    {
        "position": [
            0,
            1
        ],
        "valueFrom": "$(inputs.source)"
    }
]
INFO ['singularity', 'pull', '--force', '--name', '/home/manabu/work/toil/singularity_cache//home/manabu/work/toil/singularity_cache/ubuntu:16.04.sif', 'docker://ubuntu:16.04']
WARNING: Authentication token file not found : Only pulls of public images will succeed
INFO:    Starting build...
time="2019-09-04T10:13:28+09:00" level=warning msg="\"/run/user/3220\" directory set by $XDG_RUNTIME_DIR does not exist. Either create the directory or unset $XDG_RUNTIME_DIR.: stat /run/user/3220: no such file or directory: Trying to pull image in the event that it is a public image."
FATAL:   Unable to pull docker://ubuntu:16.04: conveyor failed to get: pinging docker registry returned: Get https://registry-1.docker.io/v2/: dial tcp: address registry-1.docker.io: no suitable address found
DEBUG Singularity error
Traceback (most recent call last):
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/job.py", line 636, in run
    getdefault(runtimeContext.tmp_outdir_prefix, DEFAULT_TMP_PREFIX)))
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/singularity.py", line 219, in get_from_requirements
    if not self.get_image(r, pull_image, force_pull):
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/singularity.py", line 163, in get_image
    check_call(cmd, env=env, stdout=sys.stderr)  # nosec
  File "/home/manabu/anaconda2/envs/toil/lib/python3.7/subprocess.py", line 347, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['singularity', 'pull', '--force', '--name', '/home/manabu/work/toil/singularity_cache//home/manabu/work/toil/singularity_cache/ubuntu:16.04.sif', 'docker://ubuntu:16.04']' returned non-zero exit status 255.
ERROR Workflow or tool uses unsupported feature:
Singularity is required to run this tool: Command '['singularity', 'pull', '--force', '--name', '/home/manabu/work/toil/singularity_cache//home/manabu/work/toil/singularity_cache/ubuntu:16.04.sif', 'docker://ubuntu:16.04']' returned non-zero exit status 255.
Traceback (most recent call last):
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/job.py", line 636, in run
    getdefault(runtimeContext.tmp_outdir_prefix, DEFAULT_TMP_PREFIX)))
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/singularity.py", line 219, in get_from_requirements
    if not self.get_image(r, pull_image, force_pull):
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/singularity.py", line 163, in get_image
    check_call(cmd, env=env, stdout=sys.stderr)  # nosec
  File "/home/manabu/anaconda2/envs/toil/lib/python3.7/subprocess.py", line 347, in check_call
    raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['singularity', 'pull', '--force', '--name', '/home/manabu/work/toil/singularity_cache//home/manabu/work/toil/singularity_cache/ubuntu:16.04.sif', 'docker://ubuntu:16.04']' returned non-zero exit status 255.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/main.py", line 830, in main
    logger=_logger)
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/executors.py", line 46, in __call__
    return self.execute(*args, **kwargs)
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/executors.py", line 106, in execute
    self.run_jobs(process, job_order_object, logger, runtime_context)
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/executors.py", line 188, in run_jobs
    job.run(runtime_context)
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/cwltool/job.py", line 665, in run
    "%s is required to run this tool: %s" % (container, Text(err))), err)
  File "/bwfefs/home/manabu/work/toil/toilgithub/lib/python3.7/site-packages/future/utils/__init__.py", line 400, in raise_from
    exec(execstr, myglobals, mylocals)
  File "<string>", line 1, in <module>
cwltool.errors.UnsupportedRequirement: Singularity is required to run this tool: Command '['singularity', 'pull', '--force', '--name', '/home/manabu/work/toil/singularity_cache//home/manabu/work/toil/singularity_cache/ubuntu:16.04.sif', 'docker://ubuntu:16.04']' returned non-zero exit status 255.
```

## Your Environment
* cwltool version: 

Python 3.7.3 | packaged by conda-forge | (default, Jul  1 2019, 21:52:21)

cwltool 1.0.20190815141648



Discussion:

Resolving patch:
diff --git a/cwltool/singularity.py b/cwltool/singularity.py
--- a/cwltool/singularity.py
+++ b/cwltool/singularity.py
@@ -118,6 +118,8 @@ def get_image(dockerRequirement,  # type: Dict[Text, Text]
             if not match:
                 dockerRequirement["dockerPull"] = "docker://" + dockerRequirement["dockerPull"]
         elif "dockerImageId" in dockerRequirement:
+            if os.path.isfile(dockerRequirement['dockerImageId']):
+                found = True
             candidates.append(dockerRequirement['dockerImageId'])
             candidates.append(_normalize_image_id(dockerRequirement['dockerImageId']))
             if is_version_3_or_newer():


Test patch: