Skip to content

fix: prevent EnsurePath panic on non-ENOENT stat errors#1588

Open
sozercan wants to merge 1 commit into
mainfrom
codex/fix-ensurepath-panic-on-os.stat-errors
Open

fix: prevent EnsurePath panic on non-ENOENT stat errors#1588
sozercan wants to merge 1 commit into
mainfrom
codex/fix-ensurepath-panic-on-os.stat-errors

Conversation

@sozercan
Copy link
Copy Markdown
Member

@sozercan sozercan commented May 4, 2026

Motivation

  • EnsurePath could dereference a nil FileInfo when os.Stat returned an error that was not ENOENT, which caused a panic for callers (e.g., ENOTDIR or permission errors).
  • The intent is to treat non-ENOENT os.Stat errors as immediate failures while preserving the existing behavior of creating missing directories.

Description

  • In pkg/utils/utils.go updated EnsurePath to immediately return non-ENOENT errors from os.Stat instead of dereferencing st, and to only call os.MkdirAll when the error is ENOENT.
  • Kept the existing checks for st.IsDir() and permission bits when os.Stat succeeds.
  • Added a regression test case StatErrorNotExist in pkg/utils/utils_test.go that exercises a non-ENOENT stat error path (e.g., path.Join(emptyFile, "child")) to ensure the function returns an error instead of panicking.

Testing

  • Added unit test StatErrorNotExist in pkg/utils alongside existing TestEnsurePath tests and validated locally that the test exercises the non-ENOENT path (regression coverage added).
  • Ran go test ./pkg/utils in this environment but the run failed due to module downloads from proxy.golang.org being forbidden, so tests could not complete here.

Codex Task

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a crash in pkg/utils.EnsurePath by correctly handling os.Stat failures that are not ENOENT (e.g., ENOTDIR, permission errors), returning the Stat error instead of dereferencing a nil FileInfo. It also adds a regression test to ensure the non-ENOENT error path returns an error and does not panic.

Changes:

  • Update EnsurePath to return immediately on non-os.IsNotExist errors from os.Stat.
  • Restrict os.MkdirAll to the ENOENT case and return its result directly.
  • Add a unit test case covering a non-ENOENT os.Stat error scenario (file/child => ENOTDIR-style error).
Show a summary per file
File Description
pkg/utils/utils.go Prevents EnsurePath panic by returning non-ENOENT os.Stat errors early and only creating directories on ENOENT.
pkg/utils/utils_test.go Adds regression coverage to ensure the non-ENOENT stat error path returns an error (and would fail if a panic regressed).

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 0

@codecov
Copy link
Copy Markdown

codecov Bot commented May 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 41.18%. Comparing base (da17cae) to head (c704b4c).
⚠️ Report is 36 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1588      +/-   ##
==========================================
+ Coverage   38.80%   41.18%   +2.37%     
==========================================
  Files          57       58       +1     
  Lines        9561    10114     +553     
==========================================
+ Hits         3710     4165     +455     
- Misses       5581     5653      +72     
- Partials      270      296      +26     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@omercnet omercnet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix looks correct. The lint failure appears to be CI infrastructure, not this diff: golangci-lint config schema fetch timed out. Please rename StatErrorNotExist to StatErrorNonENOENT since path under a file exercises ENOTDIR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

3 participants