Skip to main content
This object is created during the analysis phase to represent a file or directory that will be read or written during the execution phase. It is not an open file handle, and cannot be used to directly read or write file contents. Rather, you use it to construct the action graph in a rule implementation function by passing it to action-creating functions. See the Rules page for more information. When a File is passed to an Args object without using a map_each function, it is converted to a string by taking the value of its path field.

Members

basename

The base name of this file. This is the name of the file inside the directory.

dirname

The name of the directory containing this file. It’s taken from path and is always relative to the execution directory.

extension

The file extension of this file, following (not including) the rightmost period. Empty string if the file’s basename includes no periods.

is_directory

Returns true if this is a directory. This reflects the type the file was declared as (i.e. ctx.actions.declare_directory), not its type on the filesystem, which might differ.

is_source

Returns true if this is a source file, i.e. it is not generated.
Returns true if this was declared as a symlink. This reflects the type the file was declared as (i.e. ctx.actions.declare_symlink), not its type on the filesystem, which might differ.

owner

A label of a target that produces this File. May return None.

path

The execution path of this file, relative to the workspace’s execution directory. For files in the main repository, it consists of two parts: an optional first part called the root (see also the root module) and the second part which is the short_path. The root may be empty, which it usually is for non-generated files. For generated files it usually contains a configuration-specific path fragment that encodes things like the target CPU architecture that was used while building said file. Note that for files in external repositories, short_path reflects the runfiles tree path (prefixed with ../<repo_name>/) rather than the root-relative execution path (prefixed with external/<repo_name>/), so concatenating File.root.path and File.short_path does not yield File.path.

root

The root beneath which this file resides.

short_path

The path of this file relative to its root, excluding configuration-specific fragments of the path (the aforementioned root). For files in the main repository, this is the path of the file relative to the repository. For files in external repositories, this path begins with ../<repo_name>/ (the legacy runfiles path relative to the main repository’s runfiles directory). Note that because it starts with ../, it cannot simply be joined to RUNFILES_DIR, nor used with runfiles libraries or rlocation without stripping the leading ../.

tree_relative_path

The path of this file relative to the root of the ancestor’s tree, if the ancestor’s is_directory field is true. tree_relative_path is only available for expanded files of a directory in an action command, i.e. Args.add_all(). For other types of files, it is an error to access this field.