{"record":{"id":"6848c4e420f47c7f","repo":"swc-project/swc","slug":"swc-loader-only-accepts-path-got-name","errorCode":null,"errorMessage":"swc-loader only accepts path. Got `{name}`","messagePattern":"swc-loader only accepts path\\. Got `(.+?)`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/swc_node_bundler/src/loaders/swc.rs","lineNumber":136,"sourceCode":"                    Default::default(),\n                    None,\n                    &mut Vec::new(),\n                )\n                .unwrap();\n                return Ok(ModuleData {\n                    fm,\n                    module,\n                    helpers: Default::default(),\n                });\n            }\n        }\n\n        let fm = self\n            .compiler\n            .cm\n            .load_file(match name {\n                FileName::Real(v) => v,\n                _ => bail!(\"swc-loader only accepts path. Got `{name}`\"),\n            })\n            .with_context(|| format!(\"failed to load file `{name}`\"))?;\n\n        if let FileName::Real(path) = name {\n            if let Some(ext) = path.extension() {\n                if ext == \"json\" {\n                    let module = load_json_as_module(&fm)\n                        .with_context(|| format!(\"failed to load json file at {}\", fm.name))?;\n                    return Ok(ModuleData {\n                        fm,\n                        module,\n                        helpers: Default::default(),\n                    });\n                }\n            }\n        }\n\n        #[cfg(debug_assertions)]","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/swc-project/swc/blob/5176682b65416c6b5de6b47379ae1588ea3ecb3f/crates/swc_node_bundler/src/loaders/swc.rs#L118-L154","documentation":"In the swc_node_bundler's SwcLoader::load_file, the compiler's SourceMap is asked to load the file from disk, which requires an OS path. The function matches the incoming FileName and bails for any variant other than FileName::Real (Custom, Anon, Inline, etc.). The bundler's loader pipeline only supports inputs backed by a physical file path.","triggerScenarios":"Passing a FileName::Custom or FileName::Anon entry to the swc loader, e.g. when feeding virtually-generated or transformed modules into the bundler, or when a preceding pipeline step replaced the original Real filename with a synthetic identifier.","commonSituations":"Building custom bundling pipelines where modules come from memory (plugins, virtual entry points, playgrounds); integrating swc_node_bundler with loaders that tag files with custom names; after refactoring a loader chain the filename type silently changed.","solutions":["Only hand FileName::Real(path) entries to the swc loader; write virtual content to a real temp/cache file first if needed","Trace back the loader pipeline step that produced the non-Real FileName and keep the original path attached","For in-memory modules, use a loader designed for them (e.g. json path above is still Real-based; the whole API is path-centric)"],"exampleFix":"// before\nloader.load_file(FileName::Custom(\"virtual-entry.ts\".into()))?; // bails\n\n// after\nlet path = std::env::temp_dir().join(\"virtual-entry.ts\");\nstd::fs::write(&path, source)?;\nloader.load_file(FileName::Real(path))?;","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"use swc_common::FileName;\nfn asRealPath(f: &FileName) -> Option<&std::path::Path> {\n    match f {\n        FileName::Real(p) => Some(p),\n        _ => None, // SwcLoader::load_file bails for Custom/Anon/etc.\n    }\n}","tryCatchPattern":"match loader.load_file(name.clone()) {\n    Ok(data) => Ok(data),\n    Err(e) if e.to_string().contains(\"swc-loader only accepts path\") => {\n        let p = materialize(&name)?; // write virtual content to disk\n        loader.load_file(FileName::Real(p))\n    }\n    Err(e) => Err(e),\n}","preventionTips":["Only feed FileName::Real into swc_node_bundler loaders; materialize virtual modules to temp files","Keep filename variants stable across your transform pipeline; don't re-tag Real as Custom","Add a debug assertion in your pipeline that entries entering loaders are FileName::Real"],"tags":["swc-loader","node-bundler","filename-type","virtual-fs","module-loading"],"backgroundTag":"unsupported-filename-type","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"}