Using variables in VS code

Start to use VS Code and Robot Framework using Robocorp extensions.
To use all Keywords files in a single folder, In my init file, under Variables section, I use:
${KW_DIR} %{HOME}{/}keywords

In my test I use a resource file:
Resource ${KW_DIR}/general_commands.robot

VS Code marks the line as error:
Unresolved resource: ${KW_DIR}/general_commands.robot

Although it identifies Variable KW_DIR as %{HOME}{/}keywords

Where should I define it to fix it, robot.yaml? where?

Thanks !

I just tested this example and it works just fine on loading the keyword and using it; it’s even configurable by an env var with a default defined under *** Variables ***:

*** Settings ***
Resource    %{RESOURCES_ENV=${RESOURCES}}/common.resource


*** Variables ***
${RESOURCES}    resources

where indeed, I get a VSCode intellisense highlight as well on the env-var-with-default construct:

But it works just fine! @fabio any idea why VSCode isn’t discovering resources through env vars? Is it because they might have an arbitrary value which can’t be deducted in advance?

@yelcav Anyway, in your case, your global var isn’t static as it depends on your %{HOME} and since that can be changed/defined during runtime only, VSCode might not be sure if the resource file is found there or not. (as it cannot know in advance the value of it in the first place)

@yelcav using that case exactly it fails because you’re using {/} instead of ${/} – at least it does work for me – if it still doesn’t work for you, can you create a bug report in Issues · robocorp/robotframework-lsp · GitHub attaching a sample project where that fail?

– Note that you don’t even need that as a variable (you can just use / which works on Linux and Windows).

@cosmin the use case you mentioned is a different one (it seems it can’t resolve a default value of an environment variable when it’s not found and it point to another variable) – I created Support case where default value for an env var is a variable. · Issue #715 · robocorp/robotframework-lsp · GitHub to tackle that.

– in general it should resolve it if RESOURCES_ENV was actually defined in your environment (but note that it loads the values used when the language server starts – if it’s changed in the system you need to restart VSCode to get new values).

1 Like

Sounds good @fabio and totally agree. Good catch on the wrong {/} vs. good ${/} path separator issue!

Thanks guys, the typo was just in my request (${/}), its defined correctly in code
The tests works fine and all the resources that I define in the tests are resolvable during runtime

The issue is just VS Code, I cannot perform simple actions like “Go to Definition”

Thank you for pushing on it @yelcav .

Just made a second test within %{HOME} and indeed, the robot works just fine but it doesn’t recognise the path for going to the definition, even when this env var is already defined in the system:

Screenshot 2022-08-01 at 11.10.51

Added this as a comment to the issue.

Thanks @cosmin !
I will try to play with relative path

1 Like

@yelcav I’ve just made a new pre-release (0.49.2) which should fix this issue.

Can you get it and see if it works for your actual use case?

– note: if it doesn’t work with that version it should at least provide a better error message, so, if you still have any problem please report back with the new error message.

1 Like