Skip to content

Conversation

@crepererum
Copy link
Contributor

Before this change, pyo3 cannot be compiled for wasip2:

❯ PYO3_CROSS_PYTHON_VERSION=3.14 PYO3_CROSS_LIB_DIR=... cargo check --target=wasm32-wasip2
   Compiling pyo3-ffi v0.25.1 (/home/mneumann/src/pyo3/pyo3-ffi)
   Compiling pyo3 v0.26.0-dev (/home/mneumann/src/pyo3)
error[E0658]: use of unstable library feature `wasip2`
  --> src/conversions/std/osstr.rs:26:25
   |
26 |             let bytes = std::os::wasi::ffi::OsStrExt::as_bytes(self);
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0658]: use of unstable library feature `wasip2`
  --> src/conversions/std/osstr.rs:90:17
   |
90 |                 std::os::wasi::ffi::OsStrExt::from_bytes(fs_encoded_bytes.as_bytes(ob.py()));
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0658`.

(you can also use a nightly compiler and/or add --features=nightly, this won't change anything)

Now it can be compiled with:

PYO3_CROSS_PYTHON_VERSION=3.14 PYO3_CROSS_LIB_DIR=... cargo +nightly check --target=wasm32-wasip2 --features=nightly

It's a bit sad that this requires nightly, but until rust-lang/rust#130323 is fixed, we cannot do much about it. A similar workaround was chosen by tempfile.

@crepererum crepererum force-pushed the crepererum/wasip2-compilation-fix branch from d65bd04 to 1c31705 Compare August 27, 2025 10:28
@bschoenmaeckers
Copy link
Member

bschoenmaeckers commented Aug 29, 2025

I think we can make this work on stable. As OsStr coming from the wasip2 is guaranteed to be valid utf8. So OsStr::to_str should almost never fail. And if it does fail it is clearly a bug.

@crepererum
Copy link
Contributor Author

Good point. I think this should work, but can you check that this makes sense to you?

OS => bytes

let bytes = std::os::wasi::ffi::OsStrExt::as_bytes(self);

in WASI should just be

let bytes = self.as_encoded_bytes(); // WASI strings are UTF8

?

bytes => OS

let os_str: &OsStr =
    std::os::wasi::ffi::OsStrExt::from_bytes(fs_encoded_bytes.as_bytes(ob.py()));

should be

let os_str: &OsStr = OsStr::new(
    str::from_utf8(fs_encoded_bytes.as_bytes(ob.py()))?
);

(using this error conversion)

@bschoenmaeckers
Copy link
Member

Using self.as_encoded_bytes() sounds somewhat scary. As the docs say that it is an unspecified encoding that only roundtrips well on the same platform/rust version. Maybe this is not a big issue but I think we may want to play safe and error when it is not valid utf-8.

Maybe an other maintainer has some more insight in possible failure cases.

@davidhewitt
Copy link
Member

If we expect utf8 to always be the case, perhaps it's good enough to panic in this instance for now? I.e. to OS => bytes conversion can be

let bytes = self.to_str().expect("wasi strings are UTF8").as_bytes();

@crepererum crepererum force-pushed the crepererum/wasip2-compilation-fix branch from 1c31705 to 1c5f96e Compare September 22, 2025 14:12
@crepererum crepererum changed the title fix: compilation on (nightly) wasip2 fix: compilation on wasip2 Sep 22, 2025
@crepererum crepererum force-pushed the crepererum/wasip2-compilation-fix branch from 1c5f96e to 0882789 Compare September 22, 2025 14:13
WASI strings are UTF8, so we don't need the nightly-only APIs. See
rust-lang/rust#130323 as well.
@crepererum crepererum force-pushed the crepererum/wasip2-compilation-fix branch from 0882789 to c68b395 Compare September 22, 2025 14:34
@crepererum
Copy link
Contributor Author

This now uses the proposed stable-Rust conversation routines and is ready for another round of reviews.

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Thanks for reporting and fixing!

@davidhewitt davidhewitt added this pull request to the merge queue Oct 8, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Oct 9, 2025
@davidhewitt davidhewitt added this pull request to the merge queue Oct 9, 2025
Merged via the queue into PyO3:main with commit 558b8a1 Oct 9, 2025
43 checks passed
crepererum added a commit to crepererum/pyo3 that referenced this pull request Oct 10, 2025
WASI strings are UTF8, so we don't need the nightly-only APIs. See
rust-lang/rust#130323 as well.
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.

3 participants