[Feature request] Add ability to create directories #1094

Closed
opened 2026-02-16 17:29:26 -05:00 by yindo · 6 comments
Owner

Originally created by @nov1n on GitHub (Aug 4, 2025).

Originally assigned to: @thdxr on GitHub.

I'd like the agent to have the ability to create directories.

Currently, it seems mkdir fails for non-existent directories. I tracked it down to the security check in bash.ts around lines 70-85:

if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown"].includes(command[0])) {
  for (const arg of command.slice(1)) {
    if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue
    const resolved = await $`realpath ${arg}`
      .quiet()
      .nothrow()
      .text()
      .then((x) => x.trim())
    log.info("resolved path", { arg, resolved })
    if (resolved && !Filesystem.contains(app.path.cwd, resolved)) {
      throw new Error(
        `This command references paths outside of ${app.path.cwd} so it is not allowed to be executed.`,
      )
    }
  }
}

I think the problem is that realpath is called on the directory argument before mkdir creates it. Since realpath requires the path to exist, it fails for non-existent directories that mkdir is trying to create. This creates a catch-22 where the security check prevents mkdir from working on its intended use case.

A workaround I'm currently using to give the agent the ability to create directories is to add the following snippet to ~/.config/opencode/AGENTS.md:

# Global Agent Instructions

## Directory Creation

- Always use `install -d <dirname>` instead of `mkdir <dirname>`.
- The mkdir command fails for non-existent directories even within the current working directory.

This is not ideal though, as it can create directories outside the tool's cwd since there's no check.

Originally created by @nov1n on GitHub (Aug 4, 2025). Originally assigned to: @thdxr on GitHub. I'd like the agent to have the ability to create directories. Currently, it seems `mkdir` fails for non-existent directories. I tracked it down to the security check in [bash.ts](https://github.com/rekram1-node/opencode-sst/blob/dev/packages/opencode/src/tool/bash.ts#L77) around lines 70-85: ```bash.ts if (["cd", "rm", "cp", "mv", "mkdir", "touch", "chmod", "chown"].includes(command[0])) { for (const arg of command.slice(1)) { if (arg.startsWith("-") || (command[0] === "chmod" && arg.startsWith("+"))) continue const resolved = await $`realpath ${arg}` .quiet() .nothrow() .text() .then((x) => x.trim()) log.info("resolved path", { arg, resolved }) if (resolved && !Filesystem.contains(app.path.cwd, resolved)) { throw new Error( `This command references paths outside of ${app.path.cwd} so it is not allowed to be executed.`, ) } } } ``` I think the problem is that `realpath` is called on the directory argument before `mkdir` creates it. Since realpath requires the path to exist, it fails for non-existent directories that mkdir is trying to create. This creates a catch-22 where the security check prevents mkdir from working on its intended use case. A workaround I'm currently using to give the agent the ability to create directories is to add the following snippet to `~/.config/opencode/AGENTS.md`: ```markdown # Global Agent Instructions ## Directory Creation - Always use `install -d <dirname>` instead of `mkdir <dirname>`. - The mkdir command fails for non-existent directories even within the current working directory. ``` This is not ideal though, as it can create directories outside the tool's `cwd` since there's no check.
yindo closed this issue 2026-02-16 17:29:26 -05:00
Author
Owner

@thdxr commented on GitHub (Aug 4, 2025):

i don't think realpath requires the directory to exist - i just tested it. can you show me a screenshot of the error you see

@thdxr commented on GitHub (Aug 4, 2025): i don't think realpath requires the directory to exist - i just tested it. can you show me a screenshot of the error you see
Author
Owner

@nov1n commented on GitHub (Aug 4, 2025):

Sure, here's a test with an empty ~/.config/opencode directory.

Image
@nov1n commented on GitHub (Aug 4, 2025): Sure, here's a test with an empty `~/.config/opencode` directory. <img width="1600" height="1048" alt="Image" src="https://github.com/user-attachments/assets/e59d068b-6d69-41a2-bc46-c45ff0afee50" />
Author
Owner

@thdxr commented on GitHub (Aug 4, 2025):

i need the tool details expanded

@thdxr commented on GitHub (Aug 4, 2025): i need the tool details expanded
Author
Owner

@nov1n commented on GitHub (Aug 4, 2025):

I emailed you, since it contains sensitive info.

@nov1n commented on GitHub (Aug 4, 2025): I emailed you, since it contains sensitive info.
Author
Owner

@thdxr commented on GitHub (Aug 4, 2025):

oh i think i fixed this issue on latest - can you upgrade?

@thdxr commented on GitHub (Aug 4, 2025): oh i think i fixed this issue on latest - can you upgrade?
Author
Owner

@nov1n commented on GitHub (Aug 4, 2025):

I can confirm upgrading to 0.3.126 fixed this. Thanks for the quick turnaround!

@nov1n commented on GitHub (Aug 4, 2025): I can confirm upgrading to `0.3.126` fixed this. Thanks for the quick turnaround!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#1094