robotcodedev / robotcode

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

[BUG] Adding Library in settings in .resource execute the whole python library file at each letter #180

Closed massicotjgab closed 5 months ago

massicotjgab commented 8 months ago

Describe the bug When adding a library in settings category, at each letter of Library name, python file is executed which can lead in ressource loss depending on what is executable on the file. In my case a popup creation which litterally pop up at each letter of library name.

To Reproduce Steps to reproduce the behavior:

I have a python file which can open a popup. In nominal case, execute file do nothing but I forgot a call I made for a test, to a class+method which produce a popup. My library is at directory Dir root as following: Dir/Library.py Then in .resource file I add

***Settings***
Library     Dir

No problem for now, but when I add the dot then the first letter

***Settings***
Library     Dir.L

Window pop litterally up.

***Settings***
Library     Dir.Li

idem, etc...

Then I could have Library name but at each class letter, window pop up 3 times.

***Settings***
Library     Dir.Library.C

Edit: I also tried whith logging "X" in a file, it did the same thing but seems to be executed only once for each letter of class name.

In my case it's not very painfull because I just had to remove the line I forgot in my code, but it reveals a corner case which can rally impact user experience or lead to a leak of ressource, depending on what is executed this way.

Expected behavior Library is not executed at least while "Library" line is not fully completed.

Logs Robocode Language server mode => WARNING: QApplication was not created in the main() thread. (It's the only log it produces)

Desktop (please complete the following information):

Additional context Popup is displayed through ssh/X11 forwarding from Virtual machine Ubuntu to Windows host.

d-biehl commented 8 months ago

This is because what is a library in RobotFramework and that RobotCode has to instantiate a library to get information about the keywords and so on. For example, you can use a class in a python file as a library.

e.g. you have a file testmodule.py

class TestLibrary:
    def do_something(self):
        ...

then you can include this file in a .robot file as follows:

*** Settings ***
Library testmodule.TestLibrary

Code on the top-level of the file is executed every time, als the constructor of a Library class is also executed each time.

To prevent this, you should encapsulate your code in a library that really executes something in a query whether RobotFramework is currently running. Similar to the __main__ query in a Python file.

from robot.libraries import BuiltIn

if BuiltIn().robot_running:
   do_something_to_initialize_the_library()

oder

from robot.libraries import BuiltIn

class MyLibrary:
    def __init__(self, test_data=None):
        self.test_data = test_data
        if BuiltIn().robot_running:
            do_something_to_initialize_the_library()

This functionality is included at RobotFramework 6, but unfortunately it has not been documented, see here https://github.com/robotframework/robotframework/issues/4906

I'll leave this issue open and think about if there is any other way to mitigate the behavior.

do you have any suggestions?

massicotjgab commented 8 months ago

Thanks for your workaround, it could really help us, because we have some libraries which instantiate threads, and we also can be in a blocked state because of this. But in another way, some libraries we have must be used either in Python and in robot.

If I have some time, I could have a look on your code, fur for now I haven't oppened your source code for now, so I haven't suggestion. Can you tel me where it's done in the code?