Proposed Refactor: Replace once_cell with std::sync::LazyLock

Resolved 💬 2 comments Opened Mar 14, 2026 by htiennv Closed Mar 19, 2026
💡 Likely answer: A maintainer (etraut-openai, contributor) responded on this thread — see the highlighted reply below.

What is the proposal?

I propose refactoring the codebase to remove the external once_cell dependency and replace its usage with the standard library's std::sync::LazyLock.

Why is this change necessary/beneficial?

  • Dependency Reduction: std::sync::LazyLock has been stable since Rust 1.80.0. Since the project is currently using Rust toolchain v1.93.0, once_cell is no longer required for basic lazy initialization of statics.
  • Idiomatic Rust: Leveraging the standard library where possible is a best practice that improves maintainability and ensures long-term compatibility without relying on third-party crates.
  • Scaling & Maintenance: Reducing the total number of dependencies simplifies the [Cargo.lock](cci:7://file:///Users/tiennv/work/ext/codex/codex-rs/Cargo.lock:0:0-0:0) and makes the project cleaner.

How will this be implemented?

  1. Clean up dependencies:
  • Remove once_cell from [workspace.dependencies] in the root [Cargo.toml](cci:7://file:///Users/tiennv/work/ext/codex/codex-rs/Cargo.toml:0:0-0:0).
  • Remove once_cell from member crates: codex-core, codex-shell-command, and codex-git.
  1. Refactor Code:
  • Replace once_cell::sync::Lazy with std::sync::LazyLock in:
  • [shell-command/src/command_safety/windows_dangerous_commands.rs](cci:7://file:///Users/tiennv/work/ext/codex/codex-rs/shell-command/src/command_safety/windows_dangerous_commands.rs:0:0-0:0)
  • [utils/git/src/apply.rs](cci:7://file:///Users/tiennv/work/ext/codex/codex-rs/utils/git/src/apply.rs:0:0-0:0)
  • [core/src/test_support.rs](cci:7://file:///Users/tiennv/work/ext/codex/codex-rs/core/src/test_support.rs:0:0-0:0)
  • [core/src/auth/storage.rs](cci:7://file:///Users/tiennv/work/ext/codex/codex-rs/core/src/auth/storage.rs:0:0-0:0)
  1. Verification:
  • Ran cargo test for affected crates.
  • Ran cargo fmt to maintain code style.

Impact

  • Breaking Changes: None. This is a purely internal refactoring.
  • Affected Surfaces: All internal crates that rely on lazy-initialized constants.

View original on GitHub ↗

2 Comments

Br1an67 · 5 months ago

I've analyzed the codebase and can confirm that the migration from once_cell::sync::Lazy to std::sync::LazyLock is straightforward. Here's the complete scope:

Files requiring changes

| File | Current usage | Replacement |
|------|--------------|-------------|
| codex-rs/core/src/auth/storage.rs:26 | use once_cell::sync::Lazy; | use std::sync::LazyLock; |
| codex-rs/core/src/test_support.rs:14 | use once_cell::sync::Lazy; | use std::sync::LazyLock; |
| codex-rs/utils/git/src/apply.rs:9 | use once_cell::sync::Lazy; | use std::sync::LazyLock; |
| codex-rs/shell-command/src/command_safety/windows_dangerous_commands.rs:3 | use once_cell::sync::Lazy; | use std::sync::LazyLock; |

Cargo.toml changes

  1. Remove once_cell = "1.20.2" from codex-rs/Cargo.toml workspace dependencies (line 221)
  2. Remove once_cell = { workspace = true } from:
  • codex-rs/core/Cargo.toml (line 75)
  • codex-rs/utils/git/Cargo.toml (line 12)
  • codex-rs/shell-command/Cargo.toml (line 14)

Notes

  • All usages follow the same Lazy::new(|| { ... }) → LazyLock::new(|| { ... }) pattern — the API is identical.
  • The project uses Rust 1.93.0 and std::sync::LazyLock has been stable since Rust 1.80.0, so no MSRV concerns.
  • once_cell will still appear as a transitive dependency (used by ~40 other crates in the workspace), so it won't be removed from Cargo.lock, but it removes the direct dependency.

Happy to submit a PR if the team is interested — this is a zero-risk, mechanical refactor.

etraut-openai contributor · 5 months ago

Thanks for the suggestion, but I think we'll pass.