lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.41k stars 389 forks source link

Windows 8 - Latexmk PID #154

Closed jbkopecky closed 9 years ago

jbkopecky commented 9 years ago

Hello!

Thank you for this great plugin !

I am not sure about this one, but I get some errors when I start Latexmk compilation from my Gvim on Windows 8.

vimtex_error_pid

It looks like latexmk still can compile the .tex but I think it creates a new latexmk process instead of catching an existing one (when I stop and start continuous compiling several times, I end up with mutliple latexmk processes running)

I did have a look at the function that I think caused this error: s:latexmk_set_pid() at line 428 in latexmk.vim.

Could the bug come from the line 431 ?:

let pidinfo = systemlist(pidcm)[-1] 

I tried to run the command qprocess latexmk.exe from my windows command prompt (if I understand well, maybe it should have been something like system(pidcm) instead of systemlist(pidcmd) but I am afraid this doesnt get me any pid info output from any latxmk process.

Then, should I try to run something like:

let pidcmd = 'tasklist /v /fo csv | findstr /i "latexmk.exe"'
let pidinfo = system(pidcmd)

and then parse the answer to get latexmk process pid ?

I am not very good at vimscript or windows commands, but do you think this could be the issue I am having ?

Thank you for your help !

JB

lervag commented 9 years ago

Hi. It seems you are experiencing two different problems. The first one is related to the viewer settings, while the second is related to the PID. The latter seems to be the important one. I think the first one may arise if you for instance have disabled the viewer module?

I don't personally use windows, and so windows debugging is difficult for me. I rely on other users to make this work. However, I will try to help: It seems to me that you are right that qprocess latexmk.exe is the problem here. It should be easy to verify: In a cmd shell, first start latexmk for a tex project, then in a separate cmd shell test the output of qprocess latexmk.exe. It should return the pid. If it does not, then this is indeed your problem.

If qprocess does not work, and if you find another windows shell command that does work, similar to what you suggest, then please let me know. Also see #15.

I'll be rude enough to notify @petobens and @Konfekt, who was very helpful in implementing support for windows.

jbkopecky commented 9 years ago

Yep, I have disabled the viewer module.

I was more worried about the PID issue as well. I am sorry I just saw the #15 issue.

I do not get any result from the cmd shell when running qprocess latexmk.exe.

I do not know if this is because of my computer config or a Windows 8 issue.

I have had better results with Tasklist, running: tasklist /v /fo csv | findstr /i "latexmk.exe" that gives me an output that includes a latexmk PID (but haven't figured out how to properly parse it in vimcscript to isolate the PID).

I don't know if tasklist is compatible with all windows versions though and it seems this command is a little slow...

Also, is the function systemlist() a vim builtin function ?

petobens commented 9 years ago

@lervag @jbkopecky Unfortunately I cannot provide any help since I don't use the latexmk functionality (because I use arara to compile my documents).

Konfekt commented 9 years ago

Hm. It's me who changed the command line to obtain the process ID to use qprocess whereas before it was wmic process and not working correctly for me under Windows 7 (as still existant in LaTeX BoX, see https://github.com/LaTeX-Box-Team/LaTeX-Box/blob/64eeb2dd9a0afd33ed8610a2d07416d126d5a2ef/ftplugin/latex-box/latexmk.vim#L333). One reason for qprocess was that it is compatible with more Windows versions, starting from Windows XP Home.

lervag commented 9 years ago

@Konfekt Do you have any idea why qprocess does not work for @jbkopecky on Windows 8.1?

It is difficult to find any information on qprocess, but this seems to be related. @jbkopecky Does query process latexmk.exe provide any output?

systemlist() is an internal vim function, yes. You can read the docs with :h systemlist. I think, though, that this function was introduced in a relatively recent version of vim, so it is interesting to know which version of vim you are using.

lervag commented 9 years ago

Btw: Could you please verify that my fix for the viewer related error message works?

jbkopecky commented 9 years ago

So here are the results:

pid_issue

lervag commented 9 years ago

First, could you check what happens when you in vim on windows do the following?

:echo systemlist('echo Hello World')

If systemlist is not available, then this should give a similar error as before.

Regarding query process, could the problem be that you do not have administrator access? Here it says "Administrators have full access to all query process functions", which could imply that query process is not available without administrator rights. See also here and here and here.

I'm sorry to say that I have no idea why this does not work for you. Perhaps the problem is mainly or only the lack of systemlist?

jbkopecky commented 9 years ago

Ok, so in fact I have two issues I guess:

:echo systemlist('echo Hello World')

leads me to an Unknown function error. I tried to reinstall vim from here but it was compiled in 2013 so I guess no systemlist function yet.

The problem is that even when I fire up vim in admin mode, and run

:echo system('qprocess latexmk.exe') 

it doesn't seem to run this command with admin rights...

jbkopecky commented 9 years ago
jbkopecky commented 9 years ago

Last Update: In fact, having systemlist working made the error disappear as you said. I can now compile without seeing any issue. I guess Vimtex is only compatible with latest windows Gvim then.

However, I think that it did not fix the PID catch problem (due to admin rights limitations on Windows8) because everythime I toggle continuous compilation using <leader>ll, it only starts it (and never stops it) and creates a new latexmk process every time.

lervag commented 9 years ago

Good to hear that things are starting to work.

Does your windows user generally have admin rights? If not, perhaps giving yourself admin rights will fix this problem?

@Konfekt @petobens Do you guys understand this thing? I'm really not very good with such kinds of windows issues...

Konfekt commented 9 years ago

For explanation: I switched from wmic process to qprocess because the former would not return a PID correctly on Windows 7, so that the second LatexMk instance would override the first. That is, with wmic only a single LatexMk instance would work.

Unfortunately there's little documentation about qprocess. Perhaps it has been abandoned in Windows 8 together with the switch from cmd to PowerShell as default shell?

lervag commented 9 years ago

@jbkopecky has confirmed that qprocess (an alias to query process) is available, but only when it is run as administrator. The question is then how to ensure that the command is run as administrator.

However, this issue also raises the question if there should be a fallback method to get the PID. I.e. if qprocess fails, then perhaps I should add a second method that does not need administrator rights?

jbkopecky commented 9 years ago

How about tasklist see here and here and here? Is it compatible with all windows versions ? It works for me ! A wmic fallback sounds good, I have had a look at Tpope Vim Dispatch and it looks like he is using wmic too.

lervag commented 9 years ago

Does wmic work for you?

Does tasklist have the same problem as that mentioned by @Konfekt?

jbkopecky commented 9 years ago

I did a quick revert to try wmic and it works for me (except that the cmd windows steals focus for a moment) I can start and stop compilation (i.e. it catches the appropriate PID)

About tasklist, I am not sure to undesrtand the problem mentioned, so I can't really say. Are we trying to get several compilations cohexist without stealing each others latexmk processes ?

Konfekt commented 9 years ago

Yes. If wmic works on Windows 8 but qprocess not, than better revert to wmic. However, under Win 7 the code as it used to be, would steal latexmk processes (as it did not read the PID correctly).

lervag commented 9 years ago

Ok, then I will add fallback code to use wmic if qprocess fails. I think this should work in most cases.

lervag commented 9 years ago

I just realized I have no way of testing. I think the following lines should work:

let pidcmd = 'wmic process where '
      \ . '(CommandLine LIKE "latexmk\%' . a:data.tex . '\%") '
      \ . 'get ProcessId /value | find "ProcessId"'
let a:data.pid = strpart(systemlist(pidcmd)[0], 10)

It would be very helpful if someone could test this. @jbkopecky

Also, @jbkopecky, it would be interesting to see if an alternative with tasklist also works. Could you test and if needed modify the lines you suggested earlier, that is,

let pidcmd = 'tasklist /v /fo csv | findstr /i "latexmk.exe"'
let pid = split(systemlist(pidcmd)[-1], '\s\+')[-2]

so that the above code correctly gets the PID. This might lead to two possible fallbacks, which is better than one.

Also: It would be very helpful if you could let me know what the return value of systemlist(pidcmd)[-1] is for the original qprocess that does not work.

jbkopecky commented 9 years ago

All right I'll have a look at it as soon as I get back on my Windows machine (monday)

jbkopecky commented 9 years ago

Hello, sorry for the wait:

let pidcmd = 'wmic process where '
      \ . '(CommandLine LIKE "latexmk\%' . a:data.tex . '\%") '
      \ . 'get ProcessId /value | find "ProcessId"'
let a:data.pid = strpart(systemlist(pidcmd)[0], 10)

does not work for me. It gives me the following error (not very helpfull though):

error1

The previous vimtex wmic command works for me though. I am not sure why this one fails.

let pidcmd = 'tasklist /fi "imagename eq latexmk.exe"'
let pid = split(systemlist(pidcmd)[-1], '\s\+')[1]

For example: In cmd console, the pidcmd returns:

  1. For one latexmk process running: tasklist_on_process The vim commands returns the only PID: 1172
  2. For two processes running: tasklist_two_processes The vim command returns the last line pid: 7820 in this case.

I guess the lines are ordered by time of process creation so It works fine with me when I use tasklist command in vimtex. Could it be possible to test this pid command with different versions of windows ?

qprocess_error

in cmd console. Therefore, the resulting output for systemlist(pidcmd)[-1] in vim is: operable program or batch file.

Hope it helps !

jbkopecky commented 9 years ago

Also, is it ok to replace

systemlist(pidcmd)

by:

split(system(pidcmd), '\n')

to avoid breaking compatibility with older vim versions ?

lervag commented 9 years ago

Please test if the latest commit fixes the compatibility issue and that it doesn't break anything.

lervag commented 9 years ago

The previous vimtex wmic command works for me though. I am not sure why this one fails.

Could you please be explicit on which wmic command works for you? I don't think there was ever committed any wmic commands to vimtex...

@Konfekt @jbkopecky Regarding qprocess: If I understand correctly from the earlier discussion, qprocess is only available when run as administrator. Could you please verify this? If this is the case, then qprocess is probably a bad choice for the default program to get the PID, since normal users should probably not generally have administrator rights?

@Konfekt I've pushed a change from qprocess to tasklist. Does the tasklist version work for you? Do you know a reason why not to use tasklist (if it works)?

Konfekt commented 9 years ago

For my part, tasklist is just fine and I chose qprocess only because it was the most prevalent among tasklist et al. (available from XP Home edition and above); to boot the previous wmprocess command was as cryptic as nonfunctional .

Since then I switched to OpenSuse, thus I cannot confirm that qprocess requires administrator rights because all online documentation was not clear on this. (Of Couse, when using vimtex on Windows 7, my user account had administrator rights.)

lervag commented 9 years ago

@Konfekt OK. Thanks for your input, anyway. If the latest version fixes the issue for @jbkopecky, and if it does not introduce any problems for other users, then I think this issue may be resolved.

jbkopecky commented 9 years ago

It works for me ! Thanks !

lervag commented 9 years ago

Great!