Skip to content

Call 'DoComplete' when handling FileRedirection for CommandExpression - #4847

Merged
Jason Shirk (lzybkr) merged 5 commits into
PowerShell:masterfrom
daxian-dbw:redirect
Sep 20, 2017
Merged

Jason Shirk (lzybkr) merged 5 commits into
PowerShell:masterfrom
daxian-dbw:redirect

Conversation

@daxian-dbw

Copy link
Copy Markdown
Member

Fix #4812

Summary

When handling file redirection for CommandExpression, we don't call 'DoComplete' on the underlying PipelineProcessor of the FileRedirection object, and thus the EndProcessing method is not called on Out-File, which causes different behaviors between <expr> > out.txt and <expr> | Out-File out.txt.

The fix is to make sure 'DoComplete' is called after the stream output has been written to the redirection pipe.

Also fix another issue

This PR also fixes an issue that could mess up restoring the original pipes. Here is the repro:

PS> 1 *> b.txt > a.txt; 123
Cannot perform operation because object "PipelineProcessor" has already been disposed

The root cause is that we don't always restore pipes in the correct order. Please see the code changes in Compiler.cs for more details.

/cc Michael Klement (@mklement0)

@daxian-dbw

Copy link
Copy Markdown
Member Author

Jason Shirk (@lzybkr) Can you please take a look when you have time? Thanks!

// Expressions for this purpose are kept in 'extraFileRedirectExprs' and will be used later.
extraFileRedirectExprs.Add(Expression.Call(redirectionExpr, CachedReflectionInfo.FileRedirection_CallDoCompleteForExpression));

// The 'UnBind' and 'Dispose' operations on 'FileRedirection' objects must be done in the reversed order of 'Bind' operations.

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.

done in the reverse order

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed. Thanks!

@lzybkr Jason Shirk (lzybkr) 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.

This code has always bothered me - it's more complicated than a redirected command.

I wonder how possible it would be to move most of this logic into a helper method and avoid generating so much complicated code.

@daxian-dbw

Dongbo Wang (daxian-dbw) commented Sep 20, 2017 •

Copy link
Copy Markdown
Member Author

Jason Shirk (@lzybkr) I opened the issue #4879 to tracking the effort to revisit the implementation of this method.

@lzybkr
Jason Shirk (lzybkr) merged commit 4c29f57 into PowerShell:master Sep 20, 2017
@vors
sergei (vors) removed their request for review September 25, 2017 00:34
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
When handling file redirection for CommandExpression, we don't call 'DoComplete' on the underlying PipelineProcessor of the FileRedirection object, and thus the EndProcessing method is not called on Out-File, which causes different behaviors between <expr> > out.txt and <expr> | Out-File out.txt.

The fix is to make sure 'DoComplete' is called after the stream output has been written to the redirection pipe.

Also fix another issue

This PR also fixes an issue that could mess up restoring the original pipes. Here is the repro:

PS> 1 *> b.txt > a.txt; 123
Cannot perform operation because object "PipelineProcessor" has already been disposed
The root cause is that we don't always restore pipes in the correct order. Please see the code changes in Compiler.cs for more details.

Fix PowerShell#4812
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.

Direct redirection of a custom object ([pscustomobject] instance) to a file with ">" produces no output.

3 participants