Skip to content

Resource files should only see keywords it imports itself directly or indirectly #2581

Open

Description

@jesuspv

Given the following files (one for a test case and two for resources):

$ cat cases/case.robot
*** Settings ***

Resource    rc/SSHClient.robot
Resource    rc/TelnetClient.robot

*** Test Cases ***

My Test Case
    SSHClient.Log On
    TelnetClient.Log On

    SSHClient.Log Off
    TelnetClient.Log Off
$ cat rc/SSHClient.robot
*** Variables ***

${SERVER_IP}    1.2.3.4
${SERVER_USER}    guest
${SERVER_PASSWORD}   guest

*** Settings ***

Library    SSHLibrary

*** Keywords ***

Log On
    Open Connection    ${SERVER_IP}
    Login    ${SERVER_USER}    ${SERVER_PASSWORD}

Log Off
    Close Connection
$ cat rc/TelnetClient.robot
*** Variables ***

${SERVER_IP}    1.2.3.4
${SERVER_USER}    guest
${SERVER_PASSWORD}   guest

*** Settings ***

Library    Telnet

*** Keywords ***

Log On
    Open Connection    ${SERVER_IP}
    Login    ${SERVER_USER}    ${SERVER_PASSWORD}
    ...    login_prompt=Username:
    ...    password_prompt=Password:

Log Off
    Close Connection

When running case.robot this kind of warning appears:

[ WARN ] Keyword 'Open Connection' found both from a custom test library 'SSHLibrary' and a standard library 'Telnet'. The custom keyword is used. To select explicitly, and to get rid of this warning, use either 'SSHLibrary.Open Connection' or 'Telnet.Open Connection'.
[ WARN ] Keyword 'Login' found both from a custom test library 'SSHLibrary' and a standard library 'Telnet'. The custom keyword is used. To select explicitly, and to get rid of this warning, use either 'SSHLibrary.Login' or 'Telnet.Login'.
My Test Case

which seems to force us to prefix keywords in the resource files with the library name, even when they have imported only one library (with no name collision in the resource-file scope).

Should not be the scope of keywords called in resource files determined locally to the resource file? (at least, when they can be resolved in the resource-file scope as when we have been explicitly imported the corresponding library)

P.S.: I know that it is intended that keywords can be resolved at runtime to allow dynamically decide the implementation (I do not remember where I read about that behaviour though). However, this seems to hamper Robot framework keywords reusability, forcing us to work around by prefixing keywords, renaming them (globally unique...!), or implementing these keywords in a .py file (to be imported via Library). Please, you can also consult this resource example in the official user guide for additional evidences on how misleading is the current non-local scope resolution.

Activity

  1. changed the title [-]Scope of keywords called in resource files is globally reinterpreted when resource file is imported from another file[/-] [+]Scope of keywords called in resource files is globally reinterpreted when resource file is imported[/+] on Apr 19, 2017
  2. pekkaklarck commented on Apr 23, 2018

    @pekkaklarck
    Member

    I agree resource files should only see keywords they import themselves, not keywords in other resource files or libraries imported in the test case file directly or indirectly. Unfortunately changing that is a big task and I cannot make promises when it would happen.

    One possible workaround is using Set Library Search Order keyword to tell which library to use by default. You still need to prefix kws from the other library, though.

  3. MrBIN89 commented on May 30, 2019

    @MrBIN89

    From my point of view, it would be very nice to fix it. At least, for now, it isn't so clear where is from some keyword become available to use at suite file. Is it become from the specified resource file or from another resource file (which is used in the first resource)

    Any progress or estimations for now?

  4. phoenix9047 commented on Nov 11, 2020

    @phoenix9047

    Hi! Joining this discussion, it'd be so appreciated to have this fixed in any future version! My code in one large project would be so much more fine and clear if there wouldn't be this limitation; and while it's there - I have to use a tricky workaround to avoid usage of these two libraries in the same project..
    Thank you!

  5. pekkaklarck commented on Nov 14, 2022

    @pekkaklarck
    Member

    This is related to #4366 that was fixed in RF 6.0. I hope we could get this fixed soon as well and assign this tentatively to RF 6.1 scope

  6. added this to the v6.1 milestone on Nov 14, 2022
  7. pekkaklarck commented on Jan 4, 2023

    @pekkaklarck
    Member

    Unfortunately it turned out this isn't as easy to implement as #4366 was. When there are two library keywords that match the keyword that was used, we could easily find out which user keyword used the conflicting keyword. Unfortunately that UserKeywordHandler object doesn't have a direct access to the resource file it belongs to. With some effort we could find the UserLibrary instance that contains that UserKeywordHandler, but that UserLibrary only contains keywords and doesn't know anything about imports. The actual ResourceFile object containing imports, keywords and variables isn't actually even preserved after it's processed.

    With some enhancements to the aforementioned model objects we could make the resource file and its imports accessible via the user keyword that caused the conflict. Unfortunately these imports would just be a list of strings and we needed to somehow map them to libraries that have been imported. That gets even more complicated than anything earlier.

    A proper way to fix this is pretty much re-implementing keyword namespaces so that keywords in resources or libraries imported in different resource files don't leak like this in the first place. That's a pretty big task, and I was hoping we could resolve conflict otherwise until we get it done. Unfortunately it seems also resolving conflicts is lot of work as well, so it's probably best to wait until we can do the bigger rewrite.

  8. modified the milestones: v6.1, v7.0 on Jan 4, 2023
  9. pekkaklarck commented on Aug 25, 2023

    @pekkaklarck
    Member

    Unfortunate too big for RF 7.

  10. modified the milestones: v7.0, v8.0 on Aug 25, 2023
  11. jonsim commented on Nov 4, 2025

    @jonsim

    Just adding another use-case to this - consider the following test and resource file:

    test.resource

    *** Settings ***
    Library  OperatingSystem
    
    *** Keywords ***
    Verbose Log File
        [Arguments]  ${file}
        Log       Logging file ${file}
        Log File  ${file}

    test.robot

    *** Settings ***
    Resource  test.resource
    
    *** Test Cases ***
    Say File Contents
        Verbose Log File  ${CURDIR}/file.txt
    
    *** Keywords ***
    Log File
        [Arguments]  ${file}
        Log  intercepted log file call to ${file}

    Running test.robot results in the test unexpectedly using the Log File keyword defined in the test file, not the one imported into the resource file (similar to #2970 but for keywords). While this example is somewhat contrived, this is a real issue in practice when test.resource is third-party code. The conflict can also occur when integrating two libraries together (e.g. if the resource file imports OperatingSystem and your test file imports SSHLibrary, the resource file will use conflicting keywords from SSHLibrary).

    The only robust way to write resource files appears to be to prefix all keywords with the library or resource file name.

  12. MarvKler commented on Mar 24, 2026

    @MarvKler
    Contributor

    Hi guys!

    I have another use case:
    Keyword Library A is used by Keyword Library B and Keyword Library B is used within a .robot suite file. At the moment, you also can see, select & use the keywords from Keyword Library A directly in the test case.
    In my case, sometimes i want to see keywords from Keyword Library A only in Keyword Library B, but not in the .robot suite file, because in the test case the testers should use only keywords from Keyword Library B.

    PS: Keyword Library A/B= .resource files (not .py)

    @pekkaklarck is this use case getting resolved by this feature / bug fix ?


    I hope this is the correct feature to ask - at least for me it sounds similiar to my use case.

  13. changed the title [-]Scope of keywords called in resource files is globally reinterpreted when resource file is imported[/-] [+]Resource files should only see keywords it imports itself directly or indirectly[/+] on Sep 12, 2026
  14. pekkaklarck commented on Sep 12, 2026

    @pekkaklarck
    Member

    We haven't started detailed RF 8 planning yet, but this is likely going to be one of the most important issues to fix in that release. My current thinking is that these are the desired semantics:

    1. A test case file see all keywords in all libraries and resource files it imports, recursively. It obviously also sees local keywords in the test case file itself and these keywords have the highest precedence. This is also the current behavior.
    2. A resource file sees all keywords in all libraries and resource files it imports, recursively. It obviously also sees local keywords in the resource file itself and these keywords have the highest precedence. This is also the current behavior.
    3. A resource file doesn't see keywords imported by or defined in a test case file. If a resource file is imported by another resource file, the imported resource file doesn't see keywords the importing resource file imports elsewhere or defines itself. This is what changes.

    In practice a resource file should see keywords that are defined in the resource file itself or that it imports either directly or indirectly via other resource files.

  15. pekkaklarck commented on Sep 12, 2026

    @pekkaklarck
    Member

    This change is backwards incompatible and we need to handle it gracefully. In practice if a keyword isn't found using the new semantics, we should search for it starting from the test case file level which basically matched the old semantics. Then we have two alternatives:

    1. Log a deprecation warning and use the keyword found with the old semantics.
    2. Fail the keyword call, but instead of reporting that the keyword wasn't found, report that semantics have changed and that the keyword needs to be imported explicitly.

    I believe the first approach is better, because then old tests ought to continue to work. This issue would probably still deserve the backwards incompatible label, because in some cases the keyword that is used could also change.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions