{"record":{"id":"1ea0e04313d5ef58","repo":"swc-project/swc","slug":"sourcemap-proxy-cannot-be-called-in-this-context","errorCode":null,"errorMessage":"Sourcemap proxy cannot be called in this context","messagePattern":"Sourcemap proxy cannot be called in this context","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs","lineNumber":90,"sourceCode":"    where\n        F: FnOnce(&str, usize, usize) -> Ret,\n    {\n        #[cfg(target_arch = \"wasm32\")]\n        {\n            use swc_common::plugin::serialized::ResultValue;\n\n            let src: ResultValue<String, Box<SpanSnippetError>> =\n                read_returned_result_from_host(|serialized_ptr| unsafe {\n                    __span_to_source_proxy(sp.lo.0, sp.hi.0, serialized_ptr)\n                })\n                .expect(\"Host should return source code\");\n\n            let src = src.0?;\n            return Ok(extract_source(&src, 0, src.len()));\n        }\n\n        #[cfg(not(target_arch = \"wasm32\"))]\n        unimplemented!(\"Sourcemap proxy cannot be called in this context\")\n    }\n}\n\n/// Subset of SourceMap interface supported in plugin.\n/// Unlike `Comments`, this does not fully implement `SourceMap`.\n#[cfg(feature = \"__plugin_mode\")]\nimpl SourceMapper for PluginSourceMapProxy {\n    #[cfg_attr(not(target_arch = \"wasm32\"), allow(unused))]\n    fn lookup_char_pos(&self, pos: BytePos) -> Loc {\n        #[cfg(target_arch = \"wasm32\")]\n        {\n            let should_request_source_file = if self.source_file.get().is_none() {\n                1\n            } else {\n                0\n            };\n            let partial_loc: PartialLoc = read_returned_result_from_host(|serialized_ptr| unsafe {\n                __lookup_char_pos_source_map_proxy(","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_plugin_proxy/src/source_map/plugin_source_map_proxy.rs#L72-L108","documentation":"PluginSourceMapProxy implements SourceMapper for code running inside an SWC wasm plugin by forwarding calls to host functions imported from the wasm module's env (here __span_to_source_proxy). Those extern imports can only be linked in a wasm32 plugin guest; the non-wasm32 compilation of span_to_source therefore ends in unimplemented!(\"Sourcemap proxy cannot be called in this context\") to make any off-target use fail loudly.","triggerScenarios":"Calling PluginSourceMapProxy::span_to_source (the primitive behind span_to_snippet) in code compiled for a native target — unit tests of plugin helpers running on the host, or host code that constructs the proxy type instead of using the compiler's real SourceMap.","commonSituations":"Running a plugin crate's #[test] functions on the host toolchain (default `cargo test`) where proxy methods get touched; refactors that move source-map logic out of the wasm-gated plugin entry; CI check builds compiling plugin code for native.","solutions":["Gate all PluginSourceMapProxy method calls behind #[cfg(target_arch = \"wasm32\")].","In host-side tests, build a real swc_common::SourceMap (SourceMap::new + new_source_file) and pass that instead of the proxy.","Run plugin integration tests through swc_plugin_runner with the actual compiled .wasm so host imports link."],"exampleFix":"// before: panics when compiled for host\ndrop(proxy.span_to_source(sp, |src, a, b| src[a..b].to_string()));\n\n// after: only call inside the plugin guest\n#[cfg(target_arch = \"wasm32\")]\nlet snippet = proxy.span_to_source(sp, |src, a, b| src[a..b].to_string());","handlingStrategy":"validation","validationCode":"// fail fast with a clear message before touching any proxy method\nfn assert_wasm_guest_context(api: &'static str) {\n    assert!(\n        cfg!(target_arch = \"wasm32\"),\n        \"{api} on PluginSourceMapProxy is only callable inside a wasm32 plugin guest\"\n    );\n}","typeGuard":"fn in_plugin_guest() -> bool {\n    cfg!(target_arch = \"wasm32\")\n}","tryCatchPattern":null,"preventionTips":["Gate every PluginSourceMapProxy call site with #[cfg(target_arch = \"wasm32\")]","In host-side unit tests, build a real swc_common::SourceMap with new_source_file instead of using the proxy","Run plugin integration tests through swc_plugin_runner with the compiled .wasm so host imports link"],"tags":["wasm-plugin","source-map","target-arch","swc-plugin-proxy","host-interop"],"backgroundTag":"wasm-host-import-unavailable","analyzedSha":"5176682b65416c6b5de6b47379ae1588ea3ecb3f","analyzedAt":"2026-08-17T16:16:52.067Z","contentChangedAt":"2026-08-17T16:16:52.067Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}