"jsonnet_to_json" "outs" attribute does not allow writing to multiple directories
The jsonnet command-line tool can emit JSON to multiple files when invoked with the -m flag. It interprets the keys of the top-level map as file paths to which to write. Those paths can include not just a file name, but a directory path as well. Though the jsonnet tool does not create these output directories, if you first ensure that they do exist, it will write the output files to the proper directories. That is, when facing manifested JSON with the following top level structure
{
"dir1/file1.json": {},
"dir2/file2.json": {}
}
the jsonnet -m parent command will write to
- parent/dir1/file1.json
- parent/dir2/file2.json
as intended.
However, the jsonnet_to_json rule precludes this behavior. Instead, it infers the value of -m flag to be the directory path of the first output file. In the example above, jsonnet_to_json sees that the first output path has dir1 as its directory, and decides to pass -m dir1 to jsonnet, causing it to try to write to the following paths:
- dir1/dir1/file1.json
- dir1/dir2/file2.json
Even if we had taken care to create dir1 and dir2 beforehand, jsonnet will still fail to write those files, as neither directory dir1/dir1 nor dir1/dir2 are likely to exist.
Better would be to accept an optional attribute to designate the root output directory, with a default value of '.' (the current directory), and not make any assumptions about the first output path being indicative of the others.
I see that @davidzchen introduced this behavior in commit 03d36d12c43d8d45415d9144881c0b67ae6daca5.
@sparkprime, can you comment as to whether you think this technique of writing files to different directories should be allowed? My reading of the Jsonnet multiple file output handling code is that it doesn't care where the files are situated, so long as the containing directories already exist.
I've just added some code to compute the leading part of the directory name that is shared by all output files. That should be a decent enough heuristic.