From c67908ba17234ca10201caa70797a90b738613aa Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Wed, 5 Nov 2025 17:26:45 -0800 Subject: [PATCH] style: reformat imports granularity to module Choosing module granularity over item granularity is based on a similar choice made in the standard library to balance readabilty, with easy to read diffs. This saves 2098 lines or about 33% of the lines spent writing imports. This is beneficial to both human readers who have to scroll past large import blocks when reading a file as well as codex which shouldn't have to add as many tokens to its context. Manual changes to rustfmt.toml, rust-ci.yml, justfile, docs/contributing and docs/install. The actual format commit is intentionally in a second commit to make it easier to rebase this PR as main moves (drop the next commit and re-run cargo +nightly fmt). Added cargo nightly to the ci and justfile. In theory this is unstable, but in real world use this tends to hit a problems perhaps once every 6 months. The fix is usually fairly easy (pin a version briefly), and the problems tend to be fixed pretty quickly. --- .github/workflows/rust-ci.yml | 4 ++-- codex-rs/justfile | 3 ++- codex-rs/rustfmt.toml | 14 +++++++++++--- docs/contributing.md | 2 +- docs/install.md | 3 ++- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/.github/workflows/rust-ci.yml b/.github/workflows/rust-ci.yml index 103e8cf6bd..aca2094ba5 100644 --- a/.github/workflows/rust-ci.yml +++ b/.github/workflows/rust-ci.yml @@ -57,11 +57,11 @@ jobs: working-directory: codex-rs steps: - uses: actions/checkout@v5 - - uses: dtolnay/rust-toolchain@1.90 + - uses: dtolnay/rust-toolchain@nightly with: components: rustfmt - name: cargo fmt - run: cargo fmt -- --config imports_granularity=Item --check + run: cargo +nightly fmt --check # nightly for unstable options in rustfmt.toml - name: Verify codegen for mcp-types run: ./mcp-types/check_lib_rs.py diff --git a/codex-rs/justfile b/codex-rs/justfile index 9ddc4a37aa..cf4c66439a 100644 --- a/codex-rs/justfile +++ b/codex-rs/justfile @@ -23,7 +23,8 @@ file-search *args: # format code fmt: - cargo fmt -- --config imports_granularity=Item + # Use nightly to enable unstable options in rustfmt.toml + cargo +nightly fmt fix *args: cargo clippy --fix --all-features --tests --allow-dirty "$@" diff --git a/codex-rs/rustfmt.toml b/codex-rs/rustfmt.toml index f9686f9732..87868fa377 100644 --- a/codex-rs/rustfmt.toml +++ b/codex-rs/rustfmt.toml @@ -1,4 +1,12 @@ edition = "2024" -# The warnings caused by this setting can be ignored. -# See https://github.com/openai/openai/pull/298039 for details. -imports_granularity = "Item" + +# The following settings are unstable. Generally, to use these settings, you can either use a +# nightly toolchain, or add each args to the cargo fmt command. If using stable, these will emit a +# warning when running cargo fmt. + +# Grouping imports by module is a reasonable compromise between readability and conciseness that +# saves about 2000 lines (33%) compared to grouping by item, while still keeping related imports +# together in a way that works well with diffs and code navigation. +group_imports = "StdExternalCrate" +imports_granularity = "Module" +reorder_imports = true diff --git a/docs/contributing.md b/docs/contributing.md index 596afe3b2e..910b8bc404 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -24,7 +24,7 @@ If you want to add a new feature or change the behavior of an existing one, plea ### Opening a pull request - Fill in the PR template (or include similar information) - **What? Why? How?** -- Run **all** checks locally (`cargo test && cargo clippy --tests && cargo fmt -- --config imports_granularity=Item`). CI failures that could have been caught locally slow down the process. +- Run **all** checks locally (`cargo test && cargo clippy --tests && cargo +nightly fmt`). CI failures that could have been caught locally slow down the process. - Make sure your branch is up-to-date with `main` and that you have resolved merge conflicts. - Mark the PR as **Ready for review** only when you believe it is in a merge-able state. diff --git a/docs/install.md b/docs/install.md index 724a524e3b..e83a840c53 100644 --- a/docs/install.md +++ b/docs/install.md @@ -24,6 +24,7 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y source "$HOME/.cargo/env" rustup component add rustfmt rustup component add clippy +rustup install nightly # For rustfmt with unstable options. # Build Codex. cargo build @@ -32,7 +33,7 @@ cargo build cargo run --bin codex -- "explain this codebase to me" # After making changes, ensure the code is clean. -cargo fmt -- --config imports_granularity=Item +cargo +nightly fmt cargo clippy --tests # Run the tests.