-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix retrieving dynamic parameters from provider even if globbed path returns no results #15525
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…returns no results
vexx32
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
| } | ||
|
|
||
| if (providerPath != null) | ||
| if (providerInstance != null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was very wonder to read comments in the method above.
Is the method used for both FromSession and ToSession?
If so I wonder again because obviously the method is only for FromSession scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is independent from ToSession and FromSession, it applies to all dynamic parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I say that original design was broken - it makes no sense to check the path existence if it is on remote.
Next question is does it make sense to check the existence for local path? If it even makes sense this is the wrong place for it - all we need is the providerInstance as you said.
Do we really need globbing? I suggest to make minor breaking change and use GetUnresolvedProviderPathFromPSPath() or GetProviderPath().
Then if providerInstance is null then pathNotFoundOnClient is always true and we always effectively fall back to FileSystemProvider:
internal object CopyItemDynamicParameters(
string path,
string destination,
bool recurse,
CmdletProviderContext context)
{
if (path is null)
{
return null;
}
ProviderInfo provider = null;
string providerPath = null;
try
{
providerPath =
context.ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(
path,
context,
out provider,
out _);
}
catch (DriveNotFoundException)
{
// This exception is expected for remote sessions where drives exist in a remote session but not
// on the client.
}
catch (ItemNotFoundException)
{
// This exception is expected for remote sessions where item exist in a remote session but not
// on the client.
}
if (provider is null)
{
// At this point, we don't know if this is a remote use of copy-item because the FromSession
// and ToSession dynamic parameters have not been retrieved yet.
// Ignore these exceptions and use the FileSystem provider as default. If this is a real drive
// issue, or if the item does not exist, it will be caught later when the drive or item path is used.
var fileSystemProviders = Providers["FileSystem"];
if (fileSystemProviders.Count > 0)
{
providerPath = path;
provider = fileSystemProviders[0];
}
}
if (provider is null)
{
return null;
}
CmdletProvider providerInstance = ExecutionContext.EngineSessionState.GetProviderInstance(provider);
return providerInstance is null ? null : CopyItemDynamicParameters(providerInstance, providerPath, destination, recurse, context);
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what the original design intent was, but the path(s) after globbing are passed down to the provider for dynamic parameters (so null is now possible where it wasn't before, but it's an assert so could have happened in retail builds anyways), so presumably a provider could use that information and we should probably not make that breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the path(s) after globbing are passed down to the provider for dynamic parameters
so presumably a provider could use that information
This just makes me think that it's much more useful to pass the original path than after globbing, otherwise in CopyItemDynamicParameters there's no way to know that globbing was used. I believe it is more right design and the breaking change is under bracket 3 (unlikely breaking change).
Although if you find it totally unacceptable, it's worth cleaning up the code anyway, like in my sample.
so null is now possible where it wasn't before
providerPath argument is never null in CopyItemDynamicParameters().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not providerPath, but just path. I think if provider authors would find value in the original search path, we would have heard about it. I don't think it's worth making this change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not providerPath, but just path.
I don't understand. The method starts with:
if (path == null)
{
return null;
}
I think if provider authors would find value in the original search path, we would have heard about it. I don't think it's worth making this change.
I guess nobody uses this :-) I mainly think that the binder is already too slow and we could make it faster.
Moreover, we have already made a breaking change by passing a non-existent file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this is code seems wrong and that parameter binding is not the right place to do path validation. My guess is this code was here when copy-item was local only, and then adjusted when copy-item was extended to support remote copy.
I feel it is worth considering a breaking change to re-review this and possibly fix it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to take a small change now to improve the user experience (they would get an error from the provider instead of the parameter binder not helping them understand why it didn't work) and defer any potential breaking change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is reasonable. Can you create an issue to track it?
|
🎉 Handy links: |
PR Summary
The problem only occurs with a cmdlet that relies on a provider and the path has a wildcard that returns no results. The simple repro is:
You'll get a parameter binding error that
-ToSessionis not a valid parameter. What's happening is that the parameter binder uses the path to find a provider. It correctly finds the filesystem provider and then uses that to glob the wildcard for results. Since the wildcard doesn't match any results, the logic incorrectly skips binding dynamic parameters, so the user gets an unhelpful error message.The fix is to not require that globbing returns any results, just that a provider was returned. Then I removed an assertion checking that the path is not null. I looked through the source code for implementations of
CopyItemDynamicParameters()and the FileSystemProvider doesn't even use the path so it doesn't matter if it's null or not. The other cases already check if path is null and handles it so the assertion is not necessary even if it only affects debug builds.PR Context
Fix #6527
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.(which runs in a different PS Host).