Skip to content

host_env: os.mkdir for Windows, Redox#8128

Draft
joshuamegnauth54 wants to merge 1 commit into
RustPython:mainfrom
joshuamegnauth54:host_env-mkdir
Draft

host_env: os.mkdir for Windows, Redox#8128
joshuamegnauth54 wants to merge 1 commit into
RustPython:mainfrom
joshuamegnauth54:host_env-mkdir

Conversation

@joshuamegnauth54

@joshuamegnauth54 joshuamegnauth54 commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Python supports mkdir on Windows. I deferred to calling Rust's implementation for now. However, like rename, Python's implementation supports additional features that are currently unsupported on RustPython. I added a note for future reference.

Redox supports mkdirat which significantly cleans up the implementation.

Summary

  • Windows support for os.mkdir
  • Deduplicate Unix implementation

On a related note, I think the WASI Posix and Unix implementations can be combined eventually. 🤔

Summary by CodeRabbit

  • Refactor
    • Unified directory creation interface across platforms for improved consistency and maintainability.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 63e5e87b-54cc-4c1d-9972-ae2ac5350f33

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The PR unifies the directory-creation API in host_env: the separate make_dir(&CStr, mode) and make_dir_at(i32, &CStr, mode) functions are replaced by a single make_dir(Option<crt_fd::Borrowed<'_>>, &Path, mode) in both posix.rs and posix_wasi.rs, with rustix::fs::mkdirat as the backend. The os stdlib mkdir implementation and platform capability flags are updated to use the new signature.

Changes

Unified make_dir API refactor

Layer / File(s) Summary
New make_dir signature in posix.rs and posix_wasi.rs
crates/host_env/src/posix.rs, crates/host_env/src/posix_wasi.rs
Adds use crate::crt_fd import; replaces old make_dir(&CStr)/make_dir_at(i32, &CStr) pair with a single make_dir(Option<crt_fd::Borrowed<'_>>, &Path, mode). Non-Unix falls back to std::fs::create_dir or returns Unsupported; Unix and WASI delegate to rustix::fs::mkdirat with CWD when no fd is given.
os stdlib mkdir caller and platform flags
crates/vm/src/stdlib/os.rs
Simplifies cfg_select! AT_FDCWD condition to any(unix, target_os = "wasi"), changes MKDIR_DIR_FD from non-windows/non-redox to cfg!(unix), adds Path to std imports, and rewrites mkdir to call host_env::posix::make_dir with dir_fd.get_opt() and Path::new.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • RustPython/RustPython#7604: Introduced the crt_fd::Borrowed abstraction and the original make_dir/make_dir_at implementations that this PR directly replaces.
  • RustPython/RustPython#7636: Also modifies the cfg_select! AT_FDCWD/directory-fd fallback condition in crates/vm/src/stdlib/os.rs.

Suggested reviewers

  • youknowone

🐇 Hop, hop, hooray for paths so clean,
No more raw i32 hiding between!
Option<Borrowed> and &Path now unite,
mkdirat hums with rustix delight.
One make_dir to rule them all tonight! 🌿

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Title check ⚠️ Warning The title references Windows and Redox support, but the actual changes focus on refactoring the make_dir API to accept optional directory file descriptors and changing platform conditionals for Unix/non-Unix, not primarily adding Windows/Redox support. Consider retitling to reflect the core changes, e.g., 'Refactor make_dir API to support optional directory file descriptors' or 'Simplify make_dir with unified API for directory operations'.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
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.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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 `@crates/host_env/src/posix_wasi.rs`:
- Around line 4-10: The make_dir function uses &Path as a parameter type, but
Path is not imported from the standard library, causing a compilation error. Add
path::Path to the existing std imports at the top of the file where ffi::OsStr
and io are already imported to bring Path into scope.

In `@crates/vm/src/stdlib/os.rs`:
- Line 191: The MKDIR_DIR_FD constant in the os.rs file is currently configured
only for unix systems using cfg!(unix), but since WASI now supports the optional
dir_fd parameter in make_dir function as implemented in posix_wasi.rs, the cfg
condition needs to be updated. Modify the MKDIR_DIR_FD constant definition to
also include WASI targets in the condition so that dir_fd functionality is
enabled on WASI systems where it is now supported.
🪄 Autofix (Beta)

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: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 9e70d5e8-58e8-4b98-9525-1ee325ec8978

📥 Commits

Reviewing files that changed from the base of the PR and between fe2a7db and 9e4a98a.

📒 Files selected for processing (3)
  • crates/host_env/src/posix.rs
  • crates/host_env/src/posix_wasi.rs
  • crates/vm/src/stdlib/os.rs

Comment thread crates/host_env/src/posix_wasi.rs Outdated
Comment on lines +4 to +10
use std::{ffi::OsStr, io};

use crate::os::CheckLibcResult;
use crate::{crt_fd, os::CheckLibcResult};

pub fn make_dir(path: &CStr, mode: u32) -> io::Result<()> {
unsafe { libc::mkdir(path.as_ptr(), mode as _) }.check_libc_neg()?;
Ok(())
}

pub fn make_dir_at(dir_fd: i32, path: &CStr, mode: u32) -> io::Result<()> {
unsafe { libc::mkdirat(dir_fd, path.as_ptr(), mode as _) }.check_libc_neg()?;
Ok(())
pub fn make_dir(
dir_fd: Option<crt_fd::Borrowed<'_>>,
path: &Path,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

file="$(fd -i posix_wasi.rs | head -n1)"
echo "Using file: $file"

echo "== make_dir signature references Path =="
rg -n 'pub fn make_dir|path:\s*&Path' "$file"

echo "== imports that bring Path into scope (should have at least one match) =="
rg -n 'use\s+std::.*Path|use\s+std::path::Path' "$file" || true

Repository: RustPython/RustPython

Length of output: 265


🏁 Script executed:

head -n 20 crates/host_env/src/posix_wasi.rs

Repository: RustPython/RustPython

Length of output: 657


🏁 Script executed:

# Check what's in the CheckLibcResult import
fd -i checkclient.rs -o chelibcresult.rs || rg -l "CheckLibcResult" crates/host_env/src/ | head -n 3

Repository: RustPython/RustPython

Length of output: 322


Import Path for make_dir to compile.

Line 10 uses &Path, but Path is not in scope. Add path::Path to the standard library imports.

🔧 Proposed fix
-use std::{ffi::OsStr, io};
+use std::{ffi::OsStr, io, path::Path};
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
use std::{ffi::OsStr, io};
use crate::os::CheckLibcResult;
use crate::{crt_fd, os::CheckLibcResult};
pub fn make_dir(path: &CStr, mode: u32) -> io::Result<()> {
unsafe { libc::mkdir(path.as_ptr(), mode as _) }.check_libc_neg()?;
Ok(())
}
pub fn make_dir_at(dir_fd: i32, path: &CStr, mode: u32) -> io::Result<()> {
unsafe { libc::mkdirat(dir_fd, path.as_ptr(), mode as _) }.check_libc_neg()?;
Ok(())
pub fn make_dir(
dir_fd: Option<crt_fd::Borrowed<'_>>,
path: &Path,
use std::{ffi::OsStr, io, path::Path};
use crate::{crt_fd, os::CheckLibcResult};
pub fn make_dir(
dir_fd: Option<crt_fd::Borrowed<'_>>,
path: &Path,
🤖 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 `@crates/host_env/src/posix_wasi.rs` around lines 4 - 10, The make_dir function
uses &Path as a parameter type, but Path is not imported from the standard
library, causing a compilation error. Add path::Path to the existing std imports
at the top of the file where ffi::OsStr and io are already imported to bring
Path into scope.

Comment thread crates/vm/src/stdlib/os.rs Outdated

const OPEN_DIR_FD: bool = cfg!(not(any(windows, target_os = "redox")));
pub(crate) const MKDIR_DIR_FD: bool = cfg!(not(any(windows, target_os = "redox")));
pub(crate) const MKDIR_DIR_FD: bool = cfg!(unix);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Re-enable WASI dir_fd for mkdir capability flags.

Line [191] sets MKDIR_DIR_FD to cfg!(unix), which is false on WASI, so mkdir(..., dir_fd=...) is rejected even though crates/host_env/src/posix_wasi.rs now supports optional dir_fd in make_dir.

🔧 Proposed fix
-    pub(crate) const MKDIR_DIR_FD: bool = cfg!(unix);
+    pub(crate) const MKDIR_DIR_FD: bool = cfg!(any(unix, target_os = "wasi"));
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub(crate) const MKDIR_DIR_FD: bool = cfg!(unix);
pub(crate) const MKDIR_DIR_FD: bool = cfg!(any(unix, target_os = "wasi"));
🤖 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 `@crates/vm/src/stdlib/os.rs` at line 191, The MKDIR_DIR_FD constant in the
os.rs file is currently configured only for unix systems using cfg!(unix), but
since WASI now supports the optional dir_fd parameter in make_dir function as
implemented in posix_wasi.rs, the cfg condition needs to be updated. Modify the
MKDIR_DIR_FD constant definition to also include WASI targets in the condition
so that dir_fd functionality is enabled on WASI systems where it is now
supported.

@joshuamegnauth54 joshuamegnauth54 marked this pull request as draft June 18, 2026 01:27
Python supports `mkdir` on Windows. I deferred to calling Rust's
implementation for now. However, like `rename`, Python's implementation
supports additional features that are currently unsupported on
RustPython. I added a note for future reference.

Redox supports `mkdirat` which significantly cleans up the
implementation.
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.

1 participant