{"record":{"id":"c7f8ae2ab2318068","repo":"gleam-lang/gleam","slug":"inmemoryfilesystem-into-files-called-on-a-clone","errorCode":null,"errorMessage":"InMemoryFileSystem::into_files called on a clone","messagePattern":"InMemoryFileSystem::into_files called on a clone","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/io/memory.rs","lineNumber":63,"sourceCode":"\nimpl InMemoryFileSystem {\n    pub fn new() -> Self {\n        Self::default()\n    }\n\n    pub fn reset(&self) {\n        self.files.deref().borrow_mut().clear();\n    }\n\n    /// Returns the contents of each file, excluding directories.\n    ///\n    /// # Panics\n    ///\n    /// Panics if this is not the only reference to the underlying files.\n    ///\n    pub fn into_contents(self) -> HashMap<Utf8PathBuf, Content> {\n        Rc::try_unwrap(self.files)\n            .expect(\"InMemoryFileSystem::into_files called on a clone\")\n            .into_inner()\n            .into_iter()\n            .filter_map(|(path, file)| file.into_content().map(|content| (path, content)))\n            .collect()\n    }\n\n    /// All files currently in the filesystem (directories are not included).\n    pub fn files(&self) -> Vec<Utf8PathBuf> {\n        self.files\n            .borrow()\n            .iter()\n            .filter(|(_, f)| !f.is_directory())\n            .map(|(path, _)| path)\n            .cloned()\n            .collect()\n    }\n\n    #[cfg(test)]","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/io/memory.rs#L45-L81","documentation":"`InMemoryFileSystem::into_contents` consumes the filesystem and requires sole ownership of the underlying files map (an Rc). If any clone of the InMemoryFileSystem still exists, Rc::try_unwrap fails and this panic fires. It is a use-after-clone contract violation, not a filesystem error.","triggerScenarios":"Calling `into_contents()` on an InMemoryFileSystem that has been cloned (Rc strong count > 1), e.g. keeping a copy in a variable, storing it in a struct, or cloning it into a closure/thread before consuming.","commonSituations":"Compile pipelines that clone the in-memory FS for warnings/teardown helpers, then call into_contents at the end to emit files; test harnesses that clone the FS for assertions.","solutions":["Drop all other clones of the InMemoryFileSystem before calling into_contents.","Refactor so only one owner exists at consumption time (pass &mut / borrow instead of cloning).","If sharing is needed for reading, switch to borrowing methods (e.g. read/exists) and only consume at the very end.","Use Rc::strong_count(&self.files) in debug code to detect stray clones before consuming."],"exampleFix":"// before\nlet fs_clone = fs.clone();\nwrite_module(&mut fs_clone);\nfs.into_contents(); // panics\n// after\nwrite_module(&mut fs);\nfs.into_contents(); // ok: no clones alive","handlingStrategy":"validation","validationCode":"debug_assert_eq!(std::rc::Rc::strong_count(&fs.files), 1, \"clones exist; into_contents would panic\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never clone InMemoryFileSystem if you plan to call into_contents.","Borrow (&fs) for read-only phases; consume (into_contents) only at the end.","Audit for clones stored in structs/closures before consuming."],"tags":["rust","panic","ownership","rc"],"backgroundTag":"internal-invariant-violation","analyzedSha":"15b07c783065c92bb016b1b74ed0995a5259850f","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}