-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[codex] Add comprehensive root formatting check #25683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e5655c7
Add comprehensive formatting check recipe
anp-oai 278e87e
codex: address PR review feedback (#25683)
anp-oai 000561c
codex: fix CI failure on PR #25683
anp-oai 6999448
codex: address PR review feedback (#25683)
anp-oai 1b3f0c3
codex: address PR review feedback (#25683)
anp-oai 9616ce1
codex: address PR review feedback (#25683)
anp-oai 83919c4
codex: address PR review feedback (#25683)
anp-oai 5afb526
codex: address PR review feedback (#25683)
anp-oai d0ef086
codex: address PR review feedback (#25683)
anp-oai 562863c
codex: address PR review feedback (#25683)
anp-oai 365ac0e
codex: address PR review feedback (#25683)
anp-oai cba4ad9
codex: address PR review feedback (#25683)
anp-oai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| #!/usr/bin/env python3 | ||
| """Format repository sources or check that they are already formatted.""" | ||
|
|
||
| import argparse | ||
| import shlex | ||
| import subprocess | ||
| import sys | ||
| from concurrent.futures import ThreadPoolExecutor, as_completed | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[1] | ||
| CODEX_RS_ROOT = REPO_ROOT / "codex-rs" | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class Command: | ||
| args: tuple[str, ...] | ||
| cwd: Path = REPO_ROOT | ||
| discard_stderr: bool = False | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class FormatterGroup: | ||
| name: str | ||
| commands: tuple[Command, ...] | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class FormatterResult: | ||
| name: str | ||
| output: str | ||
| returncode: int | ||
|
|
||
|
|
||
| def formatter_groups(*, check: bool) -> tuple[FormatterGroup, ...]: | ||
| just_args = ["just", "--unstable", "--fmt"] | ||
| cargo_args = ["cargo", "fmt", "--", "--config", "imports_granularity=Item"] | ||
| # Use an unpinned overlay so Ruff is available without syncing project | ||
| # dependencies. Each `--project` still retains its local configuration context. | ||
| sdk_uv_run_args = [ | ||
| "uv", | ||
| "run", | ||
| "--frozen", | ||
| "--project", | ||
| "sdk/python", | ||
| "--no-sync", | ||
| "--with", | ||
| "ruff", | ||
| ] | ||
| scripts_uv_run_args = [ | ||
| "uv", | ||
| "run", | ||
| "--frozen", | ||
| "--project", | ||
| "scripts", | ||
| "--no-sync", | ||
| "--with", | ||
| "ruff", | ||
| ] | ||
| sdk_format_args = [ | ||
| *sdk_uv_run_args, | ||
| "ruff", | ||
| "format", | ||
| ] | ||
| scripts_format_args = [ | ||
| *scripts_uv_run_args, | ||
| "ruff", | ||
| "format", | ||
| ] | ||
|
|
||
| if check: | ||
| just_args.append("--check") | ||
| cargo_args.append("--check") | ||
| sdk_format_args.append("--check") | ||
| scripts_format_args.append("--check") | ||
| # `ruff check --diff` reports lint-driven rewrites without changing files. | ||
| # It is the check-mode counterpart of `--fix --fix-only`, not a full lint gate. | ||
| sdk_lint_args = ["ruff", "check", "--diff"] | ||
| else: | ||
| # Ruff's lint fixer and formatter are separate passes: the first applies | ||
| # fixable lint rewrites, while the second formats source layout. | ||
| sdk_lint_args = ["ruff", "check", "--fix", "--fix-only"] | ||
|
|
||
| return ( | ||
| FormatterGroup("Just", (Command(tuple(just_args)),)), | ||
| FormatterGroup( | ||
| "Rust", | ||
| # Stable rustfmt repeats a nightly-only `imports_granularity` warning | ||
| # for each crate, so suppress that expected stderr noise. | ||
| (Command(tuple(cargo_args), CODEX_RS_ROOT, discard_stderr=True),), | ||
| ), | ||
| FormatterGroup( | ||
| "Python SDK", | ||
| ( | ||
| Command( | ||
| ( | ||
| *sdk_uv_run_args, | ||
| *sdk_lint_args, | ||
| "sdk/python", | ||
| ) | ||
| ), | ||
| Command((*sdk_format_args, "sdk/python")), | ||
| ), | ||
| ), | ||
| FormatterGroup( | ||
| "Python scripts", | ||
| ( | ||
| # The SDK and internal scripts intentionally use separate project | ||
| # roots so uv and Ruff retain each project's configuration context. | ||
| Command((*scripts_format_args, "scripts")), | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def run_formatter_group(group: FormatterGroup) -> FormatterResult: | ||
| """Run one formatter group sequentially and return its buffered output.""" | ||
| output: list[str] = [] | ||
| for command in group.commands: | ||
| output.append(f"$ {shlex.join(command.args)}\n") | ||
| try: | ||
| process = subprocess.run( | ||
| command.args, | ||
| cwd=command.cwd, | ||
| stdout=subprocess.PIPE, | ||
| stderr=subprocess.DEVNULL | ||
| if command.discard_stderr | ||
| else subprocess.STDOUT, | ||
| text=True, | ||
| check=False, | ||
| ) | ||
| except OSError as error: | ||
| output.append(f"{error}\n") | ||
| return FormatterResult(group.name, "".join(output), 1) | ||
|
|
||
| output.append(process.stdout) | ||
| if process.stdout and not process.stdout.endswith("\n"): | ||
| output.append("\n") | ||
| if process.returncode != 0: | ||
| return FormatterResult(group.name, "".join(output), process.returncode) | ||
|
|
||
| return FormatterResult(group.name, "".join(output), 0) | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument( | ||
| "--check", | ||
| action="store_true", | ||
| help="check formatting without modifying files", | ||
| ) | ||
| args = parser.parse_args() | ||
| groups = formatter_groups(check=args.check) | ||
|
|
||
| failures: list[str] = [] | ||
| with ThreadPoolExecutor(max_workers=len(groups)) as executor: | ||
| futures = {} | ||
| for group in groups: | ||
| print(f"Starting {group.name} formatter...", flush=True) | ||
| futures[executor.submit(run_formatter_group, group)] = group.name | ||
| for future in as_completed(futures): | ||
| result = future.result() | ||
| print(f"==> {result.name} formatter finished") | ||
| print(result.output, end="") | ||
| if result.returncode != 0: | ||
| failures.append(result.name) | ||
|
|
||
| if failures: | ||
| print(f"Formatting failed: {', '.join(failures)}", file=sys.stderr) | ||
| return 1 | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.