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

How check if the command succeeded? #339

Closed
jclsn opened this issue Mar 30, 2023 · 20 comments
Closed

How check if the command succeeded? #339

jclsn opened this issue Mar 30, 2023 · 20 comments

Comments

@jclsn
Copy link

jclsn commented Mar 30, 2023

Is there any way to see if the build (or any other command) succeeded? It is not clear to me from the readme. I would like to use :Make to build and start debugging afterwards, but only when the build was successful of course.

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

For programmatic use, dispatch#request().status will give you the exit status of the most recent build.

@tpope tpope closed this as completed Mar 30, 2023
@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

Thanks. I get a "status is not available in dictionary" when trying this though

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

OK

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

Maybe I should be more clear. This is what I am trying

function! s:BuildAndDebug()
	:Make

	if dispatch#request().status
		autocmd QuickFixCmdPost make ++once call vimspector#Launch()
	endif
endfunction

Can you tell me what I am doing wrong?

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

:Make is asynchronous. You're checking the exit status before it has exited.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

Right, I could have though of that. I got an output now, but there is nothing like "status". There is "completed" though, but that is always 1, if the build fails or not. This is shown as success or failed though.

:!cmake --build build -- (job/36615)
{'pid': 36615, 'background': 1, 'action': 'make', 'job': 'process 36615 run', 'program': 'cmake --build build --', 'command': 'cmake --build build --', 'file': '/tmp/vLZNAVp/67', 'id': 63, 'handler': 'job', 'mods': '', 'directory':
 '/home/jan/Workspace/C++/constexpr', 'format': '%*[^"]"%f"%*\D%l: %m,"%f"%*\D%l: %m,%-G%f:%l: (Each undeclared identifier is reported only once,%-G%f:%l: for each function it appears in.),%-GIn file included from %f:%l:%c:,%-GIn f
ile included from %f:%l:%c\,,%-GIn file included from %f:%l:%c,%-GIn file included from %f:%l,%-G%*[ ]from %f:%l:%c,%-G%*[ ]from %f:%l:,%-G%*[ ]from %f:%l\,,%-G%*[ ]from %f:%l,%f:%l:%c:%m,%f(%l):%m,%f:%l:%m,"%f"\, line %l%*\D%c%*[^
 ] %m,%D%*\a[%*\d]: Entering directory %*[`'']%f'',%X%*\a[%*\d]: Leaving directory %*[`'']%f'',%D%*\a: Entering directory %*[`'']%f'',%X%*\a: Leaving directory %*[`'']%f'',%DMaking %*\a in %f,%f|%l| %m', 'expanded': 'cmake --build
build --', 'title': 'make', 'args': ''}
Success: !cmake --build build -- (job/36615)

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

'job': 'process 36615 run'

That's the job that is running. If the job is running, then it hasn't exited, and there won't be a status. You will need to check during the autocommand.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

Well, the code that produced this output, is this

command! Debug call BuildAndDebug()<CR>

function! BuildAndDebug()
	:Make!
	autocmd QuickFixCmdPost make ++once call RunVimspectorIfBuilt()
endfunction

function! RunVimspectorIfBuilt()
	echo dispatch#request()
endfunction

I would say this is during the autocommand. If I don't run :Make in the background, the process is shown as "dead".
In any case, there is no status. Would be nice if dispatch could send some event when it has finished successfully.

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

Oh, Dispatch fires the QuickFixCmdPost make event fires twice, once after the job starts, and another after the job finishes. This was to support use cases where cleanup needs to be done before returning control to the user. So you'll need to get a bit more clever than ++once.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

If I don't use once, Vimspector will be started every time I build. I did succeed doing this using this CMake plugin , but it doesn't kill backround jobs properly. Would also be nice to have a universal solution for all programming languages. Not so easy apparently.

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

If I don't use once, Vimspector will be started every time I build.

If you don't use ++once, the autocommand will run every time you build. You can use code in that autocommand to start Vimspector under the right circumstances. For example, you could set a variable before calling :Make, and then check for and unset that variable in the autocommand.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

I can still not even get the value for status. I am tired. It is a bit late here. Will try again tomorrow.

I still think that it would make things much simpler if this plugin would fire events when the task is successful or not. This could easily be followed by an autocmd.

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

Another thing I just noticed is you need to use :Make, not :Make!, to get the autocommand to run on completion.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

I tried both. There is no status value ever

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

augroup my_debug
  autocmd!
  autocmd QuickFixCmdPost make call RunVimspectorIfBuilt()
augroup END

command! Debug call BuildAndDebug()

function! BuildAndDebug()
  let g:debug_pending = 1
  Make
endfunction

function! RunVimspectorIfBuilt()
  if !exists('g:debug_pending') || !has_key(dispatch#request(), 'status')
    return
  endif
  unlet! g:debug_pending
  echomsg 'Exited with status ' . dispatch#request().status
endfunction

That'll be $200.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

There is nothing being printed for me. I checked the message history multiple times.

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

You copied the full example including the augroup? I don't know what else to tell you. It definitely works.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

Yeah, the whole thing. For me the function always returns because there is no key called status. Weird. Anyway, thanks for trying

@tpope
Copy link
Owner

tpope commented Mar 30, 2023

This is the correct way to get the status:

let status = +readfile(dispatch#request().file . '.complete', 1)[0]

That will fail with ENOENT if the request is running, so change the initial guard to get(dispatch#request(), 'complete').

The status key was actually added in PR #313, which I had checked out at the time, hence my confusion. I'll waive the $200 charge.

@jclsn
Copy link
Author

jclsn commented Mar 30, 2023

Ah yeah that finally works. Now I can finally rest in peace. I'll recommend you!

Repository owner deleted a comment from camilo863 Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants