robotcodedev / robotcode

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

Variable inspector shows a local variable which is not accessible #262

Closed simonmeggle closed 2 days ago

simonmeggle commented 1 month ago

I am reproducing the example in the Robot Framework User Guide about the VAR syntax:

https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#var-syntax

Then I start the Debugger with a breakpoint here.

This is correct: the variable ${local} is a local one:

CleanShot 2024-06-05 at 11 32 32

When I debug into the keyword Keyword, this does not seem correct to me:

CleanShot 2024-06-05 at 11 36 59

The Variable inspector shows local as a test variable, which is not true.

My expectation is that the variables shown in the list are accessible at the current line. (Or am I wrong...?)


*** Variables ***
${SUITE}         this value is overridden

*** Test Cases ***
Scope example
    VAR    ${local}     local value
    VAR    ${TEST}      test value      scope=TEST
    VAR    ${SUITE}     suite value     scope=SUITE
    VAR    ${GLOBAL}    global value    scope=GLOBAL
    Should Be Equal    ${local}     local value
    Should Be Equal    ${TEST}      test value
    Should Be Equal    ${SUITE}     suite value
    Should Be Equal    ${GLOBAL}    global value
    Keyword
    Should Be Equal    ${TEST}      new test value
    Should Be Equal    ${SUITE}     new suite value
    Should Be Equal    ${GLOBAL}    new global value

Scope example, part 2
    Should Be Equal    ${SUITE}     new suite value
    Should Be Equal    ${GLOBAL}    new global value

*** Keywords ***
Keyword
    Should Be Equal    ${local}      local value   # <--- ADDED
    Should Be Equal    ${TEST}      test value
    Should Be Equal    ${SUITE}     suite value
    Should Be Equal    ${GLOBAL}    global value
    VAR    ${TEST}      new ${TEST}      scope=TEST
    VAR    ${SUITE}     new ${SUITE}     scope=SUITE
    VAR    ${GLOBAL}    new ${GLOBAL}    scope=GLOBAL
    Should Be Equal    ${TEST}      new test value
    Should Be Equal    ${SUITE}     new suite value
    Should Be Equal    ${GLOBAL}    new global value
d-biehl commented 3 days ago

for me this seems to be the correct behavior. the first break is in a test case, then the local scope is the test scope, the second break is in a keyword, now the local scope is the keyword scope, but you look in the suite scope. a keyword cannot see the variables in test scope. but if you came back from the keyword execution then your are back in test scope and the test scope becomes the local scope:

what did you expect?

simonmeggle commented 3 days ago

a keyword cannot see the variables in test scope.

This is where I disagree. The User Guide states: "Variables with the test case scope are visible in a test case and in all user keywords the test uses.".

However, VAR ${local} local value defines a variable with local scope. Even when it's defined on test level, it's not valid on the whole test scope.

Perhaps I am wrong, but during debugging I expect to have access to all variables which are listed on the left, grouped by their scope local/test/suite/global. Do you agree with that?

In the second screenshot you see Robot Framework failing correctly because I try to access such a local variable in a sub keyword. To make that work, I would need to use a kw argument and pass the local variable to the keyword. However, on line 25 the variable inspector makes me believe that Robot Framework has access to ${local}, which is wrong in my opinion. ${local} here looks like a test scoped variable. But in fact it has local scope on test level.

d-biehl commented 2 days ago

Ah ok, now I understand. The problem here is that when robot creates a new execution context, the existing variables are simply copied into the new context. This means that each execution context has a complete copy of the variables from the parent context. This is somewhat memory-consuming, but it's easy to handle because when you want to read a variable, you don't have to consider the parent context.

In the debugger, I need to figure out the differences between the individual contexts. So, if I want to display the variables of the suite context, I take the variables from the suite context and check which of them are not in the global context. For the test context, I use the suite context. And for the local variables, I need to check if I am in a keyword and then subtract either the suite context or the test context.

Unfortunately, I cannot determine how a variable was defined, whether with VAR + scope or Set Global/Suite/Test Variable or is it a local variable.

Therefore, the suite variables also show the local variables created in the suite.

I need to figure out how to do this better, so I'll leave this issue open for now.

simonmeggle commented 2 days ago

👍 Thanks Daniel, I appreciate your efforts!