Berry: [Bug] Syntax error in parsing package.json run scripts with env variable using ' prefixed

Created on 27 Jan 2020  路  5Comments  路  Source: yarnpkg/berry

via @duncanhealy here

Bug description

package.json scripts fail to run if they have environment variables prefixed to the command that
use single ticks '

Command

yarn start
yarn build

What is the current behavior?

yarn 1.x parses package.json correctly
yarn 2.0.0-rc.27 parses package.json and fails with a syntax error below

Syntax Error: Expected "\\" or [^'] but "'" found (line 1, column 8).
    at Xe (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:36:319549)
    at Object.parse (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:36:328727)
    at Object.t.parseShell (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:36:314751)
    at Object.t.execute (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:24:16165)
    at B.r.configuration.reduceHook.script (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:24:5026)
    at Object.B [as executePackageScript] (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:24:5159)
    at async c.execute (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:58:43949)
    at async c.validateAndExecute (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:24:40434)
    at async c.run (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:36:533800)
    at async s.execute (/home/username/.nvm/versions/node/v12.14.1/lib/node_modules/yarn/bin/yarn.js:58:17806)

What is the expected behavior?

yarn start, build .. should parse and run scripts set by scripts block in package.json

Steps to Reproduce

package.json file --> set DEBUG variable to empty / -babel

  "scripts": {
    "start": "DEBUG='-babel' BROWSER=none react-scripts start",
    "build": "DEBUG='' react-scripts build"
}

Workaround

change single ticks ' to \" in scripts

Environment

  • Node Version: 12.14.1
  • Yarn v2 Version: 2.0.0-rc.27
  • OS and version:
bug

All 5 comments

This is my reproduction case:

```js repro
await packageJsonAndInstall({
scripts: {
"start": "DEBUG='' BROWSER=none yarn node -e 'console.log(process.env.BROWSER) '",
"build": "DEBUG='' yarn node -e 'console.log(process.env.DEBUG)'"
}
})

// await expect(yarn(build)).rejects.not.toThrow(
// /Syntax Error/
// )

await expect(yarn(start)).rejects.not.toThrow(
/Command failed/
)
```

I'm having a similar package.json parsing issue: trying to run this script "temp": "cross-env TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} webpack", results in

yarn temp
Syntax Error: Expected "$", "$(", "${", "&&", "'", ";", "<", "<<<", ">", ">>", "\"", "\\", "|", "|&", "||", [ \t], or end of input but "{" found (line 1, column 36).
    at Xe (/usr/local/lib/node_modules/yarn/bin/yarn.js:36:319549)
    at Object.parse (/usr/local/lib/node_modules/yarn/bin/yarn.js:36:328727)
    at Object.t.parseShell (/usr/local/lib/node_modules/yarn/bin/yarn.js:36:314751)
    at Object.t.execute (/usr/local/lib/node_modules/yarn/bin/yarn.js:24:16165)
    at B.r.configuration.reduceHook.script (/usr/local/lib/node_modules/yarn/bin/yarn.js:24:5026)
    at Object.B [as executePackageScript] (/usr/local/lib/node_modules/yarn/bin/yarn.js:24:5159)
    at async c.execute (/usr/local/lib/node_modules/yarn/bin/yarn.js:58:43949)
    at async c.validateAndExecute (/usr/local/lib/node_modules/yarn/bin/yarn.js:24:40434)
    at async c.run (/usr/local/lib/node_modules/yarn/bin/yarn.js:36:533800)
    at async s.execute (/usr/local/lib/node_modules/yarn/bin/yarn.js:58:17806)

yarn version: 2.0.0-rc.27

Same here @fleck.

Trying to execute the package.json script

"lint": "eslint src/**/*.{tsx,ts}"

With yarn lint.

Error:

Syntax Error: Expected "$", "$(", "${", "&&", "'", ";", "<", "<<<", ">", ">>", "\"", "\\", "|", "|&", "||", [ \t], or end of input but "{" found (line 1, column 17).
    at Xe (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:28:319546)
    at Object.parse (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:28:328965)
    at Object.t.parseShell (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:28:314750)
    at Object.t.execute (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:16:16930)
    at r.configuration.reduceHook.script (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:16:5078)
    at /Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:16:5207
    at async s.mktempPromise (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:2:5540)
    at async Object.E [as executePackageScript] (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:16:4838)
    at async c.execute (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:50:44239)
    at async c.validateAndExecute (/Users/fmalmo/Repositories/react-native-prototyping/.yarn/releases/yarn-berry.js:16:41968)

A temporary fix is to replace the src/**/*.{tsx,ts} with src/**/*.tsx src/**/*.ts.

@FredrikAugust Another workaround is "lint": "eslint 'src/**/*.{tsx,ts}'"

Was this page helpful?
0 / 5 - 0 ratings