Building Intelligently

At Nrwl we recommend folks embrace a monorepo-style development (read more here), which is similar to how Google, Facebook and many other companies build software. This can result in a repo containing dozens of apps and hundreds of libs. Rebuilding and retesting all of them every time we make a change does not scale. That’s why --all should rarely be used.

To show how Nx can improve this experience, imagine we have a repo with two apps and three libs.

Now, say we branched out off master and made a code change. We aren’t interested in rebuilding and retesting everything — we are only interested in rebuilding and retesting the projects that can be affected by our change. We can do it like this:

yarn affected:build --base=master yarn affected:test --base=master yarn affected:lint --base=master yarn affected:e2e --base=master

When using --base , Nx will look at the code changes in our branch (this includes both the committed and uncommitted changes). Nx then will use its code analysis to figure out what the code changes can affect, and will only rerun the minimum set of commands necessary.

Say, for instance, we changed libs/lib1/src/index.ts. After analyzing the repo, Nx will only retest the following projects:

A power tip: Run “yarn affected:dep-graph --base=master” to see what is affected by your PR

When running in the CI, it’s often necessary to specify both the base and the head. We can do that as follows:

yarn affected:build --base=origin/master --head=HEAD yarn affected:test --base=origin/master --head=HEAD yarn affected:lint --base=origin/master --head=HEAD yarn affected:e2e --base=origin/master --head=HEAD

When introducing a major change to a large codebase, we don’t want to retest all the projects affected over and over again. We probably want to retest only the ones that failed last time. With Nx, it’s easy:

yarn affected:build --base=master --only-failed yarn affected:test --base=master --only-failed yarn affected:lint --base=master --only-failed yarn affected:e2e --base=master --only-failed

Finally, we can run --help to see all the available options supported by affected:* commands.