Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ArmaanMcleod
Copy link
Contributor

@ArmaanMcleod ArmaanMcleod commented Jan 29, 2025

PR Summary

Fixed WildcardPattern.IsMatch to not include backtick in SpecialChars. Given -like maps to WildcardPattern.IsMatch it was breaking for comparison with unescaped and escaped comparisons.

PR Context

Fixes #16306

PR Checklist

@iSazonov

This comment was marked as outdated.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jan 29, 2025
@azure-pipelines

This comment was marked as outdated.

@ArmaanMcleod ArmaanMcleod requested a review from iSazonov January 29, 2025 15:22
@iSazonov
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 4 pipeline(s).

@iSazonov
Copy link
Collaborator

@ArmaanMcleod Rethinking related issue I believe right fix could be removing backtick from SpecialChars. I add a comment there. Could you please try this in new PR?

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Jan 30, 2025

@ArmaanMcleod Rethinking related issue I believe right fix could be removing backtick from SpecialChars. I add a comment there. Could you please try this in new PR?

@iSazonov Could do that but current fix is not breaking behaviour since backtick handled separately in Escape() method. If there is a decision to allow this then this is the simplest fix.

@iSazonov
Copy link
Collaborator

@ArmaanMcleod Discussion in the issue was too short, did not found root of the issue. I think their assumption about Escape() is wrong. So I would like to see the test results after removing backtick from SpecialChars so that we can investigate further.

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Jan 30, 2025

@ArmaanMcleod Discussion in the issue was too short, did not found root of the issue. I think their assumption about Escape() is wrong. So I would like to see the test results after removing backtick from SpecialChars so that we can investigate further.

@iSazonov Yep done. Lets see test results and see.

@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@iSazonov
Copy link
Collaborator

iSazonov commented Jan 30, 2025

I see only our updated xUnit test failed. Could you please fix tests.
It confirms we are on right way.

@ArmaanMcleod
Copy link
Contributor Author

ArmaanMcleod commented Jan 30, 2025

I see only our updated xUnit test failed. Could you please fix tests. It confirms we are on right way.

@iSazonov Yep will do. I also think it is worth bring back Pester tests and doing -like comparison with escaped result, that will cover the match comparison which was highlighted in the issue.

E.g. We need a test which validates below works:

'a`b' -like 'a`b'
True

I think with a change like this the more tests the better to cover all grounds.

@iSazonov

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@ArmaanMcleod ArmaanMcleod requested a review from iSazonov February 1, 2025 12:49
@ArmaanMcleod ArmaanMcleod requested a review from iSazonov February 3, 2025 12:02
@ArmaanMcleod ArmaanMcleod changed the title Fix WildcardPattern.Escape to escape backticks correctly Fix WildcardPattern.IsMatch to not include backtick as special character Feb 3, 2025
@ArmaanMcleod ArmaanMcleod changed the title Fix WildcardPattern.IsMatch to not include backtick as special character Fix WildcardPattern.IsMatch to not include backtick in SpecialChars Feb 3, 2025
@iSazonov
Copy link
Collaborator

iSazonov commented Feb 7, 2025

@mklement0 Could you please play with the build and try to find side (break something) effects?

@mklement0
Copy link
Contributor

@iSazonov, I haven't really followed the discussion, so I'm hoping @IISResetMe can take a look.

@iSazonov
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Feb 17, 2025
@IISResetMe
Copy link
Collaborator

IISResetMe commented Mar 5, 2025

Forgive me if I'm missing something obvious here, but this change appears to break IsMatch in the same way that Escape is broken - that certainly wasn't my intention with #16306

This behavior:

PS ~> 'abc`def' -like 'abc``def'
True
PS ~> 'abc`def' -like "abc````def"
True
PS ~> [WildcardPattern]::new('abc``def').IsMatch('abc`def')
True

... is entirely correct, I'm not suggesting we change that at all - having to use the same escape character for both the literal syntax and the pattern is potentially confusing, and I'm sure we can do a better job of explaining it in the docs, but it's entirely expected.

Instead of breaking IsMatch, WildcardPattern.Escape() should escape literal `'s:

PS ~> [WildcardPattern]::Escape('a`b')   # correct/desired behavior
a``b
PS ~> [WildcardPattern]::Escape('a`b')   # incorrect/current behavior
a`b

If we make that change instead, then this will start working as expected:

PS ~> 'abd`def' -like [WildcardPattern]::Escape('abc`def')  # desired behavior
True

@iSazonov
Copy link
Collaborator

iSazonov commented Mar 6, 2025

@IISResetMe Thanks!

I see

previousCharacterIsAnEscape = (c == '`') && (!previousCharacterIsAnEscape);

This says that WildcardPattern explicitly process escaping in Pattern. So the PR is not right way.

@microsoft-github-policy-service microsoft-github-policy-service bot removed the Review - Needed The PR is being reviewed label Mar 6, 2025
@ArmaanMcleod
Copy link
Contributor Author

Thanks @IISResetMe @iSazonov

Yeah had the same conclusion and just got confused. I will close this PR since its not the correct fix.

We can start again in another PR. Happy for you to take over if you have the right fix in mind. If not i will have a look based on our findings when i have time πŸ™‚

@microsoft-github-policy-service
Copy link
Contributor

microsoft-github-policy-service bot commented Mar 6, 2025

πŸ“£ Hey @ArmaanMcleod, how did we do? We would love to hear your feedback with the link below! πŸ—£οΈ

πŸ”— https://aka.ms/PSRepoFeedback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WildcardPattern.Escape() does not escape lone backticks correctly

4 participants