Skip to content

If multiple keywords match, resolve conflict first using search order #4609

Closed

Description

@jylind

The changes for fixing issue #4366 changed the Set Library Search Order functionality in a way that it cannot be used to force keywords to be used from imported resources if a keyword with same name exists in the importing resource file (or a testsuite in the future version?)

We have an old library (develop originally for RF 3.0) that enable us to created mocked user keywords to be used in user keyword “unit tests”. With this we can replace some of the user keywords (in the same resource file or testsuite) in a user keyword under test with a mocks and only concentrate on the functionality on that keyword. Just the default unit test approaches.

Now that local keywords cannot be overridden from the imported resource files, even using Set Library Search Order, this approach has now become impossible.

Related issue.
BuiltIn library does not have keyword for getting currently set search order. This is a bit inconvenient for our library. Fake keywords (and implementing resources/libraries) can be set in different stages, so it is a bit hard to keep track of currently active search order. It would be lot easier if it could be asked from robot itself.

I guess Set Library Search Order could be called with some dummy value (or None) to get the current value and then set it back (maybe with some modification) but it would look a bit cumbersome.
(In the original implementation we are using internals from BuiltIn to get this information but this is definitely not something that should be done)

Activity

  1. ursah commented on Feb 28, 2023

    @ursah

    I mentioned our library search order issue in RoboCon 2023: Extending Robot Framework workshop - @pekkaklarck had a quick glance at code and it looked like a change to the order of if statements (determining the library to call) would solve the issue to our benefit.

    Any chance to get the solution verified and in before RF 7?

  2. pekkaklarck commented on Mar 1, 2023

    @pekkaklarck
    Member

    Do I get it right that you'd like search order to be used first and only then keywords searched from the same file? That ought to be fine and as @ursah commented shouldn't be too hard to implement. The related code looks like

            if len(handlers) > 1:
                handlers = self._prioritize_same_file_or_public(handlers)
                if len(handlers) > 1:
                    handlers = self._select_best_matches(handlers)
                    if len(handlers) > 1:
                        handlers = self._filter_based_on_search_order(handlers)

    and it would be easy to change the order so that filtering based on search order is done first. Can you test that locally and see how the results look like? If that fixes your issue, I can see are there some acceptance tests that fail if the logic is changed and are those tests so important that the change isn't possible. Other things to take into account:

    • The above code is related to finding keywords from a resource file. There's similar code for finding keywords from libraries that would need to be updated as well.
    • Private keywords probably shouldn't have precedence over public keywords even if they'd be first in search order. That may require a bit bigger changes.
  3. jyligehc commented on Mar 1, 2023

    @jyligehc

    Thanks Pekka for your response.

    This is correct:

    you'd like search order to be used first and only then keywords searched from the same file

    Yes, we can try this out. Just making it sure that I understand this correctly.
    We should test it this way? Or what would be the correct order

            if len(handlers) > 1:
                handlers = self._filter_based_on_search_order(handlers)
                if len(handlers) > 1:
                    handlers = self._prioritize_same_file_or_public(handlers)
                    if len(handlers) > 1:
                        handlers = self._select_best_matches(handlers)
    

    For libraries it sould be like this?:

            if len(handlers) > 1:
                    handlers = self._filter_based_on_search_order(handlers)
                if len(handlers) > 1:
                    handlers = self._select_best_matches(handlers)
                    if len(handlers) > 1:
                        handlers, pre_run_message = self._filter_stdlib_handler(handlers)
    

    Private keywords (those from RF6 ->?).
    We have no experience with those yet so can't really say this or that.
    Obviously, as we haven't had a change to use them yet they wouldn't be a problem.
    But once we get to know them and how we could use them then it might come as an issue.

    We do have concept of "private keyword" (marked as leading underscore). If we would start tagging those private maybe we run into some issues in our code base.

  4. jyligehc commented on Mar 1, 2023

    @jyligehc

    BTW.
    Would it make sense to implement Get Library Search Order keyword in BuiltIn()?

    Now that we are manipulating this search order it is difficult to keep track what is currently active.
    Of course we can set dummy value to get current and then set it back but doesn't look that clean code.

  5. pekkaklarck commented on Mar 1, 2023

    @pekkaklarck
    Member

    Yeah, have that _filter_based_on_search_order first. If it works for you, we need to think what kind of implications it would have for others.

    Adding Get Library Search Order sounds good to me. Please submit a separate issue about that.

  6. jyligehc commented on Mar 2, 2023

    @jyligehc

    We did try this with above changes and it seemed to work as expected.

    I'll create a separate issue forGet library Search Order.

  7. pekkaklarck commented on Mar 24, 2023

    @pekkaklarck
    Member

    Could you try running Robot's acceptance tests with this change to see does it break something unexpected? If not, this could possibly still be changed in RF 6.1.

  8. ursah commented on Mar 28, 2023

    @ursah

    Got the below 6 fails from acceptance tests. The changes were as @jyrlindr proposes above.
    All seem to be related to search order.

    Name ×Documentation ×Tags Status ×Message ×Elapsed ×Start / End
    Robot . Keywords . Embedded Arguments Conflicts . Best match in library wins over search order     FAIL Status of 'Best match in library wins over search order' should have been PASS but it was FAIL. Error message: AssertionError 00:00:00.002 20230327 10:39:27.29420230327 10:39:27.296
    Robot . Keywords . Embedded Arguments Conflicts . Best match in resource wins over search order     FAIL Status of 'Best match in resource wins over search order' should have been PASS but it was FAIL. Error message: Best match != Match 00:00:00.002 20230327 10:39:27.29020230327 10:39:27.292
    Robot . Keywords . Keyword Namespaces . Local keyword in resource file has precedence even if search order is set     FAIL Wrong log message: Keyword in resource 1 != Keyword in resource 2 00:00:00.006 20230327 10:39:30.47020230327 10:39:30.476
    Robot . Keywords . Private . Local Private Keyword In Resource File Has Precedence Even If Search Order Is Set     FAIL Status of 'Local Private Keyword In Resource File Has Precedence Even If Search Order Is Set' should have been PASS but it was FAIL. Error message: Not executed 00:00:00.003 20230327 10:39:41.75420230327 10:39:41.757
    Robot . Standard Libraries . Builtin . Set Library Search Order . Exact match wins over match containing embedded arguments regardless search order     FAIL Status of 'Exact match wins over match containing embedded arguments regardless search order' should have been PASS but it was FAIL. Error message: Should not be run due to keywords with normal arguments having higher precedence. 00:00:00.001 20230327 10:47:04.01620230327 10:47:04.017
    Robot . Standard Libraries . Builtin . Set Resource Search Order . Exact match wins over match containing embedded arguments regardless search order     FAIL Status of 'Exact match wins over match containing embedded arguments regardless search order' should have been PASS but it was FAIL. Error message: Should not be run due to keywords with normal arguments having higher precedence 00:00:00.001 20230327 10:47:05.61120230327 10:47:05.612
  9. ursah commented on Apr 6, 2023

    @ursah

    @pekkaklarck Anything else we could help with to get the library search order change released?

  10. pekkaklarck commented on Apr 18, 2023

    @pekkaklarck
    Member

    Could someone create a pull request with the change? The PR should preferably also contain test fixes. I'm not sure are new tests needed, but the existing failing ones should be renamed appropriately.

  11. 4 remaining items

  12. added this to the v6.1 milestone on May 17, 2023
  13. changed the title [-]Allow Set Library Search Order to override the local keywords precedence.[/-] [+]Use library search order first for resolving conflicts if there are multiple matching keywords[/+] on May 17, 2023
  14. changed the title [-]Use library search order first for resolving conflicts if there are multiple matching keywords[/-] [+]Resolve conflict first using search order if multiple keywords match[/+] on May 17, 2023
  15. pekkaklarck commented on May 17, 2023

    @pekkaklarck
    Member

    This was implemented in great PR #4760 by @ursa-h. Thanks for contribution!

  16. ursa-h commented on May 22, 2023

    @ursa-h
    Contributor

    Nice, thank you @pekkaklarck! Kudos to @jyrlindr and the team for the code change and testing!

  17. changed the title [-]Resolve conflict first using search order if multiple keywords match[/-] [+]If multiple keywords match, resolve conflict first using search order[/+] on Jun 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acknowledgeeffort: smallpriority: mediumrc 1

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions