{"record":{"id":"11c7be67cd0d1fd5","repo":"gleam-lang/gleam","slug":"inmemoryfile-into-content-called-with-multiple-references","errorCode":null,"errorMessage":"InMemoryFile::into_content called with multiple references","messagePattern":"InMemoryFile::into_content called with multiple references","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/io/memory.rs","lineNumber":402,"sourceCode":"            ..Self::default()\n        }\n    }\n\n    /// Checks whether this is a directory's entry.\n    pub fn is_directory(&self) -> bool {\n        matches!(self.node, InMemoryFileNode::Directory)\n    }\n\n    /// Returns this file's contents if this is not a directory.\n    ///\n    /// # Panics\n    ///\n    /// Panics if this is not the only reference to the underlying files.\n    ///\n    pub fn into_content(self) -> Option<Content> {\n        let buffer = self.node.into_file_buffer()?;\n        let contents = Rc::try_unwrap(buffer)\n            .expect(\"InMemoryFile::into_content called with multiple references\")\n            .into_inner();\n\n        // All null bytes are usually from when a binary file is empty, and\n        // aren't particularly useful as text, so we treat them as binary.\n        if contents.iter().all(|byte| *byte == 0) {\n            return Some(Content::Binary(contents));\n        }\n\n        match String::from_utf8(contents) {\n            Ok(s) => Some(Content::Text(s)),\n            Err(e) => Some(Content::Binary(e.into_bytes())),\n        }\n    }\n}\n\nimpl Default for InMemoryFile {\n    fn default() -> Self {\n        Self {","sourceCodeStart":384,"sourceCodeEnd":420,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/io/memory.rs#L384-L420","documentation":"`InMemoryFile::into_content` requires sole ownership of the file's shared buffer (an Rc). Rc::try_unwrap fails when clones of the buffer/file still exist, panicking with 'called with multiple references'. Like error 134, this is a consumption-time ownership violation.","triggerScenarios":"Calling `into_content()` (e.g. via InMemoryFileSystem::into_contents) while any clone of the underlying file buffer is alive — clones taken for reads, stored handles, or files cloned out of the FS before consumption.","commonSituations":"Emitting compiled output from the in-memory filesystem after code elsewhere cloned files for inspection or caching.","solutions":["Ensure no clones of the InMemoryFile/buffer exist when calling into_content.","Take read-only snapshots via borrowing methods instead of cloning the file.","Consume files strictly after all readers have finished (structure code so handles are dropped).","Debug with Rc::strong_count(&buffer) to find lingering clones."],"exampleFix":"// before\nlet handle = fs.get(&path).cloned();\nfs.into_contents(); // panics inside into_content\n// after\ndrop(handle);\nfs.into_contents(); // sole reference","handlingStrategy":"validation","validationCode":"debug_assert_eq!(std::rc::Rc::strong_count(&buffer), 1, \"multiple references; into_content would panic\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not clone InMemoryFile handles; re-fetch or borrow for reads.","Consume files only after all readers are done.","Check Rc::strong_count in debug builds 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"}