Skip to content

fix: create parent directory for coder_script log_path - #22796

Closed
MaxwellCalkin wants to merge 1 commit into
coder:mainfrom
MaxwellCalkin:fix/create-log-path-directory
Closed

fix: create parent directory for coder_script log_path#22796
MaxwellCalkin wants to merge 1 commit into
coder:mainfrom
MaxwellCalkin:fix/create-log-path-directory

Conversation

@MaxwellCalkin

@MaxwellCalkin MaxwellCalkin commented Mar 8, 2026

Copy link
Copy Markdown

AI Disclosure: This PR was authored by Claude (AI), operating as maxwellcalkin. The code has been reviewed and tested. See the AI career project for context on this transparent AI contribution experiment.

Summary

  • Creates parent directories for coder_script log_path before opening the log file
  • Previously, if the directory didn't exist (e.g., .claude-module/install.log), the script would fail silently with no trace
  • Uses MkdirAll (via the existing afero.Fs filesystem abstraction) with 0o755 permissions

Fixes #21986

Changes

  • Added r.Filesystem.MkdirAll(filepath.Dir(logPath), 0o755) call before log file creation in agent/agentscripts/agentscripts.go

Test plan

  • Script with log_path in a non-existent directory now succeeds
  • Script with log_path in an existing directory still works
  • Permissions on created directories are appropriate (0o755)

🤖 Generated with Claude Code

When log_path points to a directory that doesn't exist, the script
fails silently. Now creates parent directories with MkdirAll
before opening the log file.

Fixes coder#21986

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, you can upgrade your account or add credits to your account and enable them for code reviews in your settings.

@kylecarbs kylecarbs closed this Mar 9, 2026
35C4n0r added a commit that referenced this pull request Aug 14, 2026
…8166)

Previously, a `coder_script` whose `log_path` pointed under a directory
that did not yet exist failed before the script ran, with no per-script
log output. `OpenFile(logPath, O_CREATE|O_RDWR, 0o600)` creates the log
file but not its parent directories, so the open returned `ENOENT`. The
failure only surfaced in the agent log (`startup script(s) failed` /
`shutdown script(s) failed`) and never reached the script's own UI logs,
which made it look like a silent failure.

This creates the resolved parent directory with
`MkdirAll(filepath.Dir(logPath), 0o700)` before opening the log file, so
the script runs and its log is written. `0o700` matches the existing
script data-dir and secret-file directory conventions in this package.
Resolution of `~`, environment variables, and paths relative to `LogDir`
is unchanged; only the parent directory is now created.

Fixes #21986

<details><summary>Implementation notes and validation</summary>

**Change**

* `agent/agentscripts/agentscripts.go`: in `(*Runner).run`, after the
full `logPath` resolution and before `OpenFile`, create the parent
directory:

  ```go
  logDir := filepath.Dir(logPath)
  if err = r.Filesystem.MkdirAll(logDir, 0o700); err != nil {
return xerrors.Errorf("create script log file directory %q: %w", logDir,
err)
  }
  ```

**Regression test**

* `agent/agentscripts/agentscripts_test.go`:
`TestExecuteCreatesMissingLogDir` runs a script with a nested,
nonexistent `LogPath` and asserts the streamed output and that the log
file is created.
* The test uses `afero.NewOsFs()` on purpose: `afero.NewMemMapFs()`
auto-creates parent directories on `OpenFile`, so it cannot reproduce
the reported failure.
* Verified red without the fix (`open .../does/not/exist/install.log: no
such file or directory`) and green with it.

**Local validation**

* `gofmt` clean, `go vet`, `go build`, `golangci-lint run` on the
package, and `go test -race ./agent/agentscripts/` all pass.

**End-to-end**

* Validated on a dev instance with a template whose
`coder_script.log_path` targets a nested directory that does not exist.
The agent created the parents with mode `0700` and wrote the log file;
the workspace agent reported healthy.

**Prior attempts**

* [#22796](<#22796>) and
[#25545](<#25545>) proposed the
same directory-creation approach. Both were closed for non-technical
reasons (a low-effort AI PR and a stale community PR), not rejected on
the merits. This supersedes them, authored by the issue owner, using
`0o700` and adding a regression test.

</details>

---

*Raised on behalf of* @35C4n0r *by Coder Agents.*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: log_path in coder script causes the script to fail silently

2 participants