New issue
Advanced search Search tips

Issue 871644 link

Starred by 1 user

Issue metadata

Status: WontFix
Owner: ----
Closed: Aug 7
Components:
EstimatedDays: ----
NextAction: ----
OS: ----
Pri: 3
Type: Bug



Sign in to add a comment

GN: `data = outputs` doesn't work inside action_foreach(..)

Project Member Reported by tapted@chromium.org, Aug 7

Issue description

Instead, it adds a dependency on, e.g., `gen/foo/{{source_name_part}}.html`

Maybe I'm just holding it wrong..

Context: I want to generate a file for each dependency, and automatically have the generated file also a data dependency so that it gets sent for test isolation. There doesn't seem to be a way to do this with a single target.

E.g. I want:

template("js_unit_tests") {
  action_foreach(target_name) {
    script = "/dev/null"
    forward_variables_from(invoker,
                           [
                             "deps",
                           ])
    sources = []
    foreach(dep, deps) {
      sources += get_target_outputs(dep)
    }

    outputs = [
      "$target_gen_dir/{{source_name_part}}.html",
    ]
    args = [ "--output" ] + rebase_path(outputs, root_build_dir)
    args += [ "--input" ] + [ "{{source}}" ]

    # Add generated html as a data dependency. Doesn't work!
    data = outputs
  }
}

# Dummy targets.
action("bar.js") {
  script = "/dev/null"
  args = [ target_name ]
  outputs = [ "$target_gen_dir/$target_name" ]
}
action("baz.js") {
  script = "/dev/null"
  args = [ target_name ]
  outputs = [ "$target_gen_dir/$target_name" ]
}

js_unit_tests("foo_tests") {
  deps = [ ":bar.js", ":baz.js"]
}


To automatically add the generated files "bar.html" and "baz.html" as data dependencies. Instead, I get

$ ninja -v ui/file_manager:foo_tests
[1/8] python /dev/null bar.js
[2/7] python /dev/null baz.js
[3/5] python /dev/null --output gen/ui/file_manager/bar.html --input gen/ui/file_manager/bar.js
[4/5] python /dev/null --output gen/ui/file_manager/baz.html --input gen/ui/file_manager/baz.js

$ gn desc . //ui/file_manager:foo_tests runtime_deps
gen/ui/file_manager/{{source_name_part}}.html


Current workaround is to add an additional target:


group("foo_test_data") {
  data = get_target_outputs(":foo_tests")
}


Which gives the desired result:

$ gn desc . //ui/file_manager:foo_test_data runtime_deps
gen/ui/file_manager/bar.html
gen/ui/file_manager/baz.html



 
A-ha! - better workaround:


template("js_unit_tests") {
  html_gen_target_name = target_name + "_html_gen"
  action_foreach(html_gen_target_name) {
    script = "/dev/null"
    forward_variables_from(invoker,
                           [
                             "deps",
                           ])
    sources = []
    foreach(dep, deps) {
      sources += get_target_outputs(dep)
    }

    outputs = [
      "$target_gen_dir/{{source_name_part}}.html",
    ]
    args = [ "--output" ] + rebase_path(outputs, root_build_dir)
    args += [ "--input" ] + [ "{{source}}" ]
  }
  group(target_name) {
    data = get_target_outputs(":$html_gen_target_name")
  }
}


(I thought I had tried that, but kept bumping into "target not defined" issues until I followed the pattern in the gn reference)
I may have been completely overthinking things...

adding generated files as a `data` dependency breaks the isolate scripts, since they expect all the data dependencies to exist before anything is built.

Is everything in out/gen sent to isolate? that seems weird. Let's try it
ahh, important omission:

  group(target_name) {
    data = get_target_outputs(":$html_gen_target_name")
    deps = [
      ":$html_gen_target_name",
    ]
  }
Are you good now? I kinda got lost in these updates. If you're not good, what are you looking to do that you haven't got working?
Status: WontFix (was: Untriaged)
ahh, yep - I think I'm good now :). Thanks for follow-ing up.

Or at least, the final solution seems to work - https://chromium-review.googlesource.com/c/chromium/src/+/1161720/13/ui/file_manager/js_unit_tests.gni

instead of `data = outputs` in an action_foreach(..) target, the workaround using an extra target, get_target_outputs and some indirection gets around it.

I think this is not worth fixing (maybe it can't be). So, WontFix. Maybe if someone else encounters the same problem, a search will encounter this bug with the workaround.

It might be hard to capture in the GN documentation. E.g. `data` is still valid inside action_foreach(), it just doesn't work with {{source_name_part}}. And the documentation around templates already leads you into this workaround with the example it gives.

Sign in to add a comment