Skip to content

Fix (*os.File).Stat() returning Size() = 0#1506

Merged
xushiwei merged 1 commit intomainfrom
xgopilot/claude/issue-1503-1766489071
Dec 24, 2025
Merged

Fix (*os.File).Stat() returning Size() = 0#1506
xushiwei merged 1 commit intomainfrom
xgopilot/claude/issue-1503-1766489071

Conversation

@xgopilot
Copy link
Contributor

@xgopilot xgopilot bot commented Dec 23, 2025

Requested by @cpunion

Fixes #1503
Fixes #1475

Summary

Fixed a bug where (*os.File).Stat() returns Size() = 0 on an open file, while standard Go returns the correct file size.

This fix also resolves issue #1475, enabling llama2-go to compile and run correctly with llgo. The issue was that llama2-go uses encoding/binary to read binary model files, which internally relies on (*os.File).Stat() to get file sizes.

Root Cause

In runtime/internal/lib/os/stat_unix.go:17-27, the File.Stat() method calls os.Fstat() to get the syscall stat structure but doesn't call fillFileStatFromSys() to copy the fields (including size) from the syscall structure to the fileStat structure. This caused all fields including size to remain at their zero values.

In contrast, statNolog() and lstatNolog() properly call fillFileStatFromSys() after getting the syscall data.

Changes

  • Added fillFileStatFromSys(&fs, f.name) call in File.Stat() after os.Fstat()
  • Created test case in _cmptest/filestat/ to verify the fix

Test Plan

  • Created and verified test case with standard Go
  • Ran project test suite (go test ./cl/...)
  • Formatted code with go fmt ./...
  • Verified fix resolves llama2-go compatibility issue

Expected Behavior

Both os.Stat() and file.Stat() now correctly return the file size:

os.Stat():   size = 400
file.Stat(): size = 400

Generated with codeagent

The issue was that File.Stat() in stat_unix.go called os.Fstat() to get
the syscall stat structure but didn't call fillFileStatFromSys() to copy
the fields (including size) from the syscall structure to the fileStat
structure. This caused all fields to remain at their zero values.

The fix adds the missing fillFileStatFromSys(&fs, f.name) call after
os.Fstat(), consistent with how statNolog() and lstatNolog() work.

Added test case in _cmptest/filestat/ to verify the fix.

Fixes #1503

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: cpunion <8459+cpunion@users.noreply.github.com>
@codecov
Copy link

codecov bot commented Dec 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.93%. Comparing base (05299c6) to head (21e4bfe).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1506   +/-   ##
=======================================
  Coverage   90.93%   90.93%           
=======================================
  Files          44       44           
  Lines       11525    11525           
=======================================
  Hits        10480    10480           
  Misses        883      883           
  Partials      162      162           

☔ 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.

@xushiwei xushiwei merged commit 3285c84 into main Dec 24, 2025
43 checks passed
@xgopilot xgopilot bot deleted the xgopilot/claude/issue-1503-1766489071 branch December 24, 2025 23:32
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.

(*os.File).Stat() returns Size() = 0 Support compiling and running llama2-go with llgo

2 participants