Skip to content

Using for with CLI_ARGS unexpectedly splits arguments containing spaces #2138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
cjlarose opened this issue Mar 25, 2025 · 2 comments
Open
Labels
state: needs triage Waiting to be triaged by a maintainer.

Comments

@cjlarose
Copy link

cjlarose commented Mar 25, 2025

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

@task-bot task-bot added the state: needs triage Waiting to be triaged by a maintainer. label Mar 25, 2025
@cjlarose cjlarose changed the title CLI_ARGS splits arguments containing spaces CLI_ARGS unexpectedly splits arguments containing spaces Mar 25, 2025
@cjlarose
Copy link
Author

cjlarose commented Mar 25, 2025

Digging into this a bit, it's clear that even though for: { var: ... } can loop over arrays, CLI_ARGS is not an array of strings, and instead a string containing all of the (possibly quoted) command line arguments after --, delimited by the space character.

In the example above, CLI_ARGS would be the string containing

inputs/hello.txt inputs/world.txt 'inputs/hello world.txt'

The default behavior of for: { var: ... } for string variables is to split on whitespace, resulting in 4 items:

  1. inputs/hello.txt
  2. inputs/world.txt
  3. 'inputs/hello
  4. world.txt'

It'd be nice if CLI_ARGS was instead an actual array, or if that would be too dramatic a breaking change, a new CLI_ARGS_ARRAY or something would be great.

@cjlarose cjlarose changed the title CLI_ARGS unexpectedly splits arguments containing spaces Using for with CLI_ARGS unexpectedly splits arguments containing spaces Mar 25, 2025
@pd93
Copy link
Member

pd93 commented Mar 26, 2025

@cjlarose Your assessment is exactly right. Unfortunately, the CLI_ARGS variable was added before Task supported variables types other than strings, so all the arguments are combined into a single value. You are able to workaround this today by making a small modification to your Taskfile:

# https://taskfile.dev

version: '3'

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

We decided against changing CLI_ARGS to an array when variables of any type were added because it would've been a breaking change for many users. However, I think it might be nice to create an experiment for this. This way, we can enable the functionality for those who want it without disrupting everyone else and then migrate to it officially for the next major version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
state: needs triage Waiting to be triaged by a maintainer.
Projects
None yet
Development

No branches or pull requests

3 participants