FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix "Invoke-Item" to accept a file path with spaces on Unix platforms by daxian-dbw · Pull Request #3850 · PowerShell/PowerShell · GitHub

Fix "Invoke-Item" to accept a file path with spaces on Unix platforms - #3850

Merged
Dongbo Wang (daxian-dbw) merged 4 commits into
PowerShell:masterfrom
daxian-dbw:invoke-item
May 24, 2017
Merged

Fix "Invoke-Item" to accept a file path with spaces on Unix platforms#3850
Dongbo Wang (daxian-dbw) merged 4 commits into
PowerShell:masterfrom
daxian-dbw:invoke-item

Conversation

Dongbo Wang (daxian-dbw) commented May 23, 2017
edited
Loading

Copy link
Copy Markdown
Member

Address #2900

Root Cause

On Unix platforms, we are currently depending on xdg-open on Linux and open on OSX to open the default program for a registered file type, so the file path is served as an argument to xdg-open and open. However, we didn't handle the case when path contains spaces.

Fix

Use the method NativeCommandParameterBinder.NeedQuotes, which is used by powershell native command processor, to check if quote is needed. If yes, add quotes in the same way as our native command processor (see the code here).
There is another problem with the current Invoke-Item on Linux and OSX -- it cannot invoke an executable properly. The fix is to first run Process.Start directly with the file path, and if it fails, then try xdg-open or open.

Additional Info

We are focusing on 2 scenarios for "Invoke-Item":

  • it can start an executable
  • it can open a file with the default program that is registered to the file type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Looks good, except one small comment.

#else
invokeProcess.StartInfo.FileName = path;
invokeProcess.Start();
finally { /* Nothing to do in FullCLR */ }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Do we need a empty finally block?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The finally block is kept there for FullCLR section only to match the try block above, so that we don't need to enclose try { and } in if/def. I will add one more comment to make the purpose more clear.
An empty finally block will be ignored during compilation (release version), so there won't be any performance concern. See the code below:

static void Main(string[] args)
{
    Process p = null;
    try { p = new Process(); } finally {  }
}

Corresponding IL

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       7 (0x7)
  .maxstack  8
  IL_0000:  newobj     instance void [System.Diagnostics.Process]System.Diagnostics.Process::.ctor()
  IL_0005:  pop
  IL_0006:  ret
} // end of method Program::Main

Copy link
Copy Markdown
Collaborator

The cmdlet seems to have the most unpredictable behavior😕

Copy link
Copy Markdown
Member Author

Ilya (@iSazonov) I think we are focusing on 2 scenarios for this cmdlet:

  • it can start an executable
  • it can open a file with the default program that is registered to the file type.

Dongbo Wang (daxian-dbw) merged commit 99f9ef2 into PowerShell:master May 24, 2017
Dongbo Wang (daxian-dbw) deleted the invoke-item branch May 24, 2017 20:31

Copy link
Copy Markdown
Collaborator

Dongbo Wang (@daxian-dbw) Thanks for clarify. Could you move the comment to the PR description for future docs?

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj/PowerShell that referenced this pull request Aug 6, 2026
…PowerShell#3850)

Use the method NativeCommandParameterBinder.NeedQuotes, which is used by powershell native command processor, to check if quotes are needed. If yes, add quotes in the same way as our native command processor.
Also, make 'Invoke-Item' on Linux and OSX able to invoke an executable properly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

4 participants


Back | FazBrowse Home | New Git URL