bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
23.22k stars 4.07k forks source link

attr.output cannot create directories #14647

Open pauldraper opened 2 years ago

pauldraper commented 2 years ago

I would like to have a named output directory from a rule.

attr.output can create output attributes, but the outputs must always be files; they cannot be directories. That seems an artificial/unnecessary limitation.

I would like to support directories. Perhaps this could be an argument to attr.output. An argument type could be "file", "directory", or "symlink".

chancila commented 2 years ago

as a workaround I have done

intermediate_dir = ctx.actions.create_dir()
# emit stuff in the dir via actions
ctx.actions.symlink(output, intermediate_dir)
pauldraper commented 2 years ago

Clever.

joeljeske commented 2 years ago

I had trouble bypassing this limitation using ctx.actions.symlink. I ended up having a rule that created many directories using ctx.actions.declare_directory and then returning them using OutputGroupInfo and naming them dynamically. Then you can access each dynamic directory using filegroup[output_group].

Its frustrating to require a workaround that should be supported properly, but this does appear to work fine.

tjgq commented 2 years ago

Unfortunately, the ctx.actions.symlink workaround creates another problem: it causes Bazel to create a symlink from a file artifact to a directory artifact. This ought to be considered a bug in ctx.actions.symlink (it causes other parts of Bazel to become confused about whether the artifact the symlink points to is a directory or not) and I'm in the process of disallowing it.

comius commented 1 year ago

Keeping P4, as far as I understand treeartifacts are already hard to correctly support within the rules. As such I think it’s better not to have the same problem on the targets / external rules interface.

github-actions[bot] commented 2 weeks ago

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.