robotcodedev / robotcode

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

[BUG] Initializing library 'Browser' with no arguments failed #292

Closed Leemur89 closed 1 month ago

Leemur89 commented 1 month ago

Describe the bug Hello, Robotcode cannot import the browser library as it fails to find node

To Reproduce Steps to reproduce the behavior:

*** Settings ***
Library             Browser

Browser is highlighted and following error is displayed (note that I manually edited the playwright.py file to dump the content of the PATH, which shows /usr/bin:/bin:/usr/sbin:/sbin and not my virtual env):

Import definition contains errors.robotcode.namespace(ImportContainsErrors)
playwright.py(72, 1): Initializing library 'Browser' with no arguments failed: PATH: /usr/bin:/bin:/usr/sbin:/sbinCouldn't execute node. Please ensure you have node.js installed and in PATH. See https://nodejs.org/ for instructions. Original error is [Errno 2] No such file or directory: 'node'
Traceback (most recent call last):
File "/Users/***/Documents/project/.venv/lib/python3.8/site-packages/Browser/playwright.py", line 69, in ensure_node_dependencies
run(["node", "-v"], stdout=DEVNULL, check=True)
File "/Users/***/.pyenv/versions/3.8.19/lib/python3.8/subprocess.py", line 493, in run
with Popen(*popenargs, **kwargs) as process:
File "/Users/***/.pyenv/versions/3.8.19/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Users/***/.pyenv/versions/3.8.19/lib/python3.8/subprocess.py", line 1720, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'node'

During handling of the above exception, another exception occurred (note that I manually edited the playwright file to display the PATH environment variable in the error message):

