Skip to content

Using for with CLI_ARGS unexpectedly splits arguments containing spaces #2138

@cjlarose

Description

@cjlarose

Description

mkdir inputs
echo 'hello' > inputs/hello.txt
echo 'world' > inputs/world.txt
echo 'hello world' > inputs/"hello world".txt
$ ls -l inputs 
total 24
-rw-r--r--  1 chrislarose  staff  12 Mar 24 17:22 hello world.txt
-rw-r--r--  1 chrislarose  staff   6 Mar 24 17:22 hello.txt
-rw-r--r--  1 chrislarose  staff   6 Mar 24 17:22 world.txt

Taskfile.yml

# https://taskfile.dev

version: '3'

tasks:
  default:
    cmds:
      - for: { var: CLI_ARGS, as: FILENAME }
        cmd: echo "{{.FILENAME}}"

Then

task default -- inputs/hello.txt inputs/world.txt inputs/"hello world".txt

produces unexpectedly

task: [default] echo "inputs/hello.txt"
inputs/hello.txt
task: [default] echo "inputs/world.txt"
inputs/world.txt
task: [default] echo "'inputs/hello"
'inputs/hello
task: [default] echo "world.txt'"
world.txt'

The command was executed 4 times, even though only 3 arguments were sent to task after --. I personally ran into this when trying to invoke task using xargs

 $ find inputs -type f | xargs task default --
task: [default] echo "inputs/world.txt"
inputs/world.txt
task: [default] echo "inputs/hello"
inputs/hello
task: [default] echo "world.txt"
world.txt
task: [default] echo "inputs/hello.txt"
inputs/hello.txt

Version

Task version: v3.42.1 (h1:HOaFbZGLOrAy2V/dLsX2rGJZVG2Qx6268KUIAIXdNE4=)

Operating system

macOS Sonoma 14.1.2

Experiments Enabled

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions