Skip to content

refactor(zig): Use more std library in zig v0.16#11

Merged
niheaven merged 5 commits into
ScoopInstaller:mainfrom
infirms:main
Jun 22, 2026
Merged

refactor(zig): Use more std library in zig v0.16#11
niheaven merged 5 commits into
ScoopInstaller:mainfrom
infirms:main

Conversation

@infirms

@infirms infirms commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for adding Zig support for shims. I wanted to contribute a small improvement.

Zig already defines many of these things in std, including NT internals. In Zig 0.16, a lot of the Windows code transitioned from Win32 APIs to NT APIs where possible, and I believe most of it has already been ported ( ~ 98 -99%).

This benefits us because we can reuse Zig’s existing helpers, definitions, and prototypes, which makes the code smaller, cleaner, and easier to maintain.

@infirms

infirms commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author
image

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4e9c6c0e-9fc8-49fc-92bc-b5a0cfd837bc

📥 Commits

Reviewing files that changed from the base of the PR and between 9260fbb and 8953ab8.

📒 Files selected for processing (1)
  • zig/version
✅ Files skipped from review due to trivial changes (1)
  • zig/version

Summary by CodeRabbit

  • Refactor
    • Updated the Windows compatibility layer to use standardized platform types/constants.
    • Simplified interop for process, file-handle, and shutdown behavior with clearer conventions.
    • Modernized Unicode literal handling for UTF-8 to UTF-16 conversion.
  • Bug Fixes
    • Corrected Windows API success/failure checks to use consistent boolean semantics.
    • Improved handling of infinite waits and shim file sizing using sentinel constants.
  • New Features
    • Added explicit support for file creation/open modes via FILE_CREATION_DISPOSITION.

Walkthrough

zig/shim.zig is refactored to replace custom Windows type aliases, numeric constants, and boolean comparisons with std.os.windows-backed types, std.unicode.utf8ToUtf16LeStringLiteral, an INFINITE constant, .toBool(), and .TRUE/.FALSE literals. CreateFileW call sites adopt struct-style windows.ACCESS_MASK/windows.FILE.SHARE arguments. Version bumped to 0.1.1.

Changes

Windows Interop Refactor

Layer / File(s) Summary
Type aliases, INFINITE constant, and extern declarations
zig/shim.zig
Replaces opaque handle/integer type definitions with std.os.windows-backed types, introduces INFINITE as a windows.DWORD, and removes the custom w() compile-time UTF-8→UTF-16 function in favor of std.unicode.utf8ToUtf16LeStringLiteral. Updates all extern "kernel32" function declarations to use windows.STARTUPINFOW and windows.PROCESS.INFORMATION instead of removed local structs.
Standard handle initialization with new types
zig/shim.zig
Updates ensureStandardHandles to use windows.STARTUPINFOW and changes CreateFileW arguments for CONIN$/CONOUT$ to struct-typed windows.ACCESS_MASK and windows.FILE.SHARE values.
Shim file operations with INFINITE constant
zig/shim.zig
Refactors getShimInfo's CreateFileW invocation to use the new windows.ACCESS_MASK/windows.FILE.SHARE signature, replaces the local extern CloseHandle binding with windows.CloseHandle, and updates file-size validity checking to compare against INFINITE instead of hardcoded 0xFFFFFFFF.
Boolean checks and error detection modernization
zig/shim.zig
Updates ctrlHandler return values to .TRUE/.FALSE struct literals, changes SetEnvironmentVariableW success check to .toBool(), updates SetConsoleCtrlHandler and CreateProcessW calls to use .TRUE and .toBool(), updates ShellExecuteExW success checks across elevated and non-elevated paths to .toBool(), and replaces the hardcoded 0xFFFFFFFF WaitForSingleObject timeout with INFINITE.

Version Bump

Layer / File(s) Summary
Version update
zig/version
Bumps version from 0.1.0 to 0.1.1.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately reflects the main change: refactoring the Zig shim to use more standard library definitions and removing custom protocol definitions like CloseHandle.
Description check ✅ Passed The description is directly related to the changeset, explaining the rationale for leveraging Zig's standard library more extensively for Windows/NT API definitions.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
zig/shim.zig (1)

575-579: ⚡ Quick win

Semantic mismatch: INFINITE is a timeout constant, not a file-size error indicator.

GetFileSize returns INVALID_FILE_SIZE (0xFFFFFFFF) on error, which happens to equal INFINITE. While numerically correct, using INFINITE here is misleading—readers may assume this relates to waiting. Consider defining a separate INVALID_FILE_SIZE constant or using std.math.maxInt(DWORD) inline with a comment.

♻️ Suggested clarification
+const INVALID_FILE_SIZE: DWORD = std.math.maxInt(DWORD);
+
 // ... later in getShimInfo:
     const file_size = GetFileSize(file_handle, null);
-    if (file_size == INFINITE) {
+    if (file_size == INVALID_FILE_SIZE) {
         writeError("Cannot get shim file size.\n");

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4a578cad-77b1-4a0a-838c-c4ff15e5e067

📥 Commits

Reviewing files that changed from the base of the PR and between ce24db4 and 77c48e2.

📒 Files selected for processing (1)
  • zig/shim.zig

Comment thread zig/shim.zig Outdated
Comment thread zig/shim.zig

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
zig/shim.zig (1)

81-89: ⚠️ Potential issue | 🔴 Critical

Fix type mismatch in CreateFileW calls — ACCESS_MASK and FILE.SHARE are u32 types, not structs.

Lines 223, 227, 231, and 566-573 pass struct literals (.{ .GENERIC = .{ .READ = true } } and .{ .READ = true }) to CreateFileW, but windows.ACCESS_MASK and windows.FILE.SHARE are type aliases for u32 in Zig 0.16's stdlib. These parameters expect bitmask constants like windows.GENERIC_READ (0x80000000), not struct values. The code will not compile. Use the appropriate u32 bitmask constants instead.

🧹 Nitpick comments (1)
zig/shim.zig (1)

583-587: ⚡ Quick win

Use a semantically correct constant for file size error checking.

GetFileSize returns INVALID_FILE_SIZE (0xFFFFFFFF) on error, not INFINITE. While both have the same numeric value, INFINITE semantically represents a timeout sentinel for wait functions. Using it here is misleading.

♻️ Suggested fix
+const INVALID_FILE_SIZE: windows.DWORD = 0xFFFFFFFF;
+
 // ... in getShimInfo:
     const file_size = GetFileSize(file_handle, null);
-    if (file_size == INFINITE) {
+    if (file_size == INVALID_FILE_SIZE) {
         writeError("Cannot get shim file size.\n");
         return error.FileReadError;
     }

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: cb6e4650-323f-4cb9-b86d-cf2133fc574c

📥 Commits

Reviewing files that changed from the base of the PR and between 77c48e2 and 119ee8c.

📒 Files selected for processing (1)
  • zig/shim.zig

Comment thread zig/shim.zig Outdated

@niheaven niheaven left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@niheaven niheaven changed the title feat(zig): shim refactor, remove CloseHandle proto, use more std refactor(zig): Use more std library in zig v0.16 Jun 22, 2026
@niheaven
niheaven merged commit f57cb53 into ScoopInstaller:main Jun 22, 2026
2 checks passed
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.

2 participants