> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://rivet.chrisboulton.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://rivet.chrisboulton.com/_mcp/server.

# Filesystem functions

> Signatures, behavior, examples, and Rivet compatibility boundaries.

File reads resolve relative to the directory of the source declaring the expression, including a child module. They never use the controller working directory or home. `path.module` identifies that root.

| Function                     | Result and rules                                                                                       |
| ---------------------------- | ------------------------------------------------------------------------------------------------------ |
| `abspath(path)`              | Normalized absolute path inside the root; safely missing descendants are allowed.                      |
| `basename(path)`             | Lexical last path component; does not read the filesystem.                                             |
| `dirname(path)`              | Lexical parent path; does not read the filesystem.                                                     |
| `file(path)`                 | Read a confined regular UTF-8 file.                                                                    |
| `filebase64(path)`           | Read a confined regular file as bytes, then standard base64.                                           |
| `fileexists(path)`           | True for a regular file, false only for safely missing files; directories and inspection errors fail.  |
| `fileset(root, pattern)`     | Sorted set of relative regular filenames matching \*, \*\*, character classes, and brace alternatives. |
| `templatestring(text, vars)` | Render evaluated text in isolated explicit vars scope; see templates.                                  |
| `templatefile(path, vars)`   | Read and render a confined UTF-8 HCL template with explicit variables; see templates.                  |

Read paths may be relative or absolute inside the declaring root. Parent traversal, URI paths, outside absolute paths, escaping or dangling symlinks, and nonregular reads fail. In-memory sources must supply an explicit root, and all source names in a compilation must be unique so declaring roots cannot be confused. `fileset` requires an existing base directory, refuses directory symlinks and symlink entries, and inspects at most 100,000 entries.

Local reads may precede global catalog validation because they supply desired values. Cloud resource acquisition waits for global validation. Rendered/read bytes used as file content are bundled; applying needs no source files. Lexical `basename`/`dirname` do not grant read access. File hashes are listed under [hash and crypto](/language/functions/hash). `templatefile` and `templatestring` are detailed under [templates](/language/templates).

## Example

```hcl
locals {
  files = {
    absolute = abspath("assets/config.json")
    base     = basename("assets/config.json")
    parent   = dirname("assets/config.json")
    text     = file("assets/config.json")
    bytes    = filebase64("assets/config.json")
    present  = fileexists("assets/config.json")
    matches  = fileset("assets", "*.json")
  }
}
output "files" {
  type  = string
  value = jsonencode(local.files)
}
```

All calls obey the [function limits and annotation rules](/language/functions).