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::LazyLockhas been stable since Rust 1.80.0. Since the project is currently using Rust toolchain v1.93.0,once_cellis 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?
- Clean up dependencies:
- Remove
once_cellfrom[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_cellfrom member crates:codex-core,codex-shell-command, andcodex-git.
- Refactor Code:
- Replace
once_cell::sync::Lazywithstd::sync::LazyLockin: - [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)
- Verification:
- Ran
cargo testfor affected crates. - Ran
cargo fmtto maintain code style.
Impact
- Breaking Changes: None. This is a purely internal refactoring.
- Affected Surfaces: All internal crates that rely on lazy-initialized constants.
2 Comments
I've analyzed the codebase and can confirm that the migration from
once_cell::sync::Lazytostd::sync::LazyLockis 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
once_cell = "1.20.2"fromcodex-rs/Cargo.tomlworkspace dependencies (line 221)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
Lazy::new(|| { ... })→LazyLock::new(|| { ... })pattern — the API is identical.std::sync::LazyLockhas been stable since Rust 1.80.0, so no MSRV concerns.once_cellwill still appear as a transitive dependency (used by ~40 other crates in the workspace), so it won't be removed fromCargo.lock, but it removes the direct dependency.Happy to submit a PR if the team is interested — this is a zero-risk, mechanical refactor.
Thanks for the suggestion, but I think we'll pass.