Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Oct 11, 2025

Fixes #issue_number

Problem

When attempting to seek to a negative position in a FileStream (e.g., fs.Seek(-1, SeekOrigin.Begin)), users encountered a confusing and unhelpful error message:

System.IO.IOException: The parameter is incorrect. : 'C:\path\to\file.txt'

This generic system error message provided no indication of what was actually wrong, forcing developers to spend time debugging to discover they were attempting to seek before the beginning of the stream.

Solution

Changed the error message to be clear and descriptive:

System.IO.IOException: An attempt was made to move the position before the beginning of the stream.

The fix replaces the call to FileStreamHelpers.ThrowInvalidArgument(_fileHandle) with a direct throw of IOException using the existing SR.IO_SeekBeforeBegin localized resource string. This makes FileStream's error messaging consistent with other stream implementations like MemoryStream, UnmanagedMemoryStream, and ReadOnlyMemoryStream, which already use this same message.

Changes

  • Modified OSFileStreamStrategy.Seek to throw IOException with the IO_SeekBeforeBegin message when the calculated position is negative

Testing

  • ✅ All 8,716 FileStream tests pass with no failures
  • ✅ No breaking changes - exception type and behavior unchanged
  • ✅ Manual testing confirms the new message displays correctly

This improvement helps developers quickly identify and fix issues with negative seek operations, significantly reducing debugging time.

Fixes #112112

Original prompt

This section details on the original issue you should resolve

<issue_title>OSFileStreamStrategy Seek Error is Confusing and not helpful.</issue_title>
<issue_description>### Description

Hello, Apologies if I'm not doing anything correct here but let's get started!

I'm working on a complex system that processes files into more optimal formats for compression and storage, a bug in this code caused it to try to seek to a negative offset position within: OSFileStreamStrategy.Seek

When this occurs, FileStreamHelpers.ThrowInvalidArgument(_fileHandle); occurs.

Which throws a really unhelpful message: The parameter is incorrect. : <file path>.

Image

This caused me to debug around thinking that my file stream was wrong, or that my input file was a corrupt image etc. When in actuality the image is fine, some code elsewhere was just trying to seek negatively.

Reproduction Steps

Attempt to seek a `OSFileStreamStrategy with a negative resultant value.

Seek(-1, SeekOrigin.Begin); should achieve this.

Expected behavior

A better exception message, would be very helpful for developers to more quickly comprehend what's going on.

Actual behavior

Explained in description

Regression?

I am not sure, there is a comment in the area: " // keep throwing the same exception we did when seek was causing actual offset change
" that implies there was a decision point at some point in time.

Known Workarounds

No response

Configuration

.NET 9, Windows 10, x64

Other information

No response</issue_description>

<agent_instructions>Please help improve the error message, and if any member names need to be changed for clarity, please fix those, too</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@adamsitnik Hello @ProbablePrime

@KalleOlaviNiemitalo is right, we can't change the exception type (it would be a breaking change), but we can change the exception message.

I am going to mark it as help-wanted. Please feel free to send a PR with a fix.

Thanks!

Fixes #112112


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Fix confusing seek error message in OSFileStreamStrategy Improve error message when seeking to negative position in FileStream Oct 11, 2025
@Copilot Copilot AI requested a review from stephentoub October 11, 2025 17:42
Copilot finished work on behalf of stephentoub October 11, 2025 17:42
@stephentoub stephentoub marked this pull request as ready for review October 11, 2025 20:17
@Copilot Copilot AI review requested due to automatic review settings October 11, 2025 20:17
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR improves the error message when attempting to seek to a negative position in a FileStream by replacing a generic system error with a clear, descriptive message that helps developers quickly identify the issue.

  • Replaces unhelpful "The parameter is incorrect" message with "An attempt was made to move the position before the beginning of the stream"
  • Maintains backward compatibility by keeping the same IOException type
  • Adds comprehensive test coverage to verify the improved error message across all SeekOrigin modes

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/libraries/System.Private.CoreLib/src/System/IO/Strategies/OSFileStreamStrategy.cs Updates error handling to throw IOException with descriptive message using existing localized resource
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileStream/Seek.cs Adds test method to verify the improved error message for negative seek operations

Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

@Copilot Copilot AI requested a review from stephentoub October 11, 2025 20:42
Copilot finished work on behalf of stephentoub October 11, 2025 20:42
@jkotas jkotas merged commit 0b9f734 into main Oct 13, 2025
140 of 145 checks passed
@jkotas jkotas deleted the copilot/fix-osfilestreamstrategy-seek-error branch October 13, 2025 05:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OSFileStreamStrategy Seek Error is Confusing and not helpful.

4 participants