Skip to content

Fix crash in oneshot coverage when file is missing or not valid Ruby - #1158

Merged
sferik merged 1 commit into
simplecov-ruby:mainfrom
skryukov:fix-oneshot-line-stub-crash
Mar 28, 2026
Merged

sferik merged 1 commit into
simplecov-ruby:mainfrom
skryukov:fix-oneshot-line-stub-crash

Conversation

@skryukov

Copy link
Copy Markdown
Contributor

This PR fixes Coverage.line_stub raising Errno::ENOENT or SyntaxError during oneshot coverage adaptation.

Errno::ENOENT: In our case, gRPC proto stubs are auto-generated and gitignored. They exist when Ruby loads them (so Coverage tracks them), but may not exist on disk by the time SimpleCov's at_exit hook calls Coverage.line_stub. Since this happens during at_exit, it's hard to orchestrate the process. User-configured filters (add_filter) could be applied earlier in the pipeline, but BlockFilter requires SourceFile objects (which need the file on disk and parsed coverage data), so it can't work at the raw-path stage.

SyntaxError: Also fixes the issue reported in #1154 comments where Coverage.line_stub crashes on non-Ruby files (e.g. YAML) tracked via enable_coverage_for_eval.

The fix: when Coverage.line_stub fails, fall back to building the line array from the oneshot data alone. This preserves coverage results (with degraded nil/0 distinction for uncovered lines) instead of crashing.

@skryukov
skryukov force-pushed the fix-oneshot-line-stub-crash branch 2 times, most recently from 0b34b91 to 8ca1908 Compare March 10, 2026 12:20
@skryukov
skryukov force-pushed the fix-oneshot-line-stub-crash branch from 8ca1908 to be8c7d9 Compare March 10, 2026 12:24

@sferik sferik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for submitting this fix! Looks good to me.

@sferik
sferik merged commit 567e8d5 into simplecov-ruby:main Mar 28, 2026
10 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.

2 participants