Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin can't execute bat file command under Windows OS with error "The system cannot find the file specified" #2261

Open
mquan86 opened this issue Dec 21, 2022 · 2 comments
Labels
bug needs triage Windows Issue that is *specific* for Windows

Comments

@mquan86
Copy link

mquan86 commented Dec 21, 2022

Describe the bug
When insert UML diagram, it will fail to generate image with error "The system cannot find the file specified"
It is because Python has a problem to detect the command file with .bat extension

I believe it is this part in applications.py:

if os.name == 'nt':
# http://code.activestate.com/recipes/409002/
info = subprocess.STARTUPINFO()
try:
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
except AttributeError:
info.dwFlags |= 1 # STARTF_USESHOWWINDOW = 0x01
p = subprocess.Popen(argv,
cwd=cwd,
stdout=open(os.devnull, 'w'),
stderr=subprocess.PIPE,
startupinfo=info,
bufsize=4096,
#~ close_fds=True
)

Should add shell = True to compatible for Windows?
Reference of similar issue in another repo: mikitex70/plantuml-markdown#11

To Reproduce

  1. Windows OS, installed PlantUML, install PlantUML plugin (https://gitlab.com/Voyvode/zim-umldiagrameditor)
  2. Has plantuml.bat in Zim application folder (so the plugin can call plantuml command)
  3. In Zim, Insert an UML
  4. Error "The system cannot find the file specified"

Logs:

INFO: Running: ('plantuml', 'C:\\Zim\\tmp\\zim-zizy_qsp\\umldiagram.png', 'C:\\Zim\\tmp\\zim-zizy_qsp\\umldiagram.puml') (cwd: None)
DEBUG: Running ErrorDialog
DEBUG: The system cannot find the file specified

Expected behavior
Should no error and able to execute command of bat file.

Screenshots
image

Environment (please complete the following information):

@introt introt added the Windows Issue that is *specific* for Windows label Dec 21, 2022
@Toggoren
Copy link
Contributor

Toggoren commented Dec 21, 2022

How to fix:

Easy path:

  1. remove .bat
  2. install plantuml via Chocolatey

Not easy path:

  1. remove .bat
  2. install AutoIt
  3. compile this script
#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

RunPlantUML()

Func RunPlantUML()
   Local $iPID = Run(@comspec & StringFormat(" /C java -jar ""%s"" %s", "!!!PUT THERE FULL PATH TO plantuml.jar!!!", $CmdLineRaw), "", @SW_HIDE, BitOR($STDERR_CHILD, $STDOUT_CHILD))
   ProcessWaitClose($iPID)

   Local $sOutput = ""
   While 1
	  $sOutput &= StdoutRead($iPID)
	  If @error Then
		 ExitLoop
	  EndIf
	  MsgBox($MB_SYSTEMMODAL, "Stdout Read:", $sOutput)
    WEnd

   $sOutput = ''
   While 1
	  $sOutput &= StderrRead($iPID)
	  If @error Then
		 ExitLoop
	  EndIf
	  MsgBox($MB_SYSTEMMODAL, "Stderr Read:", $sOutput)
   WEnd
EndFunc   ;==>RunPlantUML
  1. add resulting executable to PATH system variable
  2. if it didn't work (even after restarting the computer), then go back to "Easy path:"

Current situation:
umldiagrameditor: dotcmd = ('plantuml') passes through
Zim Desktop Wiki: Application.run
Python: subprocess.Popen
WinAPI: CreateProcessW
and pushed into lpCommandLine
Then, according to the documentation ( If the file name does not contain an extension, .exe is appended),
a .exe is added to it

My thoughts:

  1. In theory, the plugin should itself look for the executable file it needs, because it is impossible to know which of the existing executable files it needs to work.
    You can use shutil.which to search.
  2. By the way, it is possible to replace Application._lookup with shutil.which almost completely except for the part with _CAN_CALL_FLATPAK_HOST_COMMAND

@mquan86
Copy link
Author

mquan86 commented Dec 21, 2022

@Toggoren
I actually prefer a solution that doesn't depend on third party one like chocolatey or autoit, as I prefer a portable thing. Also fix that may fix many similar issues as well not just PlantUML in my particular case.
I also notice that in tryexec(), they support scan with extension, then there should be no reason not to fix the run() to execute it properly.

in tryexec/_lookup function:

elif os.name == 'nt':
# Check executable extensions from windows environment
extensions = _split_environ_list(os.environ.get('PATHEXT', '.com;.exe;.bat;.cmd'))
for dir in _split_environ_list(os.environ.get('PATH')):
for ext in extensions:
file = SEP.join((dir, cmd + ext))
if os.path.isfile(file) and os.access(file, os.X_OK):
return file
else:
return None

A note about the workaround above, I didn't test it yet, but I think it may most likely doesn't work strange away if there is executable in PATH environment. The reason is either Python or Zim doesn't actually work well with the PATH in Windows for some reason. Usually, I have to put the thing into Zim folder to make it works. I have a serval case like that already.

About the workaround, it is actually simple that I can just modify the plugin python file to call run with argument plantuml.bat instead of plantuml.

		# Call PlantUML
		try:
			#dot = Application(dotcmd)
			dot = Application('plantuml.bat')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs triage Windows Issue that is *specific* for Windows
Projects
None yet
Development

No branches or pull requests

3 participants