berry: [Bug] Syntax error in parsing package.json run scripts containing parameter substitutions or {}

Describe the bug

i am unable to run a npm script that contains parameter substitutions or the find command’s {} syntax with yarn 2, though yarn 1 and npm are both able to run it without issue.

To Reproduce

Reproduction

Playground

const {mkdir, writeFile} = require('fs/promises');

await mkdir('dist');

await writeFile('dist/example.d.ts', '// Example typescript definition file', 'utf8');

await packageJsonAndInstall({
  scripts: {
    "flowtypes": "find . -type f -not -path './node_modules/*' -regex '.*/dist/[^/]*.d.ts' -exec sh -c 'echo $1; echo ${1%.*.*}.js.flow;' _ {} \\;",
  }
});

await expect(yarn(`flowtypes`)).rejects.not.toThrow(`Command failed`);

Environment:

  • OS: macOS 11.2.3
  • Node version: 15.12.0
  • Yarn version: 2.4.0-git.20210324.hash-a307bddb

Additional context

the real command i am trying to run is a find command that runs flowgen on every dist/*.d.ts file that isn’t in node_modules:

find . -type f -not -path './node_modules/*' -regex '.*/dist/[^/]*.d.ts' -exec sh -c 'yarn flowgen $1 -o ${1%.*.*}.js.flow' _ {} \\;

i tried to wrap the potentially problematic portions of the command with single quotes as recommended in https://github.com/yarnpkg/berry/issues/797#issuecomment-607724596 and in https://github.com/mysticatea/npm-run-all/issues/200, but i’m still hitting these syntax issues.

while this example is a pretty specific command that is unlikely to affect many people, a much more common issue is the case of "start": "run-p start:*" (or similar), which is a common pattern for packages and which breaks in yarn 2 unless start:* is wrapped in single quotes.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 16 (1 by maintainers)

Most upvoted comments

How are you supposed to use sh default value for variables?

"start": "NODE_ENV=${NODE_ENV:?} node --no-warnings --experimental-specifier-resolution=node --loader ts-node/esm -r envk src/index.ts",

Complains about:

Syntax Error: Expected ":+", ":+}", ":-", ":-}", "}", or [$@*?#a-zA-Z0-9_\-] but ":" found (line 1, column 20).

Without workspaces everything works fine. What’s happening?

@theclaw37 maybe you should check the code of conduct before leaving such comments. And maybe appreciate all the hard work the contributors have put in. And you could even contribute yourself instead of just waiting for someone else to do it for you 😉