Make `Pkg.build` error message clearer.

(cherry picked from commit 6ae21d1e47a9dfab12c195e537fce9fdbada16e0)
Ref: #9158