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< moo > 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