{"record":{"id":"674035de6d59bd3f","repo":"facebook/flow","slug":"failed-to-read-stdin-674035","errorCode":null,"errorMessage":"failed to read stdin","messagePattern":"failed to read stdin","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"rust_port/crates/flow_cli/src/env_builder_debug_command.rs","lineNumber":59,"sourceCode":"        \"Print the env-builder result as a dependency graph for debugging purposes\",\n        command_spec::Visibility::Internal,\n        format!(\n            \"Usage: {exe_name} env-builder-debug [OPTION]... [FILE]\\n\\ne.g. {exe_name} env-builder-debug foo.js\\nor   {exe_name} env-builder-debug < foo.js\\n\"\n        ),\n    );\n    let spec = command_utils::add_from_flag(spec);\n    let spec = command_utils::add_path_flag(spec);\n    spec.anon(\"file\", &arg_spec::optional(arg_spec::string()))\n}\n\nfn get_file(path: Option<String>, filename: Option<String>) -> FileInput {\n    match filename {\n        Some(filename) => FileInput::FileName(command_utils::expand_path(&filename)),\n        None => {\n            let mut content = String::new();\n            std::io::stdin()\n                .read_to_string(&mut content)\n                .expect(\"failed to read stdin\");\n            FileInput::FileContent(path, content.into())\n        }\n    }\n}\n\nstruct TestCx;\n\nimpl Context for TestCx {\n    fn enable_enums(&self) -> bool {\n        true\n    }\n\n    fn file(&self) -> FileKey {\n        FileKey::new(FileKeyInner::SourceFile(\"test.js\".to_string()))\n    }\n\n    fn jsx(&self) -> JsxMode {\n        JsxMode::JsxReact","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/facebook/flow/blob/f88ac94bcf6992f5d5a158854d94613ebb92c6e6/rust_port/crates/flow_cli/src/env_builder_debug_command.rs#L41-L77","documentation":"`flow env-builder-debug` reads the program to parse from stdin when no positional FILE argument is supplied (get_file). std::io::stdin().read_to_string streams all of stdin and requires it to be valid UTF-8; any io error — most commonly ErrorKind::InvalidData from non-UTF-8 bytes, but also a closed stdin fd (EBADF) or a failing writer upstream — panics via this expect.","triggerScenarios":"Piping a file that is not UTF-8 into the command (`cat legacy.js | flow env-builder-debug` where legacy.js is Latin-1/UTF-16 or contains binary bytes); invoking with stdin closed (`<&-`); an upstream process in the pipeline crashing mid-write so read_to_string returns an error.","commonSituations":"Source files saved by legacy Windows editors (Latin-1, UTF-16 with BOM) piped into the parser; CI scripts that exec flow without wiring stdin; accidentally feeding a binary or gzip artifact into the command.","solutions":["Pass the file as the positional `file` argument (`flow env-builder-debug path/to/file.js`) so stdin is never read.","Re-encode the input before piping: `iconv -f LATIN1 -t UTF-8 file.js | flow env-builder-debug`.","Pre-validate encoding: `iconv -f UTF-8 -t UTF-8 file.js > /dev/null` — it fails loudly on the first invalid byte.","Ensure stdin is a readable fd (do not launch with `0<&-` or from a process with closed stdin)."],"exampleFix":"// before\nlet mut content = String::new();\nstd::io::stdin()\n    .read_to_string(&mut content)\n    .expect(\"failed to read stdin\");\n\n// after\nlet mut bytes = Vec::new();\nstd::io::stdin().read_to_end(&mut bytes).expect(\"failed to read stdin\");\nlet content = match String::from_utf8(bytes) {\n    Ok(content) => content,\n    Err(e) => {\n        eprintln!(\"stdin is not valid UTF-8 at byte {}\", e.utf8_error().valid_up_to());\n        std::process::exit(1);\n    }\n};","handlingStrategy":"validation","validationCode":"let mut bytes = Vec::new();\nstd::io::stdin().read_to_end(&mut bytes).expect(\"failed to read stdin\");\nif !std::str::from_utf8(&bytes).is_ok() {\n    eprintln!(\"stdin is not valid UTF-8; re-encode input (iconv) or pass a FILE argument\");\n    std::process::exit(1);\n}","typeGuard":"fn is_utf8(bytes: &[u8]) -> bool {\n    std::str::from_utf8(bytes).is_ok()\n}","tryCatchPattern":"match String::from_utf8(bytes) {\n    Ok(content) => content,\n    Err(e) => {\n        eprintln!(\"stdin is not valid UTF-8 at byte {}\", e.utf8_error().valid_up_to());\n        std::process::exit(1);\n    }\n}","preventionTips":["Prefer passing the positional FILE argument over piping via stdin.","Re-encode legacy files with iconv (-f LATIN1/-f UTF-16 -t UTF-8) before piping.","Pre-validate with `iconv -f UTF-8 -t UTF-8 file > /dev/null`.","Never invoke the command with stdin closed (0<&-)."],"tags":["stdin","utf-8","encoding","cli","io-error","panic"],"backgroundTag":"stdin-invalid-utf8","analyzedSha":"f88ac94bcf6992f5d5a158854d94613ebb92c6e6","analyzedAt":"2026-08-20T10:41:37.992Z","schemaVersion":2},"datasetVersion":"2026-08-23T11:17:13.642Z"}