Skip to content

tgrep-core: implement Error::source() and add tests for error module - #178

Merged
Shengyu Fu (shengyfu) merged 1 commit into
microsoft:mainfrom
marrionesa:fix/error-source-impl
Oct 6, 2026
Merged

Shengyu Fu (shengyfu) merged 1 commit into
microsoft:mainfrom
marrionesa:fix/error-source-impl

Conversation

@marrionesa

Copy link
Copy Markdown
Contributor

Summary

The public Error type in tgrep-core/src/error.rs used impl std::error::Error for Error {} (empty), so source() returned None for every variant. This loses the inner cause for the Io and Json variants, which wrap std::io::Error and serde_json::Error respectively.

The other three error types in the crate (GenerationError, WorktreeError, MoveStagedFilesError) already implement source() correctly. This brings Error in line with the existing pattern.

Changes

  • Implement Error::source() returning Some(error) for Io and Json, None for the four String variants (IndexNotFound, IndexCorrupted, Regex, Server).
  • Add a #[cfg(test)] mod tests block with 13 unit tests covering:
    • Display output for every variant
    • source() returning the inner error for Io/Json and None for the String variants
    • From<io::Error> and From<serde_json::Error> preserving the inner error
    • Result<T> alias round-trip and Send + Sync bounds (compile-time, since Result<T> crosses thread boundaries in walker.rs and the serve runtime)

Why this is safe

  • source() is not called anywhere in production code (grep -rn "\.source()" tgrep-core/src/ tgrep-cli/src/ returns 0 hits outside tests).
  • The change is additive: source() goes from None (default) to Some(error) for the wrapping variants. Any code that depended on None was silently losing the cause chain — this fixes that.
  • Matches the pattern already established by GenerationError::source() in tgrep-core/src/generations/mod.rs:70-79.

Validation

  • cargo fmt --all -- --check ✅
  • cargo clippy --workspace -- -D warnings ✅
  • cargo test -p tgrep-core --lib error ✅ (17 tests: 13 new + 4 pre-existing in the same module)

The public Error type used 'impl std::error::Error for Error {}' (empty),
so source() returned None for every variant — losing the inner cause for
Io and Json, which wrap std::io::Error and serde_json::Error respectively.

The other three error types in the crate (GenerationError, WorktreeError,
MoveStagedFilesError) already implement source() correctly. This brings
Error in line with the existing pattern.

Adds 13 unit tests covering Display for every variant, source() returning
the inner error for Io/Json and None for the four String variants, From
conversions preserving the inner error, and Result<T> being Send + Sync.
@marrionesa

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The implementation matches existing crate patterns and the tests adequately cover the changed behavior.

Review effort: Balanced
Findings: None

What changed in this PR

Implements error chaining for wrapped I/O and JSON errors and adds comprehensive unit coverage.

Changes:

  • Exposes underlying causes through Error::source().
  • Tests display formatting, conversions, sources, result aliases, and thread-safety bounds.
File Description
tgrep-core/​src/​error.rs Implements error sources and adds unit tests.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@shengyfu
Shengyu Fu (shengyfu) merged commit fd28073 into microsoft:main Oct 6, 2026
6 checks passed
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.

3 participants