`nx start <app> --args='<args>'` works in a counter-productive way
See original GitHub issueNx (and @nrwl/workspace:run-commands
specifically) seems to do some extremely strange and counter-productive things with the command-line args that are meant to be passed to the primary target as-is.
Context
Our use case is a command-line app (say placed under apps/cli
) that depends on various libraries under libs/
. Whenever we run yarn nx start cli
, we want nx
to check if the cli
app needs to be built first and in that case it should run the build
target for it and all of its dependencies.
This works well and is accomplished via the following configuration in nx.json
:
...
"targetDependencies": {
"start": [
{
"target": "build",
"projects": "self"
}
],
"build": [
{
"target": "build",
"projects": "dependencies"
}
],
...
After building (if needed) we want all CLI args (letβs signify them by ...
), specified via either yarn start cli --args='...'
or yarn start cli -- ...
, to be passed without modification only to the start
target of the cli
app:
apps/cli/project.json
:
...
"start": {
"executor": "@nrwl/workspace:run-commands",
"options": {
"command": "node apps/cli/dist/main.js"
}
},
...
In other words, the command executed for the start
target should be:
node apps/cli/dist/main.js ...
Current Behavior
Nx transmogrifies the CLI args after which it passes them to the commands triggered from the build
targets the libraries that the cli
app depends on.
For example, if we run yarn nx start cli --args='--option1 --option2'
, nx will need to solve the following target dependency graph:
- apps/cli:start ->
- apps/cli:build ->
- libs/lib1:build ->
- libs/lib1:build-part-1 ->
- libs/lib1:codegen ->
- libs/lib1:build-part-2 ->
- libs/lib1:codegen ->
- libs/lib1:build-part-1 ->
- libs/lib1:build ->
- apps/cli:build ->
Which yields the following topological ordering (assuming synchronous/linear execution, not parallel, for simplicity):
libs/lib1:codegen
(executor: @nrwl/workspace:run-commands)libs/lib1:build-part-1
libs/lib1:build-part-2
libs/lib1:build
(executor: @nrwl/workspace:run-commands)apps/cli:build
apps/cli:start
(executor: @nrwl/workspace:run-commands)
Instead of simply running the commands specified by targets 1., 4. and 6., Nx (via yargs
) first transforms --option1 --option2
to --_= --option1=true --option2=true
and appends it to the command string of targets 1, 4 and 6, which causes them to fail.
Expected Behavior
Given command:
yarn nx start cli --args='--option1 --option2'
and topological ordering:
libs/lib1:codegen
(executor: @nrwl/workspace:run-commands)libs/lib1:build-part-1
libs/lib1:build-part-2
libs/lib1:build
(executor: @nrwl/workspace:run-commands)apps/cli:build
apps/cli:start
(executor: @nrwl/workspace:run-commands)
Nx should pass --option1 --option2
only to target 6.
Steps to Reproduce
Let me know if the information provided is sufficient. If not, I can prepare a repo that reproduces the problem.
Failure Logs
β€ yarn nx start cli --args='--option1 --option2'
> NX Running target start for project cli and 5 task(s) it depends on
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
> nx run lib1:code-gen --args="--option1 --option2"
Error occured: Unknown option: --_=
Run with --show-stack-traces to see the full stacktrace
ERROR: Something went wrong in @nrwl/run-commands - Command failed: command --_= --option1=true --option2=true
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
> NX Running target "cli:start" failed
Failed tasks:
- lib1:code-gen
Hint: run the command with --verbose for more details.
Environment
β€ yarn nx report
> NX Report complete - copy this into the issue template
Node : 16.13.2
OS : linux x64
yarn : 3.1.1
nx : 13.8.4
@nrwl/angular : undefined
@nrwl/cli : 13.8.4
@nrwl/cypress : 13.8.4
@nrwl/detox : undefined
@nrwl/devkit : 13.8.4
@nrwl/eslint-plugin-nx : 13.8.4
@nrwl/express : 13.8.5
@nrwl/jest : 13.8.4
@nrwl/js : 13.8.4
@nrwl/linter : 13.8.4
@nrwl/nest : undefined
@nrwl/next : 13.8.4
@nrwl/node : 13.8.4
@nrwl/nx-cloud : undefined
@nrwl/react : 13.8.4
@nrwl/react-native : undefined
@nrwl/schematics : undefined
@nrwl/storybook : 13.8.4
@nrwl/tao : 13.8.4
@nrwl/web : 13.8.4
@nrwl/workspace : 13.8.4
typescript : 4.5.5
rxjs : 7.5.5
---------------------------------------
Community plugins:
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6
Iβve worked around this by wrapping the desired command args in a nested args which is then interpolated in place.
A target for running oclif:
Nesting the args by calling like this:
What worked for me with version 14.8.3:
yarn nx start cli --args=--option1,--option2