Skip to content

Optimize default file parameters in PipeTarget.ToFile(...) - #333

Merged
Tyrrrz merged 8 commits into
Tyrrrz:primefrom
yueyinqiu:331_2
Jun 3, 2026
Merged

Tyrrrz merged 8 commits into
Tyrrrz:primefrom
yueyinqiu:331_2

Conversation

@yueyinqiu

@yueyinqiu yueyinqiu commented May 31, 2026 •

Copy link
Copy Markdown
Contributor

Closes #331

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.

Pull request overview

This PR updates CliWrap’s piping helpers to better control stream lifetimes and to make file piping friendlier for real-time log tailing scenarios (issue #331).

Changes:

  • Added disposeStream parameter to PipeTarget.ToStream(...) and PipeSource.FromStream(...) to optionally take ownership and dispose the provided stream.
  • Updated PipeTarget.ToFile(...) to open files with FileShare.Read (and route copying through the auto-flushing copy helper).

Reviewed changes

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

File Description
CliWrap/PipeTarget.cs Adds disposeStream overloads for ToStream and changes ToFile to allow shared reads while writing.
CliWrap/PipeSource.cs Adds disposeStream overloads for FromStream to optionally dispose the input stream after piping.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CliWrap/PipeTarget.cs Resolved
Comment thread CliWrap/PipeTarget.cs Resolved
Comment thread CliWrap/PipeSource.cs Resolved
Tyrrrz and others added 2 commits June 2, 2026 20:37
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@Tyrrrz Tyrrrz changed the title Add disposeStream parameter on To/FromStream and modify ToFile behavior Add disposeStream parameter on ToStream/FromStream and modify ToFile behavior Jun 2, 2026
@yueyinqiu

yueyinqiu commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Hmm... do we need a ToStream(Func<Stream>)...?

Actually, I don't think creating the FileStream in advance is necessarily a bad thing. (However, this seems to break the API to some extent.)

@Tyrrrz

Tyrrrz commented Jun 3, 2026

Copy link
Copy Markdown
Owner

Hmm... do we need a ToStream(Func<Stream>)...?

Actually, I don't think creating the FileStream in advance is necessarily a bad thing. (However, this seems to break the API to some extent.)

Hmm I'm trying to understand which exact change is responsible for that test failure.

@yueyinqiu

Copy link
Copy Markdown
Contributor Author

Hmm I'm trying to understand which exact change is responsible for that test failure.

It seems that the file used to be created when calling AnonymousPipeSource.CopyToAsync, but it's now immediately created when calling PipeTarget.ToFile. Therefore, if file creation fails, the location where the exception is thrown changes.

@yueyinqiu

yueyinqiu commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Oh wait, here's another problem: if the process fails to start, will ToStream(disposeStream: true) still work? Could this lead to potential leaks?

Hmm... will it be a good idea to make pipetarget/pipesource IAsyncDisposable? But this would break the existing API...

@yueyinqiu
yueyinqiu marked this pull request as draft June 3, 2026 10:22
@Tyrrrz

Tyrrrz commented Jun 3, 2026

Copy link
Copy Markdown
Owner

Hmm I'm trying to understand which exact change is responsible for that test failure.

It seems that the file used to be created when calling AnonymousPipeSource.CopyToAsync, but it's now immediately created when calling PipeTarget.ToFile. Therefore, if file creation fails, the location where the exception is thrown changes.

Ah I see. That's my bad then, I thought refactoring to call ToStream() would be a seamless transition. I'll fix it back.

@Tyrrrz
Tyrrrz marked this pull request as ready for review June 3, 2026 13:15
@yueyinqiu

yueyinqiu commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor Author

Oh wait, here's another problem: if the process fails to start, will ToStream(disposeStream: true) still work? Could this lead to potential leaks?

@Tyrrrz Please check this as well. If the stream cannot be disposed when the process fails to start, perhaps we shouldn't provide such a misleading overload.

After some thought, I realized that adding a dispose method to pipesource/target might be too complex and not worthwhile. If custom behavior is truly needed, it can be achieved via Create instead of ToStream (although this would be much more troublesome, since CopyToAsync(..., autoFlush, ...) doesn't seem to be exposed as a public API).

@Tyrrrz

Tyrrrz commented Jun 3, 2026

Copy link
Copy Markdown
Owner

Oh wait, here's another problem: if the process fails to start, will ToStream(disposeStream: true) still work? Could this lead to potential leaks?

@Tyrrrz Please check this as well. If the stream cannot be disposed when the process fails to start, perhaps we shouldn't provide such a misleading overload.

After some thought, I realized that adding a dispose method to pipesource/target might be too complex and not worthwhile. If custom behavior is truly needed, it can be achieved via Create instead of ToStream (although this would be much more troublesome, since CopyToAsync(..., autoFlush, ...) doesn't seem to be exposed as a public API).

Yes, that's a good point; I haven't fully considered that the stream may not be disposed of in some cases.

Let's downscale to the simplest change we've explore so far: updating the default File.Open parameters (which is already done). Let's not include extra customization options for the file stream yet, I think the defaults should be optimal for 99% of scenarios. In any case, we can revisit this later.

@yueyinqiu yueyinqiu changed the title Add disposeStream parameter on ToStream/FromStream and modify ToFile behavior Modify ToFile behavior Jun 3, 2026
@yueyinqiu

Copy link
Copy Markdown
Contributor Author

Ok I have reverted the changes on ToStream and FromStream

@Tyrrrz
Tyrrrz requested a review from Copilot June 3, 2026 16:09
@Tyrrrz Tyrrrz changed the title Modify ToFile behavior Optimize default file parameters in PipeTarget.ToFile(...) Jun 3, 2026

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.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment thread CliWrap/PipeTarget.cs Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
hawkeye116477 pushed a commit to hawkeye116477/CliWrap-net462 that referenced this pull request Nov 25, 2026
)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Oleksii Holub <1935960+Tyrrrz@users.noreply.github.com>
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.

Feature Request: Support unbuffered writing and shared read access in PipeTarget.ToFile

3 participants