nx icon indicating copy to clipboard operation
nx copied to clipboard

Allow nx affected to run with a whitelist --only

Open AlexJWayne opened this issue 3 years ago • 0 comments

Description

Allow nx affected to only execute targets on a subset of projects.

Motivation

Let's say you have an app that requires special setup to run its tests. Like maybe a database. Or one app requires it be run inside a certain container. Or many other things.

In your CI, you don't want to perform this setup for all projects. So you create one job for your pure JS projects that runs:

nx affected --target test --exclude my-server-app

And another job that runs:

bash do-special-setup.sh
npx nx affected:apps --plain | grep -q 'my-server-app' && npx nx test my-server-app || echo 'apps/my-server-app is unaffected'

Which works but sucks because:

  • The grep is brittle, and would match on my-server-app-e2e. You could improve that with some regex but this gets ugly fast.
  • grep will exit with a non zero code if it fails to match, which would fail CI when everything is fine. So you have to remember to add || something to catch that.

or

bash do-special-setup.sh
npx nx affected test --exclude=app-a,app-b,lib-c,lib-d

Which works but sucks because:

  • When adding new apps or libs you'd have to add them here as well. And it's not a place you would think to look.

Suggested Implementation

This could be vastly simplified by adding an --only option to nx affected which sets up a whitelist of projects to run the target on.

npx nx affected --target test --only my-server-app

This compliments the --exclude option (a blacklist) nicely, IMHO.

Alternate Implementations

Maybe add a --if-affected to the command runner?

nx test my-server-app --if-affected

I don't like this as much because it executing targets on only affected projects is the domain and responsibility of nx affected.

AlexJWayne avatar Aug 05 '22 20:08 AlexJWayne