Skip to content

write_file should auto-create missing parent directories (or add a recursive option) #525

Description

@hl9020

Is your feature request related to a problem?

When calling write_file with a target path whose parent directory does not exist yet, the call fails. This forces a mandatory two-step pattern for any new nested file: first create_directory, then write_file. In agent workflows this is an extra round-trip on something that is almost always the intended behaviour, and a frequent source of avoidable failed calls.

Describe the solution you'd like

write_file should create missing parent directories automatically before writing, equivalent to fs.mkdir(dirname(path), { recursive: true }) prior to the write. This matches the mental model of mkdir -p + write and how most editors/tools behave on save.

If implicit creation is considered too surprising, an explicit opt-in would also solve it, e.g. a createDirs?: boolean parameter (default could stay false to preserve current behaviour).

The same allowedDirectories validation must of course still apply to the resolved parent path - directories outside the allowed roots should not be created, and the check should run against the canonical (symlink-resolved) parent path to stay consistent with the existing path-safety handling.

Describe alternatives you've considered

Calling create_directory first. It works but is redundant for the common case and adds a round-trip per new file.

Additional context

create_directory already supports creating nested directories in one operation, so the recursive-mkdir capability exists internally; this request is just to reuse it from the write_file path.

I'm happy to send a PR for this - would you prefer implicit auto-create, or behind an opt-in flag? Let me know which direction you'd accept and I'll implement accordingly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions