gleam-lang/gleam · error

channel buffer write

Error message

channel buffer write

What it means

`InMemoryFileSystem::write_bytes` writes content into a fresh InMemoryFile through std::io::Write and expects success unconditionally ('channel buffer write'). The write target is an in-memory buffer, so failure indicates the underlying channel/buffer primitive is broken — an internal invariant, only reachable when the in-memory file abstraction's writer fails.

Solutions

  1. Inspect InMemoryFile's Write impl: it should never fail for in-memory buffers; restore an unbounded Vec/Cursor-based buffer.
  2. If a channel is used, ensure the receiver stays alive and capacity is sufficient.
  3. Propagate the io::Error with `?` instead of expect so failures are diagnosable.

Example fix

// before
_ = io::Write::write(&mut file, content).expect("channel buffer write");
// after
io::Write::write(&mut file, content)?;
Defensive patterns

Strategy: validation

Validate before calling

// ensure the in-memory writer cannot fail: InMemoryFile must wrap an unbounded buffer (Vec/Cursor), not a bounded channel
assert!(matches!(file_kind, FileKind::UnboundedBuffer));

Prevention

When it happens

Trigger: Calling write_bytes, or its callers copy / write / to_in_memory_filesystem, when the InMemoryFile's internal buffer writer (implemented over a channel-like buffer) fails — practically a broken internal buffer, e.g. after a refactor replacing the buffer with a bounded channel.

Common situations: Gleam compiler runs writing modules to an in-memory FS (tests, LSP, export); would surface after changes to InMemoryFile internals rather than from user input.

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/f169a212a3b6b1c5. Report an issue: GitHub.

Appendix: source

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

                err: None,
            });
        }
        let _ = files.remove(path);
        Ok(())
    }

    fn write(&self, path: &Utf8Path, content: &str) -> Result<(), Error> {
        self.write_bytes(path, content.as_bytes())
    }

    fn write_bytes(&self, path: &Utf8Path, content: &[u8]) -> Result<(), Error> {
        // Ensure directories exist
        if let Some(parent) = path.parent() {
            self.mkdir(parent)?;
        }

        let mut file = InMemoryFile::default();
        _ = io::Write::write(&mut file, content).expect("channel buffer write");
        _ = self
            .files
            .deref()
            .borrow_mut()
            .insert(path.to_path_buf(), file);
        Ok(())
    }

    fn exists(&self, path: &Utf8Path) -> bool {
        self.files.deref().borrow().contains_key(path)
    }
}

impl FileSystemReader for InMemoryFileSystem {
    fn canonicalise(&self, path: &Utf8Path) -> Result<Utf8PathBuf, Error> {
        Ok(path.to_path_buf())
    }

View on GitHub (pinned to 15b07c7830)