{"record":{"id":"e75e144ce3f18794","repo":"wasmerio/wasmer","slug":"the-argument-to-cwd-must-be-an-absolute-path-e75e14","errorCode":null,"errorMessage":"The argument to --cwd must be an absolute path","messagePattern":"The argument to --cwd must be an absolute path","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/cli/src/commands/run/wasi.rs","lineNumber":390,"sourceCode":"                }\n            }\n\n            if !root_layers.is_empty() {\n                let existing_root = mount_fs\n                    .filesystem_at(Path::new(\"/\"))\n                    .expect(\"root fs builder should always mount /\");\n                mount_fs.set_mount(\n                    Path::new(\"/\"),\n                    Arc::new(OverlayFileSystem::new(\n                        ArcFileSystem::new(existing_root),\n                        root_layers,\n                    )),\n                )?;\n            };\n\n            if let Some(cwd) = self.cwd.as_ref() {\n                if !cwd.starts_with(\"/\") {\n                    bail!(\"The argument to --cwd must be an absolute path\");\n                }\n                builder = builder.current_dir(cwd.clone());\n            }\n\n            // Open the root of the new filesystem\n            builder = builder\n                .mount_fs(mount_fs)\n                .preopen_dir(Path::new(\"/\"))\n                .unwrap();\n\n            let dot_path = if have_current_dir {\n                PathBuf::from(MAPPED_CURRENT_DIR_DEFAULT_PATH)\n            } else {\n                PathBuf::from(\"/\")\n            };\n\n            builder.add_preopen_build(|p| {\n                p.directory(&dot_path)","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/wasmerio/wasmer/blob/8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5/lib/cli/src/commands/run/wasi.rs#L372-L408","documentation":"wasmer run validates the --cwd flag before configuring the WASIX WASI environment. If the provided current-directory argument does not start with '/', the builder refuses to set it as the guest's working directory. WASIX requires absolute paths for cwd because the guest filesystem has no notion of the host's relative directory context.","triggerScenarios":"Passing a relative path to --cwd, e.g. `wasmer run app.wasm --cwd .` or `--cwd ./build` in prepare() -> WasiEnvironmentBuilder::current_dir.","commonSituations":"Running wasmer from a shell with `--cwd $PWD` where a variable holds a relative path; scripting with `--cwd .`; CI configs that assume host-relative paths work; moving commands from containers (where relative cwd is tolerated) to wasmer.","solutions":["Convert the argument to an absolute path, e.g. use \"$PWD\" or $(realpath .) instead of \".\"","Drop the --cwd flag entirely if the default (root) working directory is acceptable","In wrappers/scripts, resolve the path with canonicalize/pathlib.abspath before passing --cwd"],"exampleFix":"// before\nwasmer run app.wasm --cwd ./workspace\n// after\nwasmer run app.wasm --cwd \"$(realpath ./workspace)\"","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn ensure_absolute_cwd(cwd: &str) -> Result<(), String> {\n    let p = Path::new(cwd);\n    if p.is_absolute() {\n        Ok(())\n    } else {\n        Err(format!(\"--cwd must be absolute, got {cwd:?}; use std::fs::canonicalize first\"))\n    }\n}","typeGuard":"fn is_absolute_path(p: &str) -> bool { std::path::Path::new(p).is_absolute() }","tryCatchPattern":"match wasmer_run_with_cwd(cwd) {\n    Err(e) if e.to_string().contains(\"must be an absolute path\") => {\n        let abs = std::fs::canonicalize(cwd)?;\n        wasmer_run_with_cwd(abs.to_str().unwrap())\n    }\n    other => other,\n}","preventionTips":["Always canonicalize paths (realpath / std::fs::canonicalize) before passing --cwd","Never pass '.' or './x' literals to --cwd","Add a shell check: [[ $CWD = /* ]] || exit 1"],"tags":["cli","wasi","argument-validation","path"],"backgroundTag":"relative-path-not-allowed","analyzedSha":"8c4b9ee9d33fb2068863fbb3d328683e7e6ff7f5","analyzedAt":"2026-09-01T23:06:31.009Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}