Skip to content

Fix IndexOutOfRangeException in InjectSnippetsIntoFile at end of file#315

Merged
rgregg-msft merged 3 commits intoOneDrive:masterfrom
gavinbarron:fix/inject-snippets-index-out-of-range
Mar 16, 2026
Merged

Fix IndexOutOfRangeException in InjectSnippetsIntoFile at end of file#315
rgregg-msft merged 3 commits intoOneDrive:masterfrom
gavinbarron:fix/inject-snippets-index-out-of-range

Conversation

@gavinbarron
Copy link
Copy Markdown
Contributor

Summary

Fixes a crash in InjectSnippetsIntoFile when snippet injection occurs at or near the end of a documentation file.

Problem

After Splice removes the old snippet tab section, the collection shrinks. The code then unconditionally accesses updatedFileContents.ElementAt(insertionLine + 1) without checking bounds, causing an ArgumentOutOfRangeException.

Changes

  • Bug fix: Added bounds check before ElementAt(insertionLine + 1) in the InsertSnippets block
  • Refactor: Extracted the core snippet injection logic into a testable ProcessSnippetInjection internal method, keeping file I/O in the original wrapper
  • Tests: Added ApiDoctor.Console.UnitTests project with 12 NUnit tests covering FileSplicer and snippet injection scenarios, including the end-of-file boundary case that triggered the crash

Testing

  • All 74 tests pass (12 new + 62 existing)
  • Integration tested against the real microsoft-graph-docs repo (18,467 files) with generate-snippets command - no crashes

Fixes #314

Add bounds check before accessing ElementAt(insertionLine + 1) after
Splice shrinks the collection. When snippet injection occurs at or near
the end of a file, the index would exceed collection bounds.

Also extracts the core snippet injection logic into a testable internal
method (ProcessSnippetInjection) and adds a new test project
(ApiDoctor.Console.UnitTests) with 12 tests covering FileSplicer and
snippet injection scenarios including the end-of-file boundary case.

Fixes OneDrive#314

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread ApiDoctor.Console/Program.cs Outdated
Comment thread ApiDoctor.Console/ApiDoctor.ConsoleApp.csproj Outdated
…alsVisibleTo

- Make ProcessSnippetInjection return type nullable (IEnumerable<string>?)
  to match the null return path
- Remove redundant <InternalsVisibleTo> from csproj since it's already
  declared in AssemblyInfo.cs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@gavinbarron
Copy link
Copy Markdown
Contributor Author

@MIchaelMainer good points, addressed both

@MIchaelMainer
Copy link
Copy Markdown
Collaborator

Blocked by required check that may no longer be running.

@rgregg-msft rgregg-msft merged commit 496674c into OneDrive:master Mar 16, 2026
3 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.

IndexOutOfRangeException in InjectSnippetsIntoFile when snippet insertion is at end of file

3 participants