Skip to content

fix(codegen): replace unsafe unwraps with error context and robust ma…#80

Open
goodbsw wants to merge 2 commits into
kube-rs:mainfrom
goodbsw:fix/codegen-panic-with-context
Open

fix(codegen): replace unsafe unwraps with error context and robust ma…#80
goodbsw wants to merge 2 commits into
kube-rs:mainfrom
goodbsw:fix/codegen-panic-with-context

Conversation

@goodbsw

@goodbsw goodbsw commented Jun 10, 2026

Copy link
Copy Markdown

Description

This PR addresses latent panic risks within the k8s-pb-codegen binary by refactoring aggressive .unwrap() calls into idiomatic Rust error-handling patterns using anyhow::Context.

As an infrastructure engineering component, the code generator should gracefully bubble up errors rather than inducing unexpected panics during build time, especially within multi-cluster CI/CD pipelines.

Key Changes

  • Eliminated unwrap() in Module Tree Traversal: Refactored the while !parts.is_empty() loop to use an atomic while let Some(module) = parts.pop() pattern, preventing potential index or bounds panics structurally.
  • Robust Path & Directory Handling: Added explicit .with_context() tracking to all dst.parent() resolutions and std::fs::create_dir_all calls to provide granular diagnosis if I/O blockages occur.
  • Safer Trait Generation Pipeline: Updated signature of append_trait_def and append_trait_impl to return anyhow::Result<()>. Replaced writeln!(...).unwrap() with ? propagation to catch disk capacity or broken pipe anomalies gracefully.
  • Cleaned Up prost_build Propagation: Aligned the Protobuf compilation phase with consistent anyhow context mapping.

Diagnostics Verified

Successfully validated workspace compilation against all targets:

cargo check --workspace --all-targets
# Output: Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.39s

goodbsw added 2 commits June 10, 2026 11:18
…tching

Signed-off-by: Seungweon Baek <goodbsw@gmail.com>
Signed-off-by: Seungweon Baek <goodbsw@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant