Skip to content

Give local keywords precedence over imported keywords in resource files #4366

Closed

Description

@pekkaklarck

Currently if we have a resource file like

*** Settings ***
Resource      another.resource

*** Keywords ***
Keyword
    Internal Keyword

Internal Keyword
    No Operation

and also the imported another.resource contains keyword with name Internal Keyword, execution fails due to there being two keywords matching the same name. Failing like that would be fine if both matches were imported, but when a resource file has a local keyword matching the name it should have precedence.

This change would be good in general, but it gets more important when we add support for private keywords in resource files (#430). Public keywords will have precedence over private keywords otherwise, but keywords in the same resource files should call them automatically if there's a conflict.

Activity

  1. added this to the v5.1 milestone on Jun 15, 2022
  2. pekkaklarck commented on Jul 5, 2022

    @pekkaklarck
    MemberAuthor

    The example in the description contains keyword Internal Keyword and another keyword Keyword that uses it. The idea of this issue is making sure Keyword would always call the local Internal Keyword even if there would be a keyword with the same name implemented elsewhere. There are three different places where such a keyword could be implemented:

    1. In a library. This is something we don't need to care, because user keywords always have a higher precedence that libraries. Already now the local Internal Keyword would be called if there was a library having a keyword with the same name.

    2. In another resource file. This currently causes an error due to there being multiple keywords with the same name. Fixing this situation so that a keyword in the same file is always called is the core of this issue. Because such a situation is currently an error, changing it wouldn't be backwards incompatible.

    3. In the test case file using that resource file. This situation is problematic, because we currently give keywords in test case files the highest precedence. If you now have Internal Keyword in a test case file, it is unconditionally called instead of the local Internal Keyword. Changing this would thus change functionality and be a backwards incompatible change. I'll write another comment about that.

  3. pekkaklarck commented on Jul 5, 2022

    @pekkaklarck
    MemberAuthor

    As discussed above, we currently give keywords in test case files the highest precedence. The motivation has been that tests always call keywords implemented in the same file even if keywords with same names would be elsewhere. That behavior is something we absolutely don't want to change.

    This functionality also means that it is possible to override certain keywords implemented in resource files by having keywords with the same name in the test case file. That's a somewhat questionable and complicated use case. Keywords in a test case file accidentally overriding keywords resource files use can also cause really strange errors.

    My opinion is that it would be better to change this that keywords in resource files would always use local keywords even if there would be a keyword with the same name in the test case file. That would be, however, a backwards incompatible change and doing them in a non-major release like RF 5.1 is generally not a good idea. I'll write another comment about alternatives we now have.

  4. pekkaklarck commented on Jul 5, 2022

    @pekkaklarck
    MemberAuthor

    We need to make a decision how to handle a situation that a keyword in a resource file (e.g. Keyword in the example in the description) uses another keyword (e.g. Internal Keyword) so that there are implementations both in the same resource file and in the test case file. These are the alternatives:

    1. Preserve the current behavior and always use the keyword in the test case file. Only favor local keywords if there's a keyword with the same name implemented in another resource file. This would be easy to implement and wouldn't have backwards incompatibility concerns. Local keywords not always having a precedence would be inconsistent, though.

    2. Always give local keywords precedence over keywords implemented elsewhere, regardless where they are implemented. Change this already in RF 5.1 even though the change is backwards incompatible.

    3. Same as option 2 but with a deprecation period. In RF 5.1 we'd emit a warning if a keyword in a test case file is used over a local keyword in a resource file and in RF 5.2 (or possibly only in RF 6.0) we'd change the behavior.

    I somewhat strongly prefer local keywords always having highest precedence which would rule the option 1 out. Deprecating this can be somewhat complicated and certainly adds extra work so I'd prefer option 2, but backwards incompatible change in a non-major version without a deprecation are so bad that I guess the option 3 is safest. What do others think about this?

  5. aaltat commented on Jul 5, 2022

    @aaltat
    Contributor

    I would go also with local keywords, it feels most natural when looking test data. But I don't feel strongly about it.

  6. pekkaklarck commented on Jul 5, 2022

    @pekkaklarck
    MemberAuthor

    Local keywords in resource files are now favored over imported keywords with same name. That was needed to enhance how private keywords (#430) work if there is also public keyword with the same name. Still need to decide do we want to favor local keywords over keywords in test cases files.

  7. pekkaklarck commented on Jul 11, 2022

    @pekkaklarck
    MemberAuthor

    I opened a discussion about this on the #devel channel on our Slack and people in general favored local keywords always having precedence. I think that's a good goal, but changing the behavior in RF 5.1 so that keywords in test case files wouldn't anymore have the highest precedence is somewhat badly backwards incompatible. I thus decided to handle this so that test case file keywords will still have precedence also over local keywords in resource files but that is deprecated. The behavior can then be changed in RF 6.0.

  8. tminakov commented on Mar 30, 2023

    @tminakov

    Are there plans to be able to override this behavior on demand - by using special tags, or some other technique?

    My use case - there is this high-level keyword in a resource file:

    Do Operation X
        General Flow A
        General Flow B
        General Flow C
    

    , which covers the flow in most usages (99% of the cases). General Flow * are defined in the same resource file.

    But in specific suites a sub-step is different (slightly modified/customized UI, or additional operation is needed, etc) and all the time I'm using RF I've been overriding it - e.g. General Flow B is redefined in that suite to cater to its custom needs.
    Suite-level precedence allowed for that, and that was also what the guide described for scoping.

    As I understand the change in v7 will break this pattern - thus the question, can the behavior be overriden on demand to the old one?

  9. tminakov commented on Mar 30, 2023

    @tminakov

    A question about the effect on variables' scope - when a variable is defined in a resource file, used in a keyword in that same file, and a suite imports the resource, does not override the keyword but declares the same variable with a new name - which value will the keyword use?
    Its local (from the resource) or the redefined in the suite?

    The use case is similar to my previous comment - a shared resource file that covers the majority of the usage, but in specific suites the operation a keyword provides is differet/customized and some of it variables are redefined (for example - UI operation with different locators; or a "file getter" with different source URI just for this suite).

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions