Add walk_deps to generated_file()

generated_file() currently uses deps and data_deps for two things: as
the roots of the metadata walk, and as dependencies of its Ninja build
statement. This makes it impossible to collect metadata by walking a
large graph without either:
1) Making consumers wait for everything that was walked (e.g. waiting
   for all C++ compiles in order to list the shared libraries of an
   APK), or
2) Creating a dependency cycle (e.g. when walking an APK target that
   itself depends on the consumer of the generated file).

This adds a walk_deps variable to generated_file(): additional roots
for the metadata walk that are not dependencies, and which may form
cycles with deps. They are tracked by the Builder like validations
(resolve once defined, finalize once resolved), and targets that use
them are written after all targets are resolved, like those that set
collect_validations_metadata. The set of delayed targets is unchanged
for builds that do not use walk_deps.

Like deps, walk_deps are subject to visibility and testonly checks.

The top-level generated_file() is now marked as walked before its walk
starts, so its own metadata is never collected, even when the walk leads
back to it (through walk_deps, or through validations when
collect_validations_metadata is true).

"gn analyze" treats a generated_file() as affected by the targets
listed in its walk_deps, without treating those targets as being built
by it. "gn refs" lists generated_file() targets as references to their
walk_deps.

A follow-up adds dep_keys, which allows depending on only the subset of
walked targets that produce the collected data.

Bug: 579603584
Change-Id: Ib823a15c08ad8e2c98d0a350749f5ac035b6b7bd
Agent-Generated: true
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/28240
Commit-Queue: Andrew Grieve <agrieve@google.com>
Reviewed-by: David Turner <digit@google.com>
diff --git a/docs/reference.md b/docs/reference.md
index d81b026..f7e4415 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -175,6 +175,7 @@
     *   [transparent: [bool] True if the bundle is transparent.](#var_transparent)
     *   [validations: [label list] Validation dependencies.](#var_validations)
     *   [visibility: [label list] A list of labels that can depend on a target.](#var_visibility)
+    *   [walk_deps: [label list] Metadata walk roots that are not dependencies.](#var_walk_deps)
     *   [walk_keys: [string list] Key(s) for managing the metadata collection walk.](#var_walk_keys)
     *   [weak_frameworks: [name list] Name of frameworks that must be weak linked.](#var_weak_frameworks)
     *   [weak_libraries: [file list] File of libraries that must be weak linked.](#var_weak_libraries)
@@ -600,6 +601,7 @@
   testonly
   validations
   visibility
+  walk_deps
   walk_keys
   weak_frameworks
   weak_libraries
@@ -2186,6 +2188,10 @@
 
   By default, validations dependencies are never visited by metadata collection,
   but setting `collect_validations_metadata = true` changes this behavior.
+
+  The metadata collection walk can also start from targets that are not
+  dependencies of the generated_file() target by listing them in `walk_deps`.
+  See `gn help walk_deps`.
 ```
 
 #### **Variables**
@@ -2198,7 +2204,7 @@
            output_extension, output_name, public, sources, testonly,
            visibility
   Generated file: contents, data_keys, rebase, walk_keys, output_conversion,
-                  outputs, collect_validations_metadata
+                  outputs, collect_validations_metadata, walk_deps
 ```
 
 #### **Example (metadata collection)**
@@ -7443,6 +7449,44 @@
   any targets in "//bar/" and any subdirectory thereof.
     visibility = [ "./*", "//bar/*" ]
 ```
+### <a name="var_walk_deps"></a>**walk_deps**: Metadata walk roots that are not dependencies.&nbsp;[Back to Top](#gn-reference)
+
+```
+  Valid on generated_file() targets that collect metadata.
+
+  The metadata walk starts from these targets in addition to deps and
+  data_deps, without making them Ninja dependencies of the generated_file()
+  target (allowing cycles with deps). Like "gen_deps", listed targets are
+  generated when the generated_file() target is generated. Like "deps", they
+  are subject to "visibility" and "testonly" checks.
+
+  They are used only when writing the generated_file() target itself, and not
+  when the metadata walk of another target passes through it. The metadata of
+  the generated_file() target itself is never collected, even when the walk
+  leads back to it.
+
+  See "gn help generated_file".
+```
+
+#### **Example**
+
+```
+  # Lists the libraries of an app without depending on them, even though the
+  # app depends on the list.
+  generated_file("app_libs_list") {
+    outputs = [ "$target_gen_dir/app_libs.txt" ]
+    data_keys = [ "lib_names" ]
+    walk_deps = [ ":app" ]
+  }
+
+  action("app") {
+    ...
+    deps = [
+      ":app_libs_list",
+      ":some_lib",
+    ]
+  }
+```
 ### <a name="var_walk_keys"></a>**walk_keys**: Key(s) for managing the metadata collection walk.&nbsp;[Back to Top](#gn-reference)
 
 ```
@@ -8541,7 +8585,8 @@
   A generated_file target can declare either 'contents' to write statically
   known contents to a file or 'data_keys' to aggregate metadata and write the
   result to a file. It can also specify 'walk_keys' (to restrict the metadata
-  collection), 'output_conversion', and 'rebase'.
+  collection), 'walk_deps' (to collect metadata from targets that are not
+  dependencies), 'output_conversion', and 'rebase'.
 ```
 
 #### **Collection and Aggregation**
diff --git a/src/gn/analyzer.cc b/src/gn/analyzer.cc
index 5c1d5e0..a724472 100644
--- a/src/gn/analyzer.cc
+++ b/src/gn/analyzer.cc
@@ -252,6 +252,10 @@
       for (const auto& validation_target_pair : item->AsTarget()->validations())
         dep_map_.insert(std::make_pair(validation_target_pair.ptr, item));
 
+      // A generated_file() is affected by the targets that it walks.
+      for (const auto& walk_dep_pair : item->AsTarget()->walk_deps())
+        walk_dep_map_.insert(std::make_pair(walk_dep_pair.ptr, item));
+
       for (const auto& dep_config_pair : item->AsTarget()->configs())
         dep_map_.insert(std::make_pair(dep_config_pair.ptr, item));
 
@@ -486,6 +490,10 @@
   auto dep_end = dep_map_.upper_bound(item);
   for (auto cur_dep = dep_begin; cur_dep != dep_end; ++cur_dep)
     AddAllItemsReferringToItem(cur_dep->second, all_affected_items);
+
+  auto walk_deps = walk_dep_map_.equal_range(item);
+  for (auto cur_dep = walk_deps.first; cur_dep != walk_deps.second; ++cur_dep)
+    AddAllItemsReferringToItem(cur_dep->second, all_affected_items);
 }
 
 bool Analyzer::WereMainGNFilesModified(
diff --git a/src/gn/analyzer.h b/src/gn/analyzer.h
index 77aed2a..492cea2 100644
--- a/src/gn/analyzer.h
+++ b/src/gn/analyzer.h
@@ -95,6 +95,12 @@
   // Maps items to the list of items that depend on them.
   std::multimap<const Item*, const Item*> dep_map_;
 
+  // Maps targets to the generated_file() targets that list them in walk_deps.
+  // Their contents are derived from the metadata of the targets they walk, but
+  // the walked targets are not their dependencies, so they are not in
+  // |dep_map_|.
+  std::multimap<const Item*, const Item*> walk_dep_map_;
+
   const SourceFile build_config_file_;
   const SourceFile dot_file_;
   const SourceFileSet build_args_dependency_files_;
diff --git a/src/gn/analyzer_unittest.cc b/src/gn/analyzer_unittest.cc
index c5aa8d4..43b09b7 100644
--- a/src/gn/analyzer_unittest.cc
+++ b/src/gn/analyzer_unittest.cc
@@ -797,4 +797,34 @@
       "}");
 }
 
+// Tests that a generated_file() is affected by the targets that it walks, and
+// that walk_deps do not prevent the walked targets from being built by "all".
+TEST_F(AnalyzerTest, WalkDeps) {
+  // app -> final -> pickles --walk_deps--> app
+  std::unique_ptr<Target> app = MakeTarget("//dir", "app");
+  app->sources().push_back(SourceFile("//dir/app.cc"));
+  std::unique_ptr<Target> final = MakeTarget("//dir", "final");
+  std::unique_ptr<Target> pickles = MakeTarget("//dir", "pickles");
+  pickles->set_output_type(Target::GENERATED_FILE);
+  pickles->walk_deps().push_back(LabelTargetPair(app.get()));
+  final->private_deps().push_back(LabelTargetPair(pickles.get()));
+  app->private_deps().push_back(LabelTargetPair(final.get()));
+  builder_.ItemDefined(std::move(app));
+  builder_.ItemDefined(std::move(final));
+  builder_.ItemDefined(std::move(pickles));
+  builder_.ItemDefined(MakeTarget("//dir", "unrelated"));
+
+  RunAnalyzerTest(
+      R"({
+       "files": [ "//dir/app.cc" ],
+       "additional_compile_targets": [ "all" ],
+       "test_targets": [ "//dir:pickles", "//dir:unrelated" ]
+       })",
+      "{"
+      R"("compile_targets":["//dir:app"],)"
+      R"/("status":"Found dependency",)/"
+      R"("test_targets":["//dir:pickles"])"
+      "}");
+}
+
 }  // namespace gn_analyzer_unittest
diff --git a/src/gn/builder.cc b/src/gn/builder.cc
index d2413f1..bf5ec83 100644
--- a/src/gn/builder.cc
+++ b/src/gn/builder.cc
@@ -285,6 +285,15 @@
       !AddValidationDeps(record, target->validations(), err))
     return false;
 
+  // walk_deps of generated_file() targets are not real dependencies, and can
+  // form cycles with them. Like validations, they only need to be defined for
+  // the target to be resolved, which provides the target pointers needed by
+  // the (delayed) metadata walk, propagates the "should_generate" flag, and
+  // reports undefined targets.
+  if (target->output_type() == Target::GENERATED_FILE &&
+      !AddValidationDeps(record, target->walk_deps(), err))
+    return false;
+
   // All targets in the default toolchain get generated by default. We also
   // check if this target was previously marked as "required" and force setting
   // the bit again so the target's dependencies (which we now know) get the
@@ -546,6 +555,9 @@
         !ResolveConfigs(&target->public_configs(), err) ||
         !ResolvePool(target, err) || !ResolveToolchain(target, err))
       return false;
+    if (target->output_type() == Target::GENERATED_FILE &&
+        !ResolveValidationDeps(&target->walk_deps(), err))
+      return false;
 
     DEBUG_BUILDER_RECORD_LOG("BEGIN_RESOLVE %s\n",
                              record->ToDebugString().c_str());
diff --git a/src/gn/builder_record.h b/src/gn/builder_record.h
index 26bdd9c..2dc4c58 100644
--- a/src/gn/builder_record.h
+++ b/src/gn/builder_record.h
@@ -135,6 +135,10 @@
 // To avoid this, these generated_file() targets MUST be written *after* all
 // items in the graph have been resolved.
 //
+// The `walk_deps` of generated_file() targets are tracked exactly like
+// validations, since they are not real dependencies and can form cycles
+// with them. Targets that use them are delayed for the same reason.
+//
 class BuilderRecord {
  public:
   using BuilderRecordSet = PointerSet<BuilderRecord>;
diff --git a/src/gn/command_desc.cc b/src/gn/command_desc.cc
index d1e004a..af2a60b 100644
--- a/src/gn/command_desc.cc
+++ b/src/gn/command_desc.cc
@@ -306,6 +306,7 @@
           {variables::kRebase, DefaultHandler},
           {variables::kWalkKeys, DefaultHandler},
           {variables::kCollectValidationsMetadata, DefaultHandler},
+          {variables::kWalkDeps, DefaultHandler},
           {variables::kWeakFrameworks, DefaultHandler},
           {variables::kWeakLibraries, DefaultHandler},
           {variables::kWriteOutputConversion, DefaultHandler},
@@ -411,6 +412,7 @@
   HandleProperty(variables::kCollectValidationsMetadata, handler_map, v, dict);
   HandleProperty(variables::kRustflags, handler_map, v, dict);
   HandleProperty(variables::kWalkKeys, handler_map, v, dict);
+  HandleProperty(variables::kWalkDeps, handler_map, v, dict);
   HandleProperty(variables::kWeakFrameworks, handler_map, v, dict);
   HandleProperty(variables::kWeakLibraries, handler_map, v, dict);
   HandleProperty(variables::kWriteOutputConversion, handler_map, v, dict);
@@ -541,6 +543,7 @@
   testonly
   validations
   visibility
+  walk_deps
   walk_keys
   weak_frameworks
   weak_libraries
diff --git a/src/gn/command_gen.cc b/src/gn/command_gen.cc
index 33462ed..0b95483 100644
--- a/src/gn/command_gen.cc
+++ b/src/gn/command_gen.cc
@@ -143,7 +143,8 @@
     // Do not write these targets yet because some of the targets visited by
     // their metadata walks might not be resolved, which would cause runtime
     // crashes. See the comment in builder_record.h.
-    if (target->collect_validations_metadata()) {
+    if (target->collect_validations_metadata() ||
+        !target->walk_deps().empty()) {
       write_info->delayed_generated_file_targets.push_back(target);
       return;
     }
diff --git a/src/gn/command_refs.cc b/src/gn/command_refs.cc
index 0645b82..fe70d72 100644
--- a/src/gn/command_refs.cc
+++ b/src/gn/command_refs.cc
@@ -40,6 +40,8 @@
       dep_map->insert(std::make_pair(dep_pair.ptr, target));
     for (const auto& validation_pair : target->validations())
       dep_map->insert(std::make_pair(validation_pair.ptr, target));
+    for (const auto& walk_dep_pair : target->walk_deps())
+      dep_map->insert(std::make_pair(walk_dep_pair.ptr, target));
   }
 }
 
@@ -55,7 +57,7 @@
 // recursed into. When |all| is true, targets are removed from the set once
 // their refs are printed, so that all refs are printed, except for those that
 // are being recursed into (which happens when there are reference cycles
-// through validations).
+// through validations or walk_deps).
 //
 // Returns the number of items printed.
 size_t RecursivePrintTarget(const DepMap& dep_map,
diff --git a/src/gn/desc_builder.cc b/src/gn/desc_builder.cc
index 1627ef6..3a3b864 100644
--- a/src/gn/desc_builder.cc
+++ b/src/gn/desc_builder.cc
@@ -61,6 +61,7 @@
 //   "metadata" : [ dictionary of target metadata values ]
 //   "data_keys" : [ list of target data keys ]
 //   "walk_keys" : [ list of target walk keys ]
+//   "walk_deps" : [ list of metadata walk roots ]
 //   "crate_root" : "root file of a Rust target"
 //   "crate_name" : "name of a Rust target"
 //   "rebase" : true or false
@@ -589,6 +590,10 @@
         res->SetKey(variables::kCollectValidationsMetadata,
                     base::Value(target_->collect_validations_metadata()));
       }
+      if (what(variables::kWalkDeps) && !target_->walk_deps().empty()) {
+        res->SetWithoutPathExpansion(variables::kWalkDeps,
+                                     RenderLabels(target_->walk_deps()));
+      }
     }
 
     if (what(variables::kDeps))
diff --git a/src/gn/functions_target.cc b/src/gn/functions_target.cc
index 9700c65..e05a36f 100644
--- a/src/gn/functions_target.cc
+++ b/src/gn/functions_target.cc
@@ -989,12 +989,16 @@
   By default, validations dependencies are never visited by metadata collection,
   but setting `collect_validations_metadata = true` changes this behavior.
 
+  The metadata collection walk can also start from targets that are not
+  dependencies of the generated_file() target by listing them in `walk_deps`.
+  See `gn help walk_deps`.
+
 Variables
 
 )" DEPENDENT_CONFIG_VARS DEPS_VARS GENERAL_TARGET_VARS
 
     R"(  Generated file: contents, data_keys, rebase, walk_keys, output_conversion,
-                  outputs, collect_validations_metadata
+                  outputs, collect_validations_metadata, walk_deps
 
 Example (metadata collection)
 
diff --git a/src/gn/functions_target_unittest.cc b/src/gn/functions_target_unittest.cc
index 3d66b68..760006e 100644
--- a/src/gn/functions_target_unittest.cc
+++ b/src/gn/functions_target_unittest.cc
@@ -226,3 +226,42 @@
   ASSERT_EQ(1u, target->public_inputs().size());
   EXPECT_EQ("//bar.in", target->public_inputs()[0].value());
 }
+
+// Checks parsing of walk_deps on generated_file().
+TEST_F(FunctionsTarget, GeneratedFileWalkDeps) {
+  TestWithScope setup;
+  Scope::ItemVector item_collector;
+  setup.scope()->set_item_collector(&item_collector);
+
+  TestParseInput input(
+      R"(generated_file("foo") {
+        outputs = [ "$root_out_dir/foo.json" ]
+        data_keys = [ "files" ]
+        walk_deps = [ "//foo:bar" ]
+      })");
+  ASSERT_FALSE(input.has_error());
+  Err err;
+  input.parsed()->Execute(setup.scope(), &err);
+  ASSERT_SUCCESS(err);
+
+  ASSERT_EQ(1u, item_collector.size());
+  const Target* target = item_collector[0]->AsTarget();
+  ASSERT_TRUE(target);
+  ASSERT_EQ(1u, target->walk_deps().size());
+  EXPECT_EQ("//foo:bar",
+            target->walk_deps()[0].label.GetUserVisibleName(false));
+
+  // walk_deps may not be used with contents.
+  TestParseInput bad_input(
+      R"(generated_file("bar") {
+        outputs = [ "$root_out_dir/bar.json" ]
+        contents = "bar"
+        walk_deps = [ ":baz" ]
+      })");
+  ASSERT_FALSE(bad_input.has_error());
+  err = Err();
+  bad_input.parsed()->Execute(setup.scope(), &err);
+  EXPECT_TRUE(err.has_error());
+  EXPECT_NE(std::string::npos, err.message().find("won't be used."))
+      << err.message();
+}
diff --git a/src/gn/generated_file_target_generator.cc b/src/gn/generated_file_target_generator.cc
index 16b705b..dea6a10 100644
--- a/src/gn/generated_file_target_generator.cc
+++ b/src/gn/generated_file_target_generator.cc
@@ -57,6 +57,8 @@
     return;
   if (!FillWalkKeys())
     return;
+  if (!FillWalkDeps())
+    return;
 
   if (!FillOutputConversion())
     return;
@@ -163,6 +165,15 @@
   return FillKeys(variables::kWalkKeys, &target_->walk_keys());
 }
 
+bool GeneratedFileTargetGenerator::FillWalkDeps() {
+  const Value* value = scope_->GetValue(variables::kWalkDeps, false);
+  if (!value)
+    return true;
+  if (!IsMetadataCollectionTarget(variables::kWalkDeps, value->origin()))
+    return false;
+  return FillGenericDeps(variables::kWalkDeps, &target_->walk_deps());
+}
+
 bool GeneratedFileTargetGenerator::FillKeys(std::string_view variable,
                                             std::vector<std::string>* out) {
   const Value* value = scope_->GetValue(variable, true);
diff --git a/src/gn/generated_file_target_generator.h b/src/gn/generated_file_target_generator.h
index 59a7539..443de8e 100644
--- a/src/gn/generated_file_target_generator.h
+++ b/src/gn/generated_file_target_generator.h
@@ -30,6 +30,7 @@
   bool FillCollectValidationsMetadata();
   bool FillDataKeys();
   bool FillWalkKeys();
+  bool FillWalkDeps();
   bool FillRebase();
 
   // Appends the list of strings in the given variable (if set) to |out|.
diff --git a/src/gn/metadata.cc b/src/gn/metadata.cc
index 8ffdbb1..a981a61 100644
--- a/src/gn/metadata.cc
+++ b/src/gn/metadata.cc
@@ -29,7 +29,8 @@
   A generated_file target can declare either 'contents' to write statically
   known contents to a file or 'data_keys' to aggregate metadata and write the
   result to a file. It can also specify 'walk_keys' (to restrict the metadata
-  collection), 'output_conversion', and 'rebase'.
+  collection), 'walk_deps' (to collect metadata from targets that are not
+  dependencies), 'output_conversion', and 'rebase'.
 
 
 Collection and Aggregation
diff --git a/src/gn/setup_unittest.cc b/src/gn/setup_unittest.cc
index d3140bd..ac7db08 100644
--- a/src/gn/setup_unittest.cc
+++ b/src/gn/setup_unittest.cc
@@ -899,3 +899,86 @@
     EXPECT_EQ(output_items[2].decoration, DECORATION_NONE);
   }
 }
+
+namespace {
+
+int RunGenForWalkDepsTest(const base::FilePath& in_path,
+                          const base::FilePath& build_path,
+                          const std::string& walk_deps) {
+  WriteFile(in_path.Append(FILE_PATH_LITERAL(".gn")),
+            "buildconfig = \"//BUILDCONFIG.gn\"\n");
+  WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILDCONFIG.gn")),
+            "set_default_toolchain(\"//:toolchain\")\n");
+  WriteFile(in_path.Append(FILE_PATH_LITERAL("script.py")), "");
+  WriteFile(in_path.Append(FILE_PATH_LITERAL("BUILD.gn")), R"(
+toolchain("toolchain") {
+  tool("stamp") {
+    command = "touch {{output}}"
+  }
+}
+action("pickle") {
+  script = "//script.py"
+  outputs = [ "$target_gen_dir/foo.pickle" ]
+  metadata = {
+    pickle_files = [ "$target_gen_dir/foo.pickle" ]
+  }
+}
+group("lib") {
+  deps = [ ":pickle" ]
+}
+generated_file("pickles") {
+  outputs = [ "$target_gen_dir/pickles.txt" ]
+  data_keys = [ "pickle_files" ]
+  rebase = root_build_dir
+  walk_deps = )" + walk_deps + R"(
+}
+action("final") {
+  script = "//script.py"
+  outputs = [ "$target_gen_dir/final.txt" ]
+  inputs = [ "$target_gen_dir/pickles.txt" ]
+  deps = [ ":pickles" ]
+}
+group("app") {
+  deps = [ ":final", ":lib" ]
+}
+)");
+
+  ScopedCommandLineForTest global_cmdline;
+  global_cmdline.get().AppendSwitchPath(switches::kRoot, in_path);
+  global_cmdline.get().AppendSwitch(switches::kQuiet);
+  MsgLoop msg_loop;
+  return commands::RunGen({FilePathToUTF8(build_path)});
+}
+
+}  // namespace
+
+// Tests that walk_deps can form a cycle with deps without becoming
+// dependencies of the generated_file(), and that undefined walk_deps targets
+// are rejected.
+TEST(GenCommandTest, WalkDeps) {
+  base::ScopedTempDir in_temp_dir;
+  ASSERT_TRUE(in_temp_dir.CreateUniqueTempDir());
+  base::FilePath in_path = base::MakeAbsoluteFilePath(in_temp_dir.GetPath());
+  base::ScopedTempDir build_temp_dir;
+  ASSERT_TRUE(build_temp_dir.CreateUniqueTempDir());
+  const base::FilePath& build_path = build_temp_dir.GetPath();
+
+  EXPECT_EQ(0, RunGenForWalkDepsTest(in_path, build_path, "[ \":app\" ]"));
+  EXPECT_EQ("gen/foo.pickle\n",
+            ReadFile(build_path.Append(FILE_PATH_LITERAL("gen/pickles.txt"))));
+  std::string ninja =
+      ReadFile(build_path.Append(FILE_PATH_LITERAL("toolchain.ninja")));
+  EXPECT_NE(std::string::npos,
+            ninja.find("build phony/pickles: phony gen/pickles.txt\n"))
+      << ninja;
+
+  // Undefined target in walk_deps fails resolution.
+  ScopedBufferedOutput buffered_out;
+  EXPECT_NE(0, RunGenForWalkDepsTest(in_path, build_path, "[ \":missing\" ]"));
+
+  std::string output;
+  for (const auto& item : buffered_out.GetItems())
+    output += item.output;
+  EXPECT_NE(std::string::npos, output.find("needs //:missing(//:toolchain)"))
+      << output;
+}
diff --git a/src/gn/target.cc b/src/gn/target.cc
index 39856c3..2bf4f27 100644
--- a/src/gn/target.cc
+++ b/src/gn/target.cc
@@ -1191,6 +1191,10 @@
     if (!Visibility::CheckItemVisibility(this, pair.ptr, err))
       return false;
   }
+  for (const auto& pair : walk_deps()) {
+    if (!Visibility::CheckItemVisibility(this, pair.ptr, err))
+      return false;
+  }
   return true;
 }
 
@@ -1227,6 +1231,14 @@
       return false;
     }
   }
+  // Like validations, walk_deps might not be resolved yet, but testonly is set
+  // when targets are defined.
+  for (const auto& pair : walk_deps()) {
+    if (pair.ptr->testonly()) {
+      *err = MakeTestOnlyError(this, pair.ptr);
+      return false;
+    }
+  }
 
   // Verify no configs have "testonly" set.
   for (ConfigValuesIterator iter(this); !iter.done(); iter.Next()) {
@@ -1326,6 +1338,9 @@
     // If deps_only, this is the top-level target and thus we don't want to
     // collect its metadata, only that of its deps and data_deps.
     if (deps_only) {
+      // Do not collect its metadata either if the walk leads back to it
+      // (which is possible with walk_deps or validations).
+      targets_walked_.add(&target);
       // Empty string will be converted below to mean all deps and data_deps.
       // Origin is null because this isn't declared anywhere, and should never
       // trigger any errors.
@@ -1368,6 +1383,12 @@
             }
           }
         }
+        if (deps_only) {
+          for (const auto& dep : target.walk_deps()) {
+            if (targets_walked_.add(dep.ptr) && !Walk(*dep.ptr, false))
+              return false;
+          }
+        }
 
         // Any other walk keys are superfluous, as they can only be a subset of
         // all deps.
diff --git a/src/gn/target.h b/src/gn/target.h
index 4403b76..3c5ae32 100644
--- a/src/gn/target.h
+++ b/src/gn/target.h
@@ -230,6 +230,7 @@
     SourceDir rebase_;
     std::vector<std::string> data_keys_;
     std::vector<std::string> walk_keys_;
+    LabelTargetVector walk_deps_;
   };
   const GeneratedFile& generated_file() const;
   GeneratedFile& generated_file();
@@ -254,6 +255,10 @@
     return generated_file().walk_keys_;
   }
   std::vector<std::string>& walk_keys() { return generated_file().walk_keys_; }
+  const LabelTargetVector& walk_deps() const {
+    return generated_file().walk_deps_;
+  }
+  LabelTargetVector& walk_deps() { return generated_file().walk_deps_; }
 
   OutputFile write_runtime_deps_output() const {
     return write_runtime_deps_output_;
diff --git a/src/gn/target_generator.h b/src/gn/target_generator.h
index bef5ddf..61a23b4 100644
--- a/src/gn/target_generator.h
+++ b/src/gn/target_generator.h
@@ -60,6 +60,9 @@
   bool EnsureSubstitutionIsInOutputDir(const SubstitutionPattern& pattern,
                                        const Value& original_value);
 
+  // Reads deps from the given var name, and saves them to |dest|.
+  bool FillGenericDeps(const char* var_name, LabelTargetVector* dest);
+
   Target* target_;
   Scope* scope_;
   const FunctionCallNode* function_call_;
@@ -75,11 +78,10 @@
   bool FillCheckIncludesStrict();
   bool FillWriteRuntimeDeps();
 
-  // Reads configs/deps from the given var name, and uses the given setting on
+  // Reads configs from the given var name, and uses the given setting on
   // the target to save them.
   bool FillGenericConfigs(const char* var_name,
                           UniqueVector<LabelConfigPair>* dest);
-  bool FillGenericDeps(const char* var_name, LabelTargetVector* dest);
 
   TargetGenerator(const TargetGenerator&) = delete;
   TargetGenerator& operator=(const TargetGenerator&) = delete;
diff --git a/src/gn/target_unittest.cc b/src/gn/target_unittest.cc
index e8b751f..6521776 100644
--- a/src/gn/target_unittest.cc
+++ b/src/gn/target_unittest.cc
@@ -1523,6 +1523,67 @@
       << err.message();
 }
 
+// Tests that walk_deps are used as roots of the metadata walk, without
+// collecting the top-level target's own metadata if the walk cycles back to it.
+TEST(TargetTest, CollectMetadataWithWalkDeps) {
+  TestWithScope setup;
+
+  TestTarget pickle(setup, "//foo:pickle", Target::ACTION);
+  Value pickle_files(nullptr, Value::LIST);
+  pickle_files.list_value().push_back(Value(nullptr, "foo.pickle"));
+  pickle.metadata().contents().emplace("files", pickle_files);
+
+  TestTarget lib(setup, "//foo:lib", Target::SOURCE_SET);
+  lib.private_deps().push_back(LabelTargetPair(&pickle));
+
+  TestTarget gen(setup, "//foo:gen", Target::GENERATED_FILE);
+  gen.metadata().contents().emplace("files", pickle_files);
+
+  TestTarget app(setup, "//foo:app", Target::GROUP);
+  app.private_deps().push_back(LabelTargetPair(&lib));
+  app.private_deps().push_back(LabelTargetPair(&gen));
+  gen.walk_deps().push_back(LabelTargetPair(&app));
+
+  Err err;
+  std::vector<Value> result;
+  TargetSet targets;
+  gen.GetMetadata({"files"}, {""}, SourceDir(), true, &result, &targets, &err);
+  EXPECT_SUCCESS(err);
+  EXPECT_EQ(result, std::vector<Value>{Value(nullptr, "foo.pickle")});
+}
+
+// walk_deps are subject to the same visibility and testonly checks as deps.
+TEST_F(TargetTest, WalkDepsVisibilityAndTestonly) {
+  TestWithScope setup;
+  Err err;
+
+  TestTarget hidden(setup, "//private:hidden", Target::GROUP);
+  hidden.visibility().SetPrivate(hidden.label().dir());
+  ASSERT_TRUE(hidden.OnResolved(&err));
+
+  TestTarget testonly(setup, "//test:testonly", Target::GROUP);
+  testonly.set_testonly(true);
+  ASSERT_TRUE(testonly.OnResolved(&err));
+
+  for (const Target* walk_dep : {&hidden, &testonly}) {
+    TestTarget gen(setup, "//app:gen", Target::GENERATED_FILE);
+    gen.action_values().outputs() =
+        SubstitutionList::MakeForTest("//out/Debug/gen.json");
+    gen.walk_deps().push_back(LabelTargetPair(walk_dep));
+    err = Err();
+    EXPECT_FALSE(gen.OnResolved(&err));
+    std::string expected_message = walk_dep == &hidden
+                                       ? "Dependency not allowed."
+                                       : "Test-only dependency not allowed.";
+    EXPECT_EQ(expected_message, err.message());
+
+    // Marking the generated_file() as testonly makes the testonly one pass.
+    gen.set_testonly(true);
+    err = Err();
+    EXPECT_EQ(walk_dep == &testonly, gen.RunChecksAfterResolution(&err));
+  }
+}
+
 TEST_F(TargetTest, WriteMetadataCollection) {
   TestWithScope setup;
   Err err;
diff --git a/src/gn/variables.cc b/src/gn/variables.cc
index adc7657..e4c9a5e 100644
--- a/src/gn/variables.cc
+++ b/src/gn/variables.cc
@@ -2451,6 +2451,46 @@
     visibility = [ "./*", "//bar/*" ]
 )";
 
+const char kWalkDeps[] = "walk_deps";
+const char kWalkDeps_HelpShort[] =
+    "walk_deps: [label list] Metadata walk roots that are not dependencies.";
+const char kWalkDeps_Help[] =
+    R"(walk_deps: Metadata walk roots that are not dependencies.
+
+  Valid on generated_file() targets that collect metadata.
+
+  The metadata walk starts from these targets in addition to deps and
+  data_deps, without making them Ninja dependencies of the generated_file()
+  target (allowing cycles with deps). Like "gen_deps", listed targets are
+  generated when the generated_file() target is generated. Like "deps", they
+  are subject to "visibility" and "testonly" checks.
+
+  They are used only when writing the generated_file() target itself, and not
+  when the metadata walk of another target passes through it. The metadata of
+  the generated_file() target itself is never collected, even when the walk
+  leads back to it.
+
+  See "gn help generated_file".
+
+Example
+
+  # Lists the libraries of an app without depending on them, even though the
+  # app depends on the list.
+  generated_file("app_libs_list") {
+    outputs = [ "$target_gen_dir/app_libs.txt" ]
+    data_keys = [ "lib_names" ]
+    walk_deps = [ ":app" ]
+  }
+
+  action("app") {
+    ...
+    deps = [
+      ":app_libs_list",
+      ":some_lib",
+    ]
+  }
+)";
+
 const char kWalkKeys[] = "walk_keys";
 const char kWalkKeys_HelpShort[] =
     "walk_keys: [string list] Key(s) for managing the metadata collection "
@@ -2680,6 +2720,7 @@
     INSERT_VARIABLE(Testonly)
     INSERT_VARIABLE(Validations)
     INSERT_VARIABLE(Visibility)
+    INSERT_VARIABLE(WalkDeps)
     INSERT_VARIABLE(WalkKeys)
     INSERT_VARIABLE(WeakFrameworks)
     INSERT_VARIABLE(WeakLibraries)
diff --git a/src/gn/variables.h b/src/gn/variables.h
index 97c21b3..b2be92c 100644
--- a/src/gn/variables.h
+++ b/src/gn/variables.h
@@ -366,6 +366,10 @@
 extern const char kVisibility_HelpShort[];
 extern const char kVisibility_Help[];
 
+extern const char kWalkDeps[];
+extern const char kWalkDeps_HelpShort[];
+extern const char kWalkDeps_Help[];
+
 extern const char kWalkKeys[];
 extern const char kWalkKeys_HelpShort[];
 extern const char kWalkKeys_Help[];