robotcodedev / robotcode

RobotFramework support for Visual Studio Code
https://robotcode.io
Apache License 2.0
177 stars 14 forks source link

[BUG] Library documentation is not differentiated by arguments #200

Closed DanielPenning closed 8 months ago

DanielPenning commented 8 months ago

Describe the bug When libraries are instantiated with different arguments, RF treats them as separate libraries. Robotcode seems to not differentiate and uses the libdoc cache created for the last library instance. In our library, we provide keywords that are dependent on the arguments the library was initiated with. Syntax highlighting does not work propely for these dependent keywords.

To Reproduce Steps to reproduce the behavior:

KeywordLib.py: A library that provides keywords dependent on its argument.

class KeywordLib:
    ROBOT_LIBRARY_SCOPE = 'GLOBAL'

    def __init__(self, lib_arg):
        self.lib_arg = lib_arg
        pass

    def get_keyword_names(self):
        return ['KeywordLib_default', f'KeywordLib_{self.lib_arg}'] 

    def run_keyword(self, name, args, kwargs):
        print("Running keyword '%s' with positional arguments %s and named arguments %s."
                % (name, args, kwargs))

When I use this library in TestFirst.robot, the KeywordLib First keyword documentation is shown:

TestFirst.robot

*** Settings ***
Library    KeywordLib.py    First

*** Test Cases ***
Call All Keywords
    KeywordLib Default
    KeywordLib First

When I use the libary again in TestSecond.robot with a different parameter, robotcode shows the keyword KeywordLib Second as missing. When I run robotcode.clearCacheRestartLanguageServers, the keyword is shown. But switching back to TestFirst.robot, the KeywordLib First keyword is shown as missing there.

TestSecond.robot:

*** Settings ***
Library    KeywordLib.py    Second

*** Test Cases ***
Call All Keywords
    KeywordLib Default
    KeywordLib Second

Expected behavior The libdoc of the specific library instance in the current file should be taken as source for syntax highlighting.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

d-biehl commented 8 months ago

In RobotFramework, it is possible to create dynamic libraries and parameterize them, as you are doing. Occasionally, libraries may have different keywords depending on the parameters, but this is not very common. That's why the first version of a library is always cached, regardless of the parameters.

To disable caching for a specific library, you can use the setting robotcode.analysis.cache.ignoredLibraries. The libraries specified here, as the name suggests, will not be cached, and the system will attempt to initialize the library based on the parameters, similar to how Robot Framework does it. However, depending on the library, this process may take a bit longer.

To still cache this dynamic library, what I like to do is create a wrapper library that inherits from the actual library and passes any necessary arguments when needed. This approach is based on the consideration that if a test object (which is essentially what a library is) provides different methods depending on the parameters, then the various parameter combinations represent multiple functional test objects that can be better represented in separate libraries. Hope you understand?

DanielPenning commented 8 months ago

Many thanks for the very prompt feedback. I did not know about robotcode.analysis.cache.ignoredLibraries. This is exactly what we need to solve the problem, the time it to (re)load the info is not critical. 👍