Ensure DirectoryContext.createOrSetWritable() always creates directories #19381
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In case DirectoryContext.createOrSetWritable() is called on a directory inside of a Tree, it already does a nice forward sweep along the path to create missing intermediate directories. When creating a bare output file in a nested directory, it does not. It implicitly assumes that the output directory was already created at a previous point in time.
This doesn't cause issues whenever we do a fully clean build, as some earlier phase (the analysis phase?) creates the missing parent directories for us. However, if we remove the nested directory structure from bazel-bin/ manually and rerun the test, this causes failures along the lines:
Solve this issue by properly catching FileNotFoundExceptions that are generated when we call Path.isWritable() on the parent directory of the output file. In that case we resort to calling
Path.createDirectoryAndParents().
I think this is likely the most efficient strategy, as it means we don't need to do a full forward scan of the path in the common case where the parent directory does exist.
This issue can be noticed in practice when switching from --remote_output_service (PR #12823) to --remote_download_minimal.