InitialWorkDirRequirement create config file with tabs and new line chars

I need to use a javascript expression to return a string that has tab-separated lines, but all sorts of escaping seems to happen, so I’m not sure of the appropriate way. This is a reproducible example

cwlVersion: v1.0
class: CommandLineTool
id: example_manifest
requirements:
  - class: DockerRequirement
    dockerPull: alpine:latest
  - class: InlineJavascriptRequirement
  - class: InitialWorkDirRequirement
    listing:
      - entry: |
          ${
             var lines = [];
             for(var i=0; i<inputs.words.length;i++) {
               var line = [inputs.words[i], "stuff"];
               lines.push(line.join('\t'));
             }
             return(lines.join('\n'));
           }
        entryname: "test_manifest"

inputs:
  words: string[]

outputs:
  output:
    type: File
    outputBinding:
      glob: test_manifest

baseCommand: ["true"]

This is fugly and painful, but doing something like this does work, but I want to cry myself to sleep:

  - class: InlineJavascriptRequirement
    expressionLib:
      - "var t = function(s) { var lines = []; for(var i=0; i<s.length;i++){var line = s[i] + '\\tstuff'; lines.push(line);} return lines.join('\\n') + '\\n';}"
  - class: InitialWorkDirRequirement
    listing:
      - entryname: test_manifest
        entry: $(t(inputs.words))

I don’t think you need to mash it all into one line. You do need to double-up backslashes with \\. The rule is that any backslash causes the next character to be skipped. This is something cwltool does to the text of the code before it is passed to the Javascript interpreter.

In CWL 1.2 we are properly specifying the backslash escaping behavior (what you are experiencing is a cwltool-specific behavior, unfortunately) and you will not have to double up the backslash in most cases, so then your original example will work.

Thanks @tetron as always :-). I ended up putting it in a js lib file where i didn’t have to do all the backslashing and could format it better. Just imported it in expressionLib and it worked.

2 Likes