info: show if package exists in store - #18
Conversation
📝 WalkthroughWalkthrough
ChangesPackage store status
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/actions/info.zig`:
- Around line 90-101: Update the access handling in the path-checking block of
the info action to print an explicit missing-store status for
error.FileNotFound, while propagating or reporting any other access errors
instead of ignoring them; preserve the existing green “Exists in store” output
for successful access.
🪄 Autofix
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: f1d9af5f-c28a-4e7b-be79-b27dd1f61f90
📒 Files selected for processing (1)
src/actions/info.zig
| var path_buf: [Io.Dir.max_path_bytes]u8 = undefined; | ||
| const path = try std.fmt.bufPrint(&path_buf, "/opt/packa/store/{s}-{f}-{s}", .{ | ||
| pkg.name.slice(&state.string_state), pkg.version, pkg_id.slice(&state.string_state)[0..32], | ||
| }); | ||
|
|
||
| // TODO: improve message | ||
| if (Io.Dir.cwd().access(io, path, .{})) { | ||
| try t.setColor(.green); | ||
| try t.writer.print("Exists in store\n", .{}); | ||
| try t.setColor(.reset); | ||
| } else |_| {} | ||
|
|
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C 6 'Io\.Dir\.access|\.access\(io|catch \|_\|' --glob '*.zig' .
fd -H -t f -g 'build.zig' -g 'build.zig.zon' -g '.zig-version' -g '.tool-versions' . \
-x rg -n -C 3 'zig|version|minimum' {}Repository: D-Berg/packa
Length of output: 11127
🏁 Script executed:
set -euo pipefail
printf '%s\n' '--- manifest and toolchain references ---'
fd -H -t f . | sort | rg '(^|/)(build\.zig(\.zon)?|\.zig-version|\.tool-versions|README.*)$' | while read -r f; do
printf '\n### %s\n' "$f"
sed -n '1,80p' "$f"
done
printf '%s\n' '--- relevant source context ---'
sed -n '1,45p' src/util.zig
sed -n '80,170p' src/actions/info.zigRepository: D-Berg/packa
Length of output: 9350
🌐 Web query:
Zig std.Io.Dir.access error.NotFound API Zig 0.14 0.15
💡 Result:
In Zig versions 0.14 and 0.15, std.fs.Dir.access is the standard function used to check for the existence or accessibility of a file or directory [1][2]. When the specified path does not exist, this function returns error.FileNotFound [3][2]. API Usage and Best Practices The function signature is pub fn access(dir: Dir, io: Io, sub_path: []const u8, options: AccessOptions) AccessError!void (often accessed via std.fs.cwd().access(...)) [3]. When using this function to check if a file exists, it is critical to handle error.FileNotFound explicitly [2]. A common pitfall is the Time-Of-Check-To-Time-Of-Use (TOCTOU) race condition [1][4]. The official Zig documentation and community best practices strongly advise against using access to check if a file exists before opening it [1][4][2]. Instead, you should simply attempt to open the file and handle the error.FileNotFound result from the open operation itself [1][4][2]. This approach is more robust and prevents potential race conditions between the check and the actual file access [4][2]. Context for Zig 0.14/0.15 Zig has been undergoing significant refactoring of its standard library, including moving toward an updated I/O API [5][6][7]. While std.fs.Dir.access remains a standard component for path testing, some older std.fs methods have been deprecated in favor of newer std.Io equivalents [7]. Always ensure you are checking the latest documentation for your specific Zig version, as the standard library continues to evolve [6]. If you encounter unexpected behavior, such as crashes or panics when passing specific paths (e.g., extremely long paths on Windows), be aware that these may be tracked as specific issues in the Zig issue tracker [8], rather than expected API behavior.
Citations:
- 1: https://stackoverflow.com/questions/77179502/check-if-a-file-exists-in-zig
- 2: https://ziggit.dev/t/does-a-file-exist-for-read-write/5085
- 3: https://github.com/ziglang/zig/blob/c58687225ba961c94b069f5bea3036267b80ebc6/lib/std/Io/Dir.zig
- 4: https://github.com/ziglang/zig/blob/master/lib/std/fs.zig
- 5: https://git.jakstys.lt/motiejus/zig/src/commit/39ee46a6c1b5a56129950953d304a1169b7ffa67/src/main.cpp
- 6: https://ziglang.org/download/0.15.1/release-notes.html
- 7: https://github.com/ziglang/zig/blob/master/lib/std/fs/Dir.zig
- 8: Panic due to Out-of-Bounds Access in std.fs.Dir.access on Windows with Long Paths ziglang/zig#24442
🌐 Web query:
site:github.com/ziglang/zig 0.16.0 lib/std/Io/Dir.zig access error.FileNotFound
💡 Result:
In Zig 0.16.0, an error.FileNotFound returned by a function in lib/std/fs/Dir.zig (or lib/std/Io/Dir.zig) indicates that the requested file or directory path does not exist [1][2][3]. This error is a standard member of the error sets for various directory and file operations in the Zig standard library, including AccessError, OpenError, MakeError, and DeleteDirError [1][2][4]. If you are encountering this error when you believe the file should exist, consider the following common causes: 1. Incorrect Path: Verify that the sub_path provided to the function is correct relative to the Dir object being used [3]. 2. Race Conditions: If the file was expected to be created or accessed by another process, it may have been deleted or moved, resulting in a race condition [5][4]. 3. Permissions or Mounts: On some systems, especially when using tools like Wine or specific container environments, path resolution or permission settings can cause unexpected FileNotFound errors [6]. 4. Symlinks: If the path involves a symbolic link, ensure the target of the link exists and is accessible [3]. You can debug this by printing the path being accessed just before the call to ensure it matches your expectations, or by using std.fs.cwd.realpathAlloc(allocator, path) to resolve the absolute path for verification. If the error persists unexpectedly, check your operating system's specific behavior regarding the file system operations involved [6].
Citations:
- 1: https://github.com/ziglang/zig/blob/c58687225ba961c94b069f5bea3036267b80ebc6/lib/std/Io/Dir.zig
- 2: https://github.com/ziglang/zig/blob/master/lib/std/fs/Dir.zig
- 3: https://github.com/ziglang/zig/blob/master/lib/std/fs/test.zig
- 4: https://github.com/ziglang/zig/blob/d6b4e1918b77e2992038d385d0951c7c926703a1/lib/std/fs/Dir.zig
- 5: Dir.statFile panics when given a file that doesn't exist. ziglang/zig#10375
- 6: with 64-bit wine, std.fs.Dir.openDir incorrectly reports error.FileNotFound when opening a sub-directory ziglang/zig#3500
Report the missing-store result explicitly.
When access returns error.FileNotFound, print an explicit missing status. Propagate or report all other errors instead of treating them as absence.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/actions/info.zig` around lines 90 - 101, Update the access handling in
the path-checking block of the info action to print an explicit missing-store
status for error.FileNotFound, while propagating or reporting any other access
errors instead of ignoring them; preserve the existing green “Exists in store”
output for successful access.
Summary by CodeRabbit
New Features
Performance