Traceback (most recent call last):
File "/Users/***/Documents/project/.venv/lib/python3.8/site-packages/Browser/browser.py", line 875, in __init__
self.playwright = Playwright(
File "/Users/***/Documents/project/.venv/lib/python3.8/site-packages/Browser/playwright.py", line 53, in __init__
self.ensure_node_dependencies()
File "/Users/***/Documents/project/.venv/lib/python3.8/site-packages/Browser/playwright.py", line 72, in ensure_node_dependencies
raise RuntimeError(
RuntimeError: PATH: /usr/bin:/bin:/usr/sbin:/sbinCouldn't execute node. Please ensure you have node.js installed and in PATH. See https://nodejs.org/ for instructions. Original error is [Errno 2] No such file or directory: 'node'

Expected behavior Library is imported

Logs Robotcode output:

client for file:///Users/***/Documents/project stopped.
executeRobotCode: /Users/***/Documents/project/.venv/bin/python -u -X utf8 /Users/***/.vscode/extensions/d-biehl.robotcode-0.85.0/bundled/tool/robotcode --format json --no-color --no-pager --default-path . profiles list
executeRobotCode: exit code 0
create Language client: RobotCode Language Server mode=pipe for folder "project"
trying to start Language client: RobotCode Language Server mode=pipe for folder "project"
client for file:///Users/***/Documents/project starting.
executeRobotCode: /Users/***/Documents/project/.venv/bin/python -u -X utf8 /Users/***/.vscode/extensions/d-biehl.robotcode-0.85.0/bundled/tool/robotcode --format json --no-color --no-pager --default-path . profiles list
executeRobotCode: exit code 0
client for file:///Users/***/Documents/project running.
executeRobotCode: /Users/***/Documents/project/.venv/bin/python -u -X utf8 /Users/***/.vscode/extensions/d-biehl.robotcode-0.85.0/bundled/tool/robotcode --format json --no-color --no-pager --default-path . profiles list
client for file:///Users/***/Documents/project started.
executeRobotCode: /Users/***/Documents/project/.venv/bin/python -u -X utf8 /Users/***/.vscode/extensions/d-biehl.robotcode-0.85.0/bundled/tool/robotcode --format json --no-color --no-pager discover info
executeRobotCode: /Users/***/Documents/project/.venv/bin/python -u -X utf8 /Users/***/.vscode/extensions/d-biehl.robotcode-0.85.0/bundled/tool/robotcode --format json --no-color --no-pager --default-path . discover --read-from-stdin all
executeRobotCode: exit code 0
executeRobotCode: exit code 0
executeRobotCode: exit code 0

Robotcode LSP output:

[Error - 11:06:21 AM] Server process exited with code 0.

Desktop (please complete the following information):

Additional context I did as you suggested here but this did not change anything :/

Note that I'm using nvm, can this interfere?

Thanks

d-biehl commented 1 month ago

nvm is based on node but it is not node.

did you install node? and where did you install node?

did you call rfbrowser init after installing the Browser library? does this work?

How you start vscode, is node in PATH?

maybe, after installing all needed libraries/packages a restart of your computer is needed, mostly a logoff / login is enough, so that all PATHs are initialized correctly.

Leemur89 commented 1 month ago

From vscode terminal (with my venv):

$ which node
/Users/***/.nvm/versions/node/v18.19.0/bin/node
$ rfbrowser init chromium                                                                                                            [11:14:29]
2024-08-22 11:14:52,792 [INFO    ] ==============================================================================================================
2024-08-22 11:14:52,792 [INFO    ] Installing node dependencies...
2024-08-22 11:14:53,701 [INFO    ] Installing rfbrowser node dependencies at /Users/***/Documents/project/.venv/lib/python3.8/site-packages/Browser/wrapper
2024-08-22 11:14:53,869 [INFO    ] npm WARN config production Use `--omit=dev` instead.
2024-08-22 11:14:55,333 [INFO    ] 
2024-08-22 11:14:55,333 [INFO    ] added 67 packages, and audited 68 packages in 1s
2024-08-22 11:14:55,333 [INFO    ] 
2024-08-22 11:14:55,333 [INFO    ] 8 packages are looking for funding
2024-08-22 11:14:55,333 [INFO    ]   run `npm fund` for details
2024-08-22 11:14:55,334 [INFO    ] 
2024-08-22 11:14:55,334 [INFO    ] found 0 vulnerabilities
2024-08-22 11:14:55,338 [INFO    ] 
2024-08-22 11:14:55,339 [INFO    ] Installing browser chromium binaries to /Users/***/Documents/project/.venv/lib/python3.8/site-packages/Browser/wrapper
2024-08-22 11:14:55,339 [INFO    ] npx --quiet playwright install chromium 
2024-08-22 11:15:22,759 [INFO    ] |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■| 100% of 1 MiB
2024-08-22 11:15:22,820 [INFO    ] 
2024-08-22 11:15:22,820 [INFO    ] rfbrowser init completed
2024-08-22 11:15:22,820 [INFO    ] ==============================================================================================================

So yes I have node in my path and I ran rfbrowser init, I'll give a try on the reboot. edit still the same after a reboot. Also tried a global install of node using brew install node. Still the same...

d-biehl commented 1 month ago

did you try to execute the command `RobotCode: Clear Cache and Restart Language Servers"?

Leemur89 commented 1 month ago

yup multiple times

Also rfbrowser clean-node followed by rfbrowser init

d-biehl commented 1 month ago

ok, because nvm is running on the shell. did you call the nvm command before calling rfbrowser init? did you try to open vscode from terminal/shell where you call nvm before?

I do not know nvm, so that's why I'm asking. Is there a way to put you node installation to the system/userpath without starting a shell? I know this from pyenv, there is a shim folder, where you can manage the default user python, the only thing you have to do ist, to put it into the userpath. There is an nvm alias command, maybe this?

Leemur89 commented 1 month ago

If I run a python from my terminal with my venv it is working fine:

$ python
Python 3.8.19 (default, Aug 20 2024, 11:17:47) 
[Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import run
>>> run(["node", "-v"], check=True)
v18.19.0
CompletedProcess(args=['node', '-v'], returncode=0)

I suspect the issue on the PATH not being properly set up in the LSP

Leemur89 commented 1 month ago

Actually nvm is setting for me the node globally in my system, so from any terminal I got the node with the version I've set with nvm (here 18.19.0)

d-biehl commented 1 month ago

yes, because nvm is running in the .bashrc or .zshrc, but if you start vscode, vscode is started without a shell, so the nvm script is not started. And yes, if you open a terminal in vscode a full shell is started, but vscode/lsp does not know what is started in this shell.

So you need to set/update the paths with nvm before you start vscode.

Leemur89 commented 1 month ago

I closed VSCode, then launched it with the command code from a terminal as you suggested, and now it works, thanks! I just need to be careful next time, as it stays in cache: if I close VSCode and open it the classical way (from applications), it will still work because lib will be in cache. And then it will fail again on the next 'Clear Cache and reload LSP'

I think a neat improvement would be on the rfbrowser library to perform the node check only in "run" mode (there is some boolean on RF to know whether we are on dryrun/static analysis vs actual run which could be used to smartly bypass the node check), I'll try to raise an MR to them

Anyway, thank you for your quick support !

Leemur89 commented 1 month ago

FYI https://github.com/MarketSquare/robotframework-browser/pull/3758