shinyypig / matlab-in-vscode

A vscode extension for matlab.
MIT License
45 stars 4 forks source link

bug: When launched from extension, bad path is given and matlab session cannot be made. #5

Closed anakinsleftleg closed 1 year ago

anakinsleftleg commented 1 year ago

I've successfully installed the Matlab API Engine for Python. It works manually run, it works with the other Matlab terminal vscode extension, and if I manually run the matlab_engine.py script from this extension as a file, it works too. But running from the extension itself in vscode fails.

I'm in windows 10 with the latest (April 2023) VScode version with matlab 2022b.

The issue I believe is in the below linked line. The path of the current terminal window context is being used as the beginning path for the location of the matlab_engine.py script which is wrong, and it's also not escaping the folder separators correctly. When i click the double arrow to run the m-file in matlab, this is what I get:

anakin@mustafar MINGW64 ~/Documents/Projects/Software/App (master)
$ python c:\Users\anakin\.vscode\extensions\shinyypig.matlab-in-vscode-0.3.2\pybackend\matlab_engine.py --cmd="""fprintf('Working directory: %s\n', pwd);"""
C:\Program Files\Python310\python.exe: can't open file 'C:\\Users\\anakin\\Documents\\Projects\\Software\\App\\Usersanakin.vscodeextensionsshinyypig.matlab-in-vscode-0.3.2pybackendmatlab_engine.py': [Errno 2] No such file or directory

So you can see there is some issue with the path in the command to start the script run through the extension.

https://github.com/shinyypig/matlab-in-vscode/blob/65a936a6b3afdd4e37b6fd4605afdf4890bf4a03/src/extension.ts#L36

https://github.com/shinyypig/matlab-in-vscode/blob/65a936a6b3afdd4e37b6fd4605afdf4890bf4a03/src/extension.ts#L49

shinyypig commented 1 year ago

Thank you for pointing out this bug, I will fix this as soon as possible.

p.s. Due to my recent business trip, I also welcome you to create a pull request.

anakinsleftleg commented 1 year ago

Thanks, I'm not adept at the extension code, I just have an idea how to read it. I can wait until you are able to address it, but thank you for being open to it!

shinyypig commented 1 year ago

I tried to run it on my Windows computer and there were no errors. My system version is Windows 11.

anakin@mustafar MINGW64 ~/Documents/Projects/Software/App (master)
$ python c:\Users\anakin\.vscode\extensions\shinyypig.matlab-in-vscode-0.3.2\pybackend\matlab_engine.py --cmd="""fprintf('Working directory: %s\n', pwd);"""
C:\Program Files\Python310\python.exe: can't open file 'C:\\Users\\anakin\\Documents\\Projects\\Software\\App\\Usersanakin.vscodeextensionsshinyypig.matlab-in-vscode-0.3.2pybackendmatlab_engine.py': [Errno 2] No such file or directory

The .py file locates in

c:\Users\anakin\.vscode\extensions\shinyypig.matlab-in-vscode-0.3.2\pybackend\matlab_engine.py,

why the terminal returns that

C:\\Users\\anakin\\Documents\\Projects\\Software\\App\\Usersanakin.vscodeextensionsshinyypig.matlab-in-vscode-0.3.2pybackendmatlab_engine.py?

If you have time, please run the following code in terminal and tell me what the result is.

python c:\Users\anakin\.vscode\extensions\shinyypig.matlab-in-vscode-0.3.2\pybackend\matlab_engine.py
anakinsleftleg commented 1 year ago

I'm in windows 10. I think the issue is the command is being executed without quotes around the path:

anakin@mustafar MINGW64 ~/Documents/Projects/Software/FFOVMTF (master)
$ python c:\Users\anakin\.vscode\extensions\shinyypig.matlab-in-vscode-0.3.2\pybackend\matlab_engine.py
C:\Program Files\Python310\python.exe: can't open file 'C:\\Users\\anakin\\Documents\\Projects\\Software\\FFOVMTF\\Usersanakin.vscodeextensionsshinyypig.matlab-in-vscode-0.3.2pybackendmatlab_engine.py': [Errno 2] No such file or directory

anakin@mustafar MINGW64 ~/Documents/Projects/Software/FFOVMTF (master)
$ python "c:\Users\anakin\.vscode\extensions\shinyypig.matlab-in-vscode-0.3.2\pybackend\matlab_engine.py"
---------------------------------------------------------
MATLAB Engine for Python is ready (terminate with 'quit')
---------------------------------------------------------
>>> quit
Terminating MATLAB Engine.

That seems to be the issue. When the command is run from the VS Code extension icons, it opens the terminal and does not include quotes around the file path. So double quotes around the path to the extension script seems to do the trick and the startup commands after that work without any modification.

shinyypig commented 1 year ago

Fixed in https://github.com/shinyypig/matlab-in-vscode/commit/5b87be3e09f9775179fad08f55ac2c0c868b03ca, please update the extension.

Thank you for pointing out this bug.

anakinsleftleg commented 1 year ago

yea looks like it works now, thank you!