gleam-lang/gleam · error

InMemoryFileSystem::into_files called on a clone

Error message

InMemoryFileSystem::into_files called on a clone

What it means

`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.

Solutions

  1. Drop all other clones of the InMemoryFileSystem before calling into_contents.
  2. Refactor so only one owner exists at consumption time (pass &mut / borrow instead of cloning).
  3. If sharing is needed for reading, switch to borrowing methods (e.g. read/exists) and only consume at the very end.
  4. Use Rc::strong_count(&self.files) in debug code to detect stray clones before consuming.

Example fix

// before
let fs_clone = fs.clone();
write_module(&mut fs_clone);
fs.into_contents(); // panics
// after
write_module(&mut fs);
fs.into_contents(); // ok: no clones alive
Defensive patterns

Strategy: validation

Validate before calling

debug_assert_eq!(std::rc::Rc::strong_count(&fs.files), 1, "clones exist; into_contents would panic");

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14). Data as JSON: /api/errors/c7f8ae2ab2318068. Report an issue: GitHub.

Appendix: source

Thrown at compiler-core/src/io/memory.rs:63

impl InMemoryFileSystem {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn reset(&self) {
        self.files.deref().borrow_mut().clear();
    }

    /// Returns the contents of each file, excluding directories.
    ///
    /// # Panics
    ///
    /// Panics if this is not the only reference to the underlying files.
    ///
    pub fn into_contents(self) -> HashMap<Utf8PathBuf, Content> {
        Rc::try_unwrap(self.files)
            .expect("InMemoryFileSystem::into_files called on a clone")
            .into_inner()
            .into_iter()
            .filter_map(|(path, file)| file.into_content().map(|content| (path, content)))
            .collect()
    }

    /// All files currently in the filesystem (directories are not included).
    pub fn files(&self) -> Vec<Utf8PathBuf> {
        self.files
            .borrow()
            .iter()
            .filter(|(_, f)| !f.is_directory())
            .map(|(path, _)| path)
            .cloned()
            .collect()
    }

    #[cfg(test)]

View on GitHub (pinned to 15b07c7830)