Skip to content

[Bug?]: Portable shell mishandles $@ #6968

@gibson042

Description

@gibson042

Self-service

  • I'd be willing to implement a fix

Describe the bug

From the yarn scripting documentation:

Of course, you can also use the $@ array to retrieve the list of all arguments, which will let you reuse them in multiple commands:

{
  "scripts": {
    "build-and-test": "yarn build \"$@\" && yarn test \"$@\""
  }
}

But I'm seeing improper handling of unquoted $@, in particular that separating whitespace is absent (and also that even when quoted, an immediately following adjacent string is incorrectly treated as a separate argument).

To reproduce

Comprehensive exploration:

$ for s in dollarat dollarat-quoted dollarat-adjacent dollarat-quoted-adjacent; do
    printf '# "%s": %s\n' "$s" "$(node -p "JSON.stringify(require('./package.json').scripts['$s'])")"
    yarn run "$s" arg1 'arg2  has  spaces' arg3
  done
# "dollarat": "node -p 'process.argv.slice(1)' -- $@"
[ 'arg1arg2', 'has', 'spacesarg3' ]
# "dollarat-quoted": "node -p 'process.argv.slice(1)' -- \"$@\""
[ 'arg1', 'arg2  has  spaces', 'arg3' ]
# "dollarat-adjacent": "node -p 'process.argv.slice(1)' -- ':'$@':'"
[ ':arg1arg2', 'has', 'spacesarg3:' ]
# "dollarat-quoted-adjacent": "node -p 'process.argv.slice(1)' -- ':'\"$@\"':'"
[ ':arg1', 'arg2  has  spaces', 'arg3', ':' ]

whereas correct behavior would look like this:

 # "dollarat": "node -p 'process.argv.slice(1)' -- $@"
-[ 'arg1arg2', 'has', 'spacesarg3' ]
+[ 'arg1', 'arg2', 'has', 'spaces', 'arg3' ]
 # "dollarat-quoted": "node -p 'process.argv.slice(1)' -- \"$@\""
 [ 'arg1', 'arg2  has  spaces', 'arg3' ]
 # "dollarat-adjacent": "node -p 'process.argv.slice(1)' -- ':'$@':'"
-[ ':arg1arg2', 'has', 'spacesarg3:' ]
+[ ':arg1', 'arg2', 'has', 'spaces', 'arg3:' ]
 # "dollarat-quoted-adjacent": "node -p 'process.argv.slice(1)' -- ':'\"$@\"':'"
-[ ':arg1', 'arg2  has  spaces', 'arg3', ':' ]
+[ ':arg1', 'arg2  has  spaces', 'arg3:' ]

Environment

System:
    OS: Linux 5.15 Debian GNU/Linux 12 (bookworm) 12 (bookworm)
    CPU: (4) x64 Intel(R) Core(TM) i5-4460  CPU @ 3.20GHz
  Binaries:
    Node: 22.20.0 - /tmp/xfs-7c7bcd5f/node
    Yarn: 4.10.3-dev - /tmp/xfs-7c7bcd5f/yarn
    npm: 10.9.3 - /usr/local/bin/npm

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions