Skip to content

Add new PipeTarget.ToFile overload with more parameters - #332

Closed
yueyinqiu wants to merge 4 commits into
Tyrrrz:primefrom
yueyinqiu:331
Closed

yueyinqiu wants to merge 4 commits into
Tyrrrz:primefrom
yueyinqiu:331

Conversation

@yueyinqiu

Copy link
Copy Markdown
Contributor

Closes #331

I am not entirely sure if I correctly understand the meaning of 'update the default file settings'. I think it is worth noting that avoiding caching may potentially lead to a loss in performance. Additionally, I referred to the current implementation of ToStream(bool autoFlush), and I wonder if we might want to avoid breaking binary compatibility?

Therefore, I have opted to simply add a new overload for now; if you believe the original ToFile could be modified directly, we can adjust it further.

@Tyrrrz

Tyrrrz commented May 22, 2026

Copy link
Copy Markdown
Owner

I am not entirely sure if I correctly understand the meaning of 'update the default file settings'.

I mean't using a more specific overload of File.Create here:

var target = File.Create(filePath);

if you believe the original ToFile could be modified directly, we can adjust it further.

I don't see any reason not to... do you?

@yueyinqiu

yueyinqiu commented May 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Sure! If we welcome such change, I have no problem with it. I have updated the code.

Below are the settings for each parameter:

  • FileMode: The default value is Create, which maintains the original behavior, but users can change it to Append or other values ​​to obtain different behaviors;
  • FileAccess: Always Write as the stream won't be exposed to the caller, and we just need it to be writable;
  • FileShare: The default value is Read, which is different from the previous None (other processes cannot access it);
  • bufferSize: The default value is 4096, which maintains the original behavior;
  • FileOptions: The default value is None, which maintains the original behavior; (I didn't set it WriteThrough by default, as this might be too heavy--in most cases, autoFlush is enough.)
  • autoFlush: The default value is true, which is different from the previous false.

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 PipeTarget.ToFile to support the use case from #331 (real-time log tailing) by allowing callers to control the underlying FileStream settings (mode/share/buffer/options) and flushing behavior.

Changes:

  • Expanded PipeTarget.ToFile to accept FileMode, FileShare, bufferSize, FileOptions, and autoFlush.
  • Switched implementation from File.Create(...) to constructing FileStream directly to apply the requested settings.

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

Comment thread CliWrap/PipeTarget.cs Outdated
Comment on lines +187 to +193
public static PipeTarget ToFile(
string filePath,
FileMode mode = FileMode.Create,
FileShare share = FileShare.Read,
int bufferSize = 4096,
FileOptions options = FileOptions.None,
bool autoFlush = true
Comment thread CliWrap/PipeTarget.cs Outdated
Comment on lines +198 to +199
using var target = new FileStream(filePath, mode, FileAccess.Write, share, bufferSize, options);
await origin.CopyToAsync(target, autoFlush, cancellationToken).ConfigureAwait(false);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmmm... I found no reason to use ToAsyncDisposable here.

Comment thread CliWrap/PipeTarget.cs Resolved
@yueyinqiu

Copy link
Copy Markdown
Contributor Author

I haven't noticed that there is a formatting requirements. I have modified the code.

@Tyrrrz

Tyrrrz commented May 25, 2026

Copy link
Copy Markdown
Owner

Do you think we can avoid creating a new overload and just update the existing method, by replacing the option-less File.Create(...) with your version?

@yueyinqiu

yueyinqiu commented May 26, 2026 •

Copy link
Copy Markdown
Contributor Author

Hmm... I'm not quite sure what concerns are currently preventing us from providing these options.

I think sometimes we might need Append instead of just Create. In some cases, we might also need FileShare to be ReadWrite (but in others it seems less safe), and FileOptions.WriteThrough might be necessary in some very important scenarios (but often it might be overkill and not encouraged). Theoretically speaking, we should always offer greater customizability?

@Tyrrrz

Tyrrrz commented May 27, 2026

Copy link
Copy Markdown
Owner

I think you can get sufficient customizability via PipeTarget.ToStream(new FileStream(...)). I don't want to mirror FileStream's options in PipeTarget.ToFile(...), because then we implicitly take responsibility for keeping it in sync with any additions/changes to FileStream's constructors/factories in newer versions of .NET.

@yueyinqiu

yueyinqiu commented May 28, 2026 •

Copy link
Copy Markdown
Contributor Author

I think you can get sufficient customizability via PipeTarget.ToStream(new FileStream(...))

Actually, in that case, the file stream can't be disposed timely. (Sometimes it's not convenient to manage it yourself.) I think maybe adding a parameter like leaveOpen or autoDispose can also be accepted?

I don't want to mirror FileStream's options in PipeTarget.ToFile(...), because then we implicitly take responsibility for keeping it in sync with any additions/changes to FileStream's constructors/factories in newer versions of .NET.

This might not just be a mirror; instead we genuinely need such features. For example, autoFlush is not a FileStream's option but it is there, and FileAccess, which is one option of FileStream, is not provided. We don't need to update with .NET unless there are actually any new features—in which case it should be updated anyway.

@Tyrrrz

Tyrrrz commented May 28, 2026 •

Copy link
Copy Markdown
Owner

Actually, in that case, the file stream can't be disposed timely. (Sometimes it's not convenient to manage it yourself.) I think maybe adding a parameter like leaveOpen or autoDispose can also be accepted?

Yes, I was thinking about that too. Let's add bool disposeStream = false as the last parameter. Similar to this HttpClient's constructor.

Perhaps we should also update PipeSource.FromStream(...) in the same way for symmetry.

Would that solve the customizability concerns?

@yueyinqiu

yueyinqiu commented May 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Ok sure. I have updated the code.

Additionally, I've merged the two existing ToStream methods. If you think this isn't appropriate, I could change it to three overloads instead.

@Tyrrrz

Tyrrrz commented May 30, 2026

Copy link
Copy Markdown
Owner

@yueyinqiu unfortunately we can't merge those methods because it breaks binary backward compatibility

@yueyinqiu

yueyinqiu commented May 31, 2026 •

Copy link
Copy Markdown
Contributor Author

I've created a new PR #333 instead

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