{"record":{"id":"0bea49dbe8985ec8","repo":"swc-project/swc","slug":"is-not-a-valid-expression","errorCode":null,"errorMessage":"{} is not a valid expression","messagePattern":"(.+?) is not a valid expression","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc/src/config/mod.rs","lineNumber":2235,"sourceCode":"        fn expr(cm: &SourceMap, handler: &Handler, src: String) -> Box<Expr> {\n            let fm = cm.new_source_file(FileName::Anon.into(), src);\n\n            let mut errors = Vec::new();\n            let expr = parse_file_as_expr(\n                &fm,\n                Syntax::Es(Default::default()),\n                Default::default(),\n                None,\n                &mut errors,\n            );\n\n            for e in errors {\n                e.into_diagnostic(handler).emit()\n            }\n\n            match expr {\n                Ok(v) => v,\n                _ => panic!(\"{} is not a valid expression\", fm.src),\n            }\n        }\n\n        fn mk_map(\n            cm: &SourceMap,\n            handler: &Handler,\n            values: impl Iterator<Item = (Atom, Atom)>,\n            is_env: bool,\n        ) -> ValuesMap {\n            let mut m = HashMap::default();\n\n            for (k, v) in values {\n                let v = if is_env {\n                    format!(\"'{v}'\")\n                } else {\n                    (*v).into()\n                };\n                let v_str = v.clone();","sourceCodeStart":2217,"sourceCodeEnd":2253,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc/src/config/mod.rs#L2217-L2253","documentation":"Runtime panic while building swc's `jsc.globalPassOptions` (GlobalPassOption::build in crates/swc/src/config/mod.rs). Every entry in `vars` (and every env value, wrapped as `'VALUE'` before parsing) is run through `parse_file_as_expr` with ES syntax; parse errors are emitted to the handler first, then the function panics with `{} is not a valid expression`, printing the offending source text.","triggerScenarios":"A `.swcrc`/config `jsc.globalPassOptions.vars` value that is not a valid JS expression (e.g. `\"1 +\"`); or an `envs` entry whose environment variable content contains a single quote (e.g. `IT'S`) — the `'{v}'` wrapping then produces invalid JS and parsing fails.","commonSituations":"Inline-replacing `process.env.*` with values containing apostrophes or unbalanced quotes/braces; env vars read from user machines (locales, paths) leaking quotes into the generated expression; multiline env values.","solutions":["Make each vars value a valid JS expression: quote strings (`'\"production\"'` or `\\\"production\\\"`) and escape embedded quotes","For envs, sanitize the variable content before it reaches the config — escape or strip single quotes","Pre-validate every expression string with swc_ecma_parser before handing the config to swc","Remove `globalPassOptions` if the global inlining pass is not needed"],"exampleFix":"// before (.swcrc)\n\"globalPassOptions\": { \"vars\": { \"process.env.MSG\": \"it's fine\" } }\n\n// after (.swcrc)\n\"globalPassOptions\": { \"vars\": { \"process.env.MSG\": \"\\\"it's fine\\\"\" } }","handlingStrategy":"validation","validationCode":"use swc_ecma_parser::{lexer::Lexer, Parser, Syntax, EsSyntax};\nuse swc_common::{FileName, SourceMap, sync::Lrc};\n\nfn is_valid_js_expr(cm: &Lrc<SourceMap>, src: &str) -> bool {\n    let fm = cm.new_source_file(FileName::Anon.into(), src.to_string());\n    let lexer = Lexer::new(\n        Syntax::Es(EsSyntax::default()),\n        Default::default(),\n        &fm,\n        None,\n    );\n    let mut p = Parser::new_from(lexer);\n    p.parse_expr().is_ok()\n}\n\n// validate every globalPassOptions value before building the config\nlet ok = cfg.global_pass_options\n    .vars\n    .iter()\n    .all(|(k, v)| is_valid_js_expr(&cm, v));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Quote string values in `vars` so they parse as JS string literals","Escape or strip single quotes from env values before they get wrapped in `'...'`","Treat environment content as untrusted input to config parsing"],"tags":["swc","config","swcrc","javascript-expression","global-pass","env-vars"],"backgroundTag":"invalid-config-expression","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}