1) GN has a new "friend" feature that lets you whitelist targets to include private headers.
2) "gn help friend" has more detail and examples.
3) Please write new targets with a well-defined public API and enforce this in the build! This is now more practical (it wasn't before) and really helps build sanitation.
MORE INFO
GN's binary targets (for compiling C++) have a variable called "public" that almost nobody uses. People should use this more! This is the equivalent to "hdrs" in Blaze/Bazel, and is a way for you to list your target's public API.
Most targets list all headers and source files in "sources". This means they're implicitly public, meaning that other targets can include these headers if there is a direct dependency, or public dependency chain going the right direction. If you define a "public" variable, headers in "sources" become implicitly private and targets that depend on your target can only include the headers listed in the "public" list (don't put .cc files in "public", they won't get compiled).¹
One problem with "public" was that you couldn't unit test your internal APIs because the test target can only include your public headers. With the "friend" concept, you can now whitelist targets like your component's tests that can bypass your public API (you still need a proper dependency between the two targets).
GN CHECK
The header checking only works when "gn check" is enabled for your target.
Large portions of Chrome do not have this enabled (see "check_targets" in src/.gn). Every once in a while somebody complains to me about something that broke and it was clearly a GN bug that this obvious problem wasn't identified. But the problem is actually that header checking wasn't enabled for the code in question. Please volunteer to add header checking for more targets and put them in the whitelist in the .gn file.
Brett
¹ This magic flipping of meaning of "sources" is a bit confusing and it would be nice to be more strict like Blaze, but the historical lack of public APIs in Chrome would make this annoying.
Comment 1 by eugene...@chromium.org
, Apr 11 2018