Skip to content

Enable path globbing for native commands - #1639

Closed
Andy Jordan (andyleejordan) wants to merge 1 commit into
masterfrom
andschwa/globbing
Closed

Andy Jordan (andyleejordan) wants to merge 1 commit into
masterfrom
andschwa/globbing

Conversation

@andyleejordan

Copy link
Copy Markdown
Member

Enable path globbing for native commands

E.g. ls *.md expands to ls /path/to/README.md /path/to/CHANGELOG.md

If expansion fails, the argument is sent literally, so ls foo* results in /usr/bin/ls: cannot access foo*: No such file or directory.

Don't glob expand if the verbatim marker was used.

/cc Jason Shirk (@lzybkr) James Truher (@JamesWTruher)

After tracing what the native command parameter binder does, it seems most reasonable to add this logic to appendOneNativeArgument. I know that doesn't sound right, but in fact it already deals with being sent an argument that gets expanded to a list. However I could be completely wrong, as the code around here is a bit strange.

When a parameter.ParameterNameSpecified is true, we don't want to glob, as this correlates to -flag. Moreover, the CommandParameterInternal should probably be updated to interpret --flag as a Parameter (instead of an Argument); as it does not currently.

We want to glob when parameter.ArgumentSpecified is true, and only once it's been converted to a string (or expanded to a set of strings) (which happens in the poorly named appendOneNativeArgument function).

We do not want to glob if the --% verbatim marker was seen.

I need to add tests, but figured I'd get this up for review.

Also I already have one known bug: globbing doesn't appear to work for .dotfiles. So although *.md works, .gi* and *git* does not work. But it doesn't have anything to do with these changes, it fails similarly for Get-ChildItem too. I'll file a bug report tomorrow.

E.g. `ls *.md` expands to `ls /path/to/README.md /path/to/CHANGELOG.md`

If expansion fails, the argument is sent literally, so `ls foo*` results
in `/usr/bin/ls: cannot access foo*: No such file or directory`.

Don't glob expand if the verbatim marker was used.

foreach (string path in resolvedPaths)
{
arg = path;

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.

There are cases in the following loop where arg gets modified, so path cannot be used as it's an iterator. So arg = path seemed to be the cleanest solution (and meant not having to rename the variable elsewhere).

@andyleejordan

Copy link
Copy Markdown
Member Author

The globber doesn't appear to be handling escapes.

@lzybkr

Copy link
Copy Markdown
Contributor

The failures are legit - in some cases we can't glob.

At a minimum, I think we can't do globbing on Windows at all - Windows tools implement globbing themselves, so even if we did something, it might work differently than the tool we're calling.

That said, we still need to be smarter somehow as the test failure demonstrates (basically powershell -command "[Console]::WriteLine(1)". I think we're treating the left of :: as a provider (like filesystem) and don't recognize it as a provider.

At a minimum, I think we need a way to escape globbing characters, but that's messy. --% helps here, the test failure wouldn't be a problem in this implementation if it used --%, but that's half the solution because you may want to mix arguments with globbing and no globbing, hence the need for some escape.

The question is - where do process escapes? If it was just in the native command processor, it's less risky, but also less useful. If we do it in the globbing code, we also run a greater risk of breaking something.

I'm fine with punting the escape character for now, make that an issue, but put the globbing under #if UNIX and fix the test to use --%.

@TravisEz13 Travis Plunk (TravisEz13) added WG-Engine core PowerShell engine, interpreter, and runtime Review - waiting on author and removed Review - needed labels Aug 6, 2016
@TravisEz13

Copy link
Copy Markdown
Member

Removed review needed since tests are failing...

@andyleejordan

Copy link
Copy Markdown
Member Author

Closing until I get back to this. Thanks for the review!

@andyleejordan
Andy Jordan (andyleejordan) deleted the andschwa/globbing branch August 11, 2016 01:24
@TravisEz13 Travis Plunk (TravisEz13) added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Waiting on Author labels May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept WG-Engine core PowerShell engine, interpreter, and runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants