{"record":{"id":"545827aa6ef840e3","repo":"swc-project/swc","slug":"invalid-path-path","errorCode":null,"errorMessage":"Invalid path \"{path:?}\"","messagePattern":"Invalid path \"(.+?)\"","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/preset_env_base/src/query.rs","lineNumber":132,"sourceCode":"        Ok(result)\n    }\n}\n\npub fn targets_to_versions(v: Option<Targets>, path: Option<PathBuf>) -> Result<TargetInfo, Error> {\n    match v {\n        #[cfg(not(target_arch = \"wasm32\"))]\n        None => {\n            let mut browserslist_opts = browserslist::Opts {\n                mobile_to_desktop: true,\n                ignore_unknown_versions: true,\n                ..Default::default()\n            };\n            if let Some(path) = path {\n                browserslist_opts.path = path\n                    .clone()\n                    .into_os_string()\n                    .into_string()\n                    .map_err(|_| anyhow!(\"Invalid path \\\"{path:?}\\\"\"))?\n                    .into();\n            }\n            let distribs = browserslist::execute(&browserslist_opts)\n                .with_context(|| \"failed to resolve browserslist query from browserslist config\")?;\n\n            // When targets is None, it means the user didn't specify any targets.\n            // We use browserslist config or defaults, so we should NOT treat empty\n            // results as \"unknown version\". That case is for when a user explicitly\n            // specifies a query like \"Chrome > 99999\" that returns empty.\n            let versions = BrowserData::parse_versions(distribs)\n                .with_context(|| \"failed to parse browser version\")?;\n\n            Ok(TargetInfo {\n                versions: Arc::new(versions),\n                unknown_version: false,\n            })\n        }\n        #[cfg(target_arch = \"wasm32\")]","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/swc-project/swc/blob/d7d74346665e36e692aa07e13d4ee3ee692ee35c/crates/preset_env_base/src/query.rs#L114-L150","documentation":"Thrown by preset_env_base's targets_to_versions (crates/preset_env_base/src/query.rs) when a provided `path` cannot be converted to a UTF-8 String before being handed to browserslist as the config search directory (OsString::into_string fails on invalid UTF-8 bytes). It only occurs on non-wasm targets when env.targets is None, i.e. when browserslist config discovery is being performed relative to that path.","triggerScenarios":"Calling swc compile/preset-env without explicit targets while the project/config directory path contains non-UTF-8 bytes - e.g. Latin-1/CP1252 accented characters in a directory name on Windows or macOS, or invalid UTF-8 sequences from a misconfigured filesystem; the path is then used for browserslist's config lookup.","commonSituations":"Machines with localized usernames ('José', 'Dvořák') producing non-UTF-8 encodings; CI checkouts into byte-encoded paths; archives that preserved legacy encodings; monorepos whose root path includes such segments.","solutions":["Rename the offending directory (or run the build from) a path containing only valid UTF-8 characters","Specify explicit env.targets (or a browserslist query) in swc options so the filesystem config lookup with `path` is skipped","On Windows, ensure the path is not encoded in a legacy code page (check with a UTF-8 aware shell); on Unix, run `iconv`-style validation of the path bytes","Report upstream: browserslist's Opts.path could accept an OsString/PathBuf representation to avoid the lossy conversion"],"exampleFix":"# before: project lives at a non-UTF-8 path\n~/Jos\\xe9/project $ npx swc src -d out   # -> Invalid path\n\n# after: move or symlink to a UTF-8 path\n~/projects/jose/project $ npx swc src -d out","handlingStrategy":"validation","validationCode":"// Rust callers of preset_env_base: validate before invoking targets_to_versions\nfn ensure_utf8_path(path: &Path) -> Result<&str> {\n    path.to_str().ok_or_else(|| {\n        anyhow::anyhow!(\n            \"config path {} is not valid UTF-8; rename it or pass explicit targets\",\n            path.display()\n        )\n    })\n}\n// JS/CLI side: run the build from a UTF-8-only path\n// const ok = /^[\\x00-\\x7f -￿]*$/.test(process.cwd());","typeGuard":null,"tryCatchPattern":"// JS callers of @swc/core compile APIs\ntry {\n  await compileSwc(src, { /* no env.targets -> browserslist config lookup */ });\n} catch (e) {\n  if (String(e).includes('Invalid path')) {\n    // path bytes are not UTF-8: move the project or set explicit targets\n    return compileSwc(src, { env: { targets: 'defaults' } });\n  }\n  throw e;\n}","preventionTips":["Check project and username paths for non-UTF-8 characters before running swc without explicit targets","Always set env.targets (or browserslist query) in CI to avoid filesystem config discovery entirely","Prefer UTF-8 locales/encodings on Windows so OsString paths survive into_string()"],"tags":["preset-env","browserslist","path","utf-8","config","filesystem"],"backgroundTag":null,"analyzedSha":"d7d74346665e36e692aa07e13d4ee3ee692ee35c","analyzedAt":"2026-08-16T12:41:13.160Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}