Skip to content

std::fs::canonicalize fails to resolve /dev/stdin connected to pipe #95239

@crazystylus

Description

@crazystylus

When program tries to canonicalizes /dev/stdin it should resolve to tty, pipe or file as per configured redirection
I tried this code and attached a pipe to stdin:

use std::fs;

fn main() {
    let resolved_path = fs::canonicalize("/dev/stdin").unwrap();
    println!("{}", resolved_path.into_os_string().into_string().unwrap());
}
echo " " | cargo run

I expected to see this happen: /dev/stdin canonicalizes to /proc/14063/fd/pipe:[127582]

Instead, this happened:

    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/canon-issue`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:4:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Meta

rustc --version --verbose:

rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.59.0
LLVM version: 13.0.0
Backtrace

    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/canon-issue`
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:4:56
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Same thing in Python 3.9.7

import os
print(os.path.realpath("/dev/stdin"))
echo " " | python3 code.py

This is the result

/proc/16616/fd/pipe:[162072]

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`C-bugCategory: This is a bug.O-unixOperating system: Unix-likeT-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions