{"record":{"id":"aa65fe33fd0e8e72","repo":"wasmerio/wasmer","slug":"dir-must-be-a-valid-string","errorCode":null,"errorMessage":"dir must be a valid string","messagePattern":"dir must be a valid string","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/cli/src/commands/run/wasi.rs","lineNumber":700,"sourceCode":"        let (instance, wasi_env) = builder.instantiate_ext(module.clone(), module_hash, store)?;\n\n        Ok((wasi_env, instance))\n    }\n\n    pub fn for_binfmt_interpreter() -> Result<Self> {\n        let dir = std::env::var_os(\"WASMER_BINFMT_MISC_PREOPEN\")\n            .map(Into::into)\n            .unwrap_or_else(|| PathBuf::from(\".\"));\n        Ok(Self {\n            deny_multiple_wasi_versions: true,\n            env_vars: std::env::vars_os()\n                .map(|(name, value)| Ok((utf8_env_part(name)?, utf8_env_part(value)?)))\n                .collect::<Result<_>>()?,\n            volumes: vec![MappedDirectory {\n                host: dir.clone(),\n                guest: dir\n                    .to_str()\n                    .expect(\"dir must be a valid string\")\n                    .to_string(),\n            }],\n            ..Self::default()\n        })\n    }\n\n    fn prepare_package_loader(\n        &self,\n        env: &WasmerEnv,\n        client: Arc<dyn HttpClient + Send + Sync>,\n    ) -> Result<BuiltinPackageLoader> {\n        let checkout_dir = env.cache_dir().join(\"checkouts\");\n        let tokens = tokens_by_authority(env)?;\n\n        let loader = BuiltinPackageLoader::new()\n            .with_cache_dir(checkout_dir)\n            .with_shared_http_client(client)\n            .with_tokens(tokens);","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/wasmerio/wasmer/blob/8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5/lib/cli/src/commands/run/wasi.rs#L682-L718","documentation":"When constructing a WASI run configuration for a single directory argument (Self constructor in the WASI runner), the host dir PathBuf is converted to a guest string with .to_str().expect(...). A directory path that is not valid UTF-8 causes this panic, because the guest path is represented as a String.","triggerScenarios":"Creating the WASI environment with a directory (e.g. from `wasmer run --dir <path>` or an API equivalent) whose path contains non-UTF-8 bytes, hitting the expect at wasi.rs:700.","commonSituations":"Passing a mount/dir path built from raw OS bytes on Linux; programmatically constructing WasiEnv with paths taken from non-UTF-8 environment variables or filesystem listings.","solutions":["Ensure the dir path is valid UTF-8 (rename the directory or use an ASCII-safe path)","Convert with lossy conversion on your side before passing (replace invalid bytes) if a lossy name is acceptable","Sanitize the source of the path (env vars, argv) to valid UTF-8 before constructing the WASI state","Upgrade wasmer for graceful handling of non-UTF-8 paths"],"exampleFix":"// before\nlet dir = std::env::current_dir()?; // may be non-UTF-8\n// after\nlet dir_str = std::env::current_dir()?.into_os_string().into_string()\n    .map_err(|_| anyhow::anyhow!(\"dir must be valid UTF-8\"))?;\nlet dir = PathBuf::from(dir_str);","handlingStrategy":"validation","validationCode":"let dir = std::path::Path::new(raw_dir);\nif dir.to_str().is_none() {\n    return Err(anyhow::anyhow!(\"--dir path must be valid UTF-8\"));\n}","typeGuard":"fn valid_dir(p: &std::path::Path) -> Option<&str> { p.to_str() }","tryCatchPattern":"std::panic::catch_unwind(|| build_wasi_state(dir))\n    .map_err(|_| anyhow::anyhow!(\"dir path is not valid UTF-8\"))?","preventionTips":["Validate UTF-8 of every directory path before constructing WasiState","Sanitize paths sourced from env vars and argv","Prefer String paths in your config layer, converting from OsString explicitly with error handling","Test the CLI with unusual path encodings in CI"],"tags":["wasi","path","utf-8","panic","cli"],"backgroundTag":"invalid-utf8-path","analyzedSha":"8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5","analyzedAt":"2026-09-01T23:06:31.009Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}