Skip to content
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

yarn run --if-present option flag to avoid exiting with a non-zero exit code when the script is undefined #6894

Closed
HavaH123 opened this issue Jan 9, 2019 · 23 comments

Comments

@HavaH123
Copy link

HavaH123 commented Jan 9, 2019

Do you want to request a feature or report a bug?
feature

What is the current behavior?
yarn run does not have an option to exit with a zero code if the script run is not defined in package.json.

My use case is that I manage a mono-repo using yarn and lerna and I use something like
lerna exec --scope -- yarn run script-name or run yarn run script-name in a loop over all the packages.

This is fragile because it breaks if any of the packages dont have the script-name defined.

What is the expected behavior?

Similar to npm, provide a --if-present option that exits with a zero code if the script is undefined.
yarn run --if-defined script-name

Please mention your node.js, yarn and operating system version.

$ node -v
v8.15.0
$ yarn -v
1.12.3
$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.13.6
BuildVersion:	17G3025
@ghost ghost assigned rally25rs Jan 9, 2019
@ghost ghost added the triaged label Jan 9, 2019
@astorije
Copy link

astorije commented Jan 23, 2019

npm run has a --if-present flag, and it would be really handy if there was feature parity on this. Is there another way to achieve that with Yarn?
(Our use case is a shared CI configuration that runs a few scripts such as yarn test and yarn lint on a few repositories, not all of which have all those scripts)

EDIT: I missed that @HavaH123 had mentioned npm's --if-present in his issue description.

@rally25rs
Copy link
Contributor

Thanks for the info about npm's --if-present flag. I didn't know that existed. I think this is a good idea. Care to work up a PR? 😸

@Tranced
Copy link

Tranced commented Jan 28, 2019

@rally25rs Hey, I'd like to take this on!

@peijiesim
Copy link
Contributor

Hi @Tranced, are you actively working on this? If not, I'd like to give it a shot. Thanks.

@KingScooty
Copy link

KingScooty commented Aug 13, 2019

Any news on this? I'm really missing a feature like this at the moment. Would be amazing to get this in.

@kachkaev
Copy link

kachkaev commented Aug 21, 2019

As a maintainer of a monorepo with a few packages, I'm also interested in having --if-present. Some of them require build before publishing, but some do not due to their simplicity. Some are in TS, some are in pure JS. Some have tests, some do not. What I want to do in CI is to call top-level build, lint:tsc and test commands without caring if they exist in every package.

// top-level package.json (ideal scenario)
"scripts": {
  "build": "yarn workspaces run --if-present build",
  // ...
  "lint:tsc": "yarn workspaces run --if-present lint:tsc",
  "test": "yarn workspaces run --if-present test"
},

Until --if-present is available, some of my packages/×/package.json files have to contain these dummy scripts for the top-level commands to work:

// packages/×/package.json
"scripts": {
  "build": "echo \"build is not configured for package \\\"×\\\", skipping.\"",
  "lint:tsc": "echo \"TypeScript is not configured for package \\\"×\\\", skipping.\"",
  "test": "echo \"Unit testing is not configured for package \\\"×\\\", skipping.\""
},

That's clearly redundant.

After accidentally hearing about npm run --if-present I got quite exited, being sure that the same flag exists for Yarn Workspaces too. However, it now seems that this is not the case and I should undo my premature deletion of dummy scripts 😁

@mbpreble
Copy link
Contributor

mbpreble commented Oct 6, 2019

I'd like to pick this up - it wasn't too bad to get working locally, hope to have something reviewable soon.

mbpreble pushed a commit to mbpreble/yarn that referenced this issue Oct 6, 2019
…cripts

When --if-present is provided, yarn run will exit without error, even if the specified script is not
defined in package.json.

implement yarnpkg#6894
mbpreble pushed a commit to mbpreble/yarn that referenced this issue Oct 6, 2019
If --if-present is set, yarn run will complete successfully if the specified package script is not
defined in pckage.json.

re yarnpkg#6894
mbpreble pushed a commit to mbpreble/yarn that referenced this issue Oct 6, 2019
* If --if-present is set, yarn run will complete successfully if the specified package script is not
defined in package.json.
@mbpreble
Copy link
Contributor

mbpreble commented Oct 6, 2019

Opened MR #7603 for this feature.

@mbpreble
Copy link
Contributor

mbpreble commented Oct 6, 2019

Didn't see @ulentini's open MR for this, the one I produced is not much different.
Should this issue be closed? The conversation on that MR stalled, this doesn't seem like something the maintainer wants added to yarn.

@molant
Copy link

molant commented Oct 10, 2019

Also looking forward this.

@mbpreble I'm looking at the other PR (#7159) and I think some of the feedback still applies to yours. Notably this comment:

Wouldn't silencing with || true also silence any errors if the script exist but fails? Imagine running a script like test, test:e2e or lint. There's definitely a use-case for exiting with 0 if the script just doesn't exist, especially for things like generalized CI-pipelines.

Maybe if there's a test case for that it will have a higher change of being accepted?

@mbpreble
Copy link
Contributor

That comment seems to stand well enough on its own as being true. I agree this feature does provide a different behavior than just ignoring all errors when invoking yarn.

There are a few people people asking for this. @arcanis - could you take another look and help move this toward a resolution one way or the other?

@lcswillems
Copy link

lcswillems commented Oct 12, 2019

Maybe @rally25rs could be deassigned to this issue because he doesn't seem involved anymore and maybe somebody else could be assigned? Somebody willing to do it like @mbpreble .

I would really like this feature too!

@emiliskiskis
Copy link

Issue still persists in March 30, 2020. Why did this issue go stale?

@sdc224
Copy link

sdc224 commented Jun 19, 2020

Guys any update over this issue?

@arcanis
Copy link
Member

arcanis commented Jun 19, 2020

yarn workspaces foreach run ... (exclusive to the v2) already skips scripts that don't exist, and together with its options you have the same behavior as described in the op without having to add a lengthy option to run.

@arcanis arcanis closed this as completed Jun 19, 2020
@astorije
Copy link

astorije commented Jun 21, 2020

Is there a way to benefit from this without using workspaces? My workflow described above (#6894 (comment)) doesn't use them.

htbkoo added a commit to htbkoo/personal-portfolio that referenced this issue Feb 14, 2021
…s `yarn` does not support `if-present` argument, reference: yarnpkg/yarn#6894 (comment))
@airtonix
Copy link

airtonix commented May 15, 2021

this doesn't work as advertised.

❯ yarn workspaces foreach run setup
➤ YN0000: No matches found: "setup:*"
➤ YN0000: Done in 0s 526ms

still throws exit 1

edit:

you have to run it like:

yarn workspaces foreach --include '@airtonix-react-native/app-*' run setup

@risen228
Copy link

risen228 commented Nov 2, 2021

I really don't understand why it's still not implemented. This is the only problem I experiencing with yarn day by day.

@Tofandel
Copy link

Tofandel commented Apr 3, 2022

without having to add a lengthy option to run

So instead we have to use a very lengthy command 😵‍💫

@michaelfaith
Copy link

@arcanis why was this closed out? The foreach option you mentioned isn't a suitable alternative for what's being requested.

ferame added a commit to ferame/hostinger-task that referenced this issue Dec 11, 2022
It seems to be an npm functionality that is not replicated on yarn with yarnpkg/yarn#6894
@jaikumar-tj
Copy link

Can you reopen this issue? It seems the issue is not fixed yet and the alternate suggestions wasn't resolving the issue

@AntonyF-Andreani
Copy link

Please reopen this issue/request of feature.

In my case in CICD, we have an workarround:

run: echo "result=[[ $(cat package.json| jq .scripts.lint) != null ]]" >> $GITHUB_OUTPUT.

But for interoperability between pnpm, npm and yarn, would be awesome have this feature.

@ranisalt
Copy link

The argument to close this issue is quite stupid. I don't always want to run on every possible workspace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests