Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some bugs about file system on emscripten target. #119250

Closed
TheVeryDarkness opened this issue Dec 23, 2023 · 8 comments
Closed

Some bugs about file system on emscripten target. #119250

TheVeryDarkness opened this issue Dec 23, 2023 · 8 comments
Labels
C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@TheVeryDarkness
Copy link

TheVeryDarkness commented Dec 23, 2023

I tried this code:

//! This is src/bin/metadata.rs.
//! Compiled to wasm32-unknown-emscripten.
//! My codes are in <https://github.com/TheVeryDarkness/wasm-emscripten-fs-bug>.
use std::fs::{create_dir, metadata, read_dir, write};

fn main() {
    create_dir("workspace");
    write("workspace/a.txt", "I'm a.\n").unwrap();
    write("workspace/b.txt", "This is b.\n").unwrap();
    for file in read_dir("workspace").unwrap() {
        let file = file.unwrap();
        println!("{:?}", file.path());
        let meta = file.metadata().unwrap();
        println!("{:?}", meta);
        println!("{}", meta.len());
    }
    let meta = metadata("workspace").unwrap();
    println!("{:?}", meta);
    // This seems to be the same with permission code.
    println!("{:?}", meta.file_type());
    println!("{}", meta.is_dir());
    println!("{}", meta.is_file());
    println!("{}", meta.is_symlink());
}

I expected to see this happen: got correct file_type() and len().

Instead, this happened: file_type() returns permission code, while len() returns created time..

Meta

rustc --version --verbose:

wasm_emscripten_fs_bug % rustc +nightly --version --verbose
rustc 1.77.0-nightly (d6d7a9386 2023-12-22)
binary: rustc
commit-hash: d6d7a93866f2ffcfb51828b8859bdad760b54ce0
commit-date: 2023-12-22
host: aarch64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6
Backtrace

No panic here, so there's no RUST_BACKTRACE.

Running with RUST_BACKTRACE=1 cargo +nightly run -Zbuild-std --target wasm32-unknown-emscripten --release -vv --bin metadata.

@TheVeryDarkness TheVeryDarkness added the C-bug Category: This is a bug. label Dec 23, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 23, 2023
@Nilstrieb Nilstrieb added O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 23, 2023
@Nilstrieb
Copy link
Member

std just forwards from libc

pub fn metadata(&self) -> io::Result<FileAttr> {

pub fn stat(p: &Path) -> io::Result<FileAttr> {

https://github.com/rust-lang/libc/blob/6424b09903dd0e5a3e25a541299da71f88bca932/src/unix/linux_like/emscripten/mod.rs#L256

libc seems to have some cfgs about new stat ABIs.. that seems relevant. it activates this cfg when emcc has version >3.1.42. what's your emcc -dumpversion?

@TheVeryDarkness
Copy link
Author

@Nilstrieb Sorry, I didn't see the notification.

wasm_emscripten_fs_bug % emcc -dumpversion
3.1.50-git

I'm using emscripten installed with Homebrew, and it's usually fresh enough.

@kleisauke
Copy link
Contributor

It looks like the Emscripten version parser in libc doesn't understand the -git suffix in version numbers.
https://github.com/rust-lang/libc/blob/98999135403524e510d4cdd357b507bf1e5c3e42/build.rs#L259-L284

I always thought this -git suffix would only be included for "tip-of-tree builds". It's a bit unfortunate that this also happens with the Emscripten distributed by Homebrew.

As a possible workaround for now, you can patch emscripten-version.txt to remove this suffix:

$ emcc -dumpversion
3.1.52-git
$ cat $EMSDK/upstream/emscripten/emscripten-version.txt
3.1.52-git
$ sed -i 's/-git//' $EMSDK/upstream/emscripten/emscripten-version.txt
$ emcc -dumpversion
3.1.52

(on macOS, you'll probably need to change it to use $(brew --prefix emscripten)/libexec/emscripten-version.txt instead)

@TheVeryDarkness
Copy link
Author

It looks like the Emscripten version parser in libc doesn't understand the -git suffix in version numbers.
https://github.com/rust-lang/libc/blob/98999135403524e510d4cdd357b507bf1e5c3e42/build.rs#L259-L284

I always thought this -git suffix would only be included for "tip-of-tree builds". It's a bit unfortunate that this also happens with the Emscripten distributed by Homebrew.

As a possible workaround for now, you can patch emscripten-version.txt to remove this suffix:

$ emcc -dumpversion
3.1.52-git
$ cat $EMSDK/upstream/emscripten/emscripten-version.txt
3.1.52-git
$ sed -i 's/-git//' $EMSDK/upstream/emscripten/emscripten-version.txt
$ emcc -dumpversion
3.1.52

(on macOS, you'll probably need to change it to use $(brew --prefix emscripten)/libexec/emscripten-version.txt instead)

@kleisauke Yes, it works. Thanks a lot. Would you open an issue to libc?

kleisauke added a commit to kleisauke/libc that referenced this issue Dec 24, 2023
Some Emscripten versions come with `-git` attached, so trim any
non-numeric chars from the end of the string.

See: rust-lang/rust#119250.
kleisauke added a commit to kleisauke/libc that referenced this issue Dec 24, 2023
Some Emscripten versions come with `-git` attached, so trim any
non-numeric chars from the end of the string.

See: rust-lang/rust#119250.
@kleisauke
Copy link
Contributor

I just opened PR rust-lang/libc#3498 for this.

@TheVeryDarkness
Copy link
Author

I just opened PR rust-lang/libc#3498 for this.

Thanks a lot.

@TheVeryDarkness
Copy link
Author

TheVeryDarkness commented Dec 25, 2023

Well, I just heard that Emscripten from Homebrew is unofficial.

And there's still more problems when I run my program. Maybe I'll install Emscripten with emsdk later, and test my program again.

bors added a commit to rust-lang/libc that referenced this issue Dec 29, 2023
Improve the version parser of Emscripten

Some Emscripten versions come with `-git` attached, so trim any non-numeric chars from the end of the string.

See: rust-lang/rust#119250.
kleisauke added a commit to kleisauke/libc that referenced this issue Dec 29, 2023
Some Emscripten versions come with `-git` attached, so split the
version string also on the `-` char.

See: rust-lang/rust#119250.
bors added a commit to rust-lang/libc that referenced this issue Dec 30, 2023
Improve the version parser of Emscripten

Some Emscripten versions come with `-git` attached, so split the version string also on the `-` char.

See: rust-lang/rust#119250.
@TheVeryDarkness
Copy link
Author

Thanks a lot for you all! And I have another problem with strict mode in emscripten, which is not so critical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-wasm Target: WASM (WebAssembly), http://webassembly.org/ T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants