Core Nx Tutorial - Step 4: Build Affected Projects

Nx scales your development by doing code change analysis to see what apps or libraries are affected by a particular pull request.

Commit all the changes in the repo:

git add .

git commit -am 'init'

git checkout -b testbranch

Open packages/cli/src/ascii.go and change the go code:

1package main 2 3import ( 4 "fmt" 5 "os" 6) 7 8func check(e error) { 9 if e != nil { 10 panic(e) 11 } 12} 13 14func main() { 15 fmt.Println("Hello, Dish of the Day") 16 dat, err := os.ReadFile("../ascii/assets/cow.txt") 17 check(err) 18 fmt.Print(string(dat)) 19} 20

Run nx print-affected --select=projects, and you should see cli printed out. The nx print-affected looks at what you have changed and uses the project graph to figure out which projects are affected by this change.

Now revert those changes

git checkout .

Make a change to the shared ASCII art

Update packages/ascii/assets/cow.txt:

1 _____ 2< Hi! > 3 ----- 4 \ ^__^ 5 \ (oo)\_______ 6 (__)\ )\/\ 7 ||----w | 8 || || 9

Run nx print-affected --select=projects, and this time you should see cli, blog and ascii printed out.

Build Affected Projects

Printing the affected projects can be handy, but usually you want to do something with them. For instance, you may want to build everything that has been affected.

Run nx affected -t build to rebuild only the projects affected by the change.

✔ nx run blog:build (1s)

✔ nx run cli:build (2s)

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> NX Successfully ran target build for 2 projects (2s)

See Nx Cloud run details at https://nx.app/runs/XfhRFaOyGCE

Note that Nx only built blog and cli. It didn't build ascii because there is no build script created for it.

Affected -t *

You can run any target against the affected projects in the graph like this:

nx affected -t test

What's Next