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
Correctly change the working directory in python #296
Conversation
Thanks! Can you add a test? |
@justinmk Sure, I've had a test written anyway. |
neovim/plugin/script_host.py
Outdated
@@ -65,7 +65,9 @@ def setup(self, nvim): | |||
sys.stderr = RedirectStream(lambda data: nvim.err_write(data)) | |||
# Setup the DirChanged listener | |||
cid = nvim.channel_id | |||
nvim.command('au DirChanged * call rpcrequest(%d, "python_chdir", v:event)' % cid) | |||
cmd = 'au DirChanged * call rpcrequest(%d, "python_chdir", v:event)' \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use autocmd
instead of au
, which makes it easier go grep for when searching for autocmd-related things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
Alright, all tests ran through. Fixed the last linter warning. |
neovim/plugin/script_host.py
Outdated
@@ -63,6 +63,12 @@ def setup(self, nvim): | |||
self.saved_stderr = sys.stderr | |||
sys.stdout = RedirectStream(lambda data: nvim.out_write(data)) | |||
sys.stderr = RedirectStream(lambda data: nvim.err_write(data)) | |||
# Setup the DirChanged listener |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm maybe we can use the host autocmd infrastructure here. (We need to add a flag to not start the host for the autocmd, but that would be useable in general)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually that would require a bit bigger refactor than I thought, so let's keep this as is for now (and maybe revisit at UpdateRemotePlugins
refactor)
What is happening here? https://travis-ci.org/neovim/python-client/jobs/330770820#L512 I get this locally as well. Sometimes.
It starts happening at a8a98b8. This culprit seems to be this line: nvim.command('au DirChanged * call rpcrequest(%d, "python_chdir", v:event)' % cid) |
you mean you don't see it on master? |
neovim/plugin/script_host.py
Outdated
@@ -63,6 +63,12 @@ def setup(self, nvim): | |||
self.saved_stderr = sys.stderr | |||
sys.stdout = RedirectStream(lambda data: nvim.out_write(data)) | |||
sys.stderr = RedirectStream(lambda data: nvim.err_write(data)) | |||
# Setup the DirChanged listener | |||
cid = nvim.channel_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unnecessary temporary
neovim/plugin/script_host.py
Outdated
# Setup the DirChanged listener | ||
cid = nvim.channel_id | ||
cmd = 'autocmd DirChanged *' \ | ||
+ ' call rpcrequest(%d, "python_chdir", v:event)' \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also we prefer to use new-style .format()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I, too, prefer .format()
. I simply followed what I've seen being used before. Will change it.
Yup. I just do:
Commenting out the following line makes it work again:
|
Any |
Hmm, maybe it shouldn't be done in |
Yup, |
It is a bit racy, if a vim script does
the |
@mhinz Hmm. That is interesting. I'm using it already, and couldn't see any errors. Which nvim version are you using?
|
Hm, you're right. I can only reproduce it with master and this repo is using nightlies during CI tests. I can't reproduce it with the 0.2.2 release. I'll bisect nvim later. |
This issue starts happening after neovim/neovim@1ebc96fe1. |
Yes, it changes processing of RPC job messages, but not more than to how they were before RPC jobs gained stderr support neovim/neovim#4723 (and to how RPC sockets always have worked), so they are less "forgiving" to potentially racy patterns. Synchronous initialization should be initiated by one party only. In the case of plugin hosts, this is already nvim, by using one of |
neovim/plugin/script_host.py
Outdated
@@ -44,6 +44,14 @@ def __init__(self, nvim): | |||
exec('import sys', self.module.__dict__) | |||
self.legacy_vim = LegacyVim.from_nvim(nvim) | |||
sys.modules['vim'] = self.legacy_vim | |||
# Since the 'DirChanged' autocmd is established asynchronously, import | |||
# os manually already, to prevent a potential race condition | |||
exec('import os', self.module.__dict__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed? The autocmd handler method is executed in the context of script_host.py
which already has the import.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to work-around the race you described, by importing os before, so that Vim can see the current working directory without creating a race.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the race has nothing to do with import os
. Quick fix would rather be to do
nvim.command('call rpcrequest({}, "python_chdir", {"cwd": getcwd()}', async=True)
after registering the autocmd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bfredl What's your suggestion of implementing this? It's definitely required, we need to watch for the DirChange somehow. I've just looked at the ruby plugin, and found they're doing it quite similar. |
Lets use the current solution for now (with the fix I just posted), until we improve host autocmd registration. |
seems to be a chicken-egg problem, instead I went with this:
Pushed an update to @Leandros branch, will merge after CI. |
This release adds support for Python 3.7 which now is in beta. `async` is a keyword in python3.7, and it should be replaced with `async_` when used as a keyword argument to API methods. `async` is still supported with python 2.7 and 3.6 for the moment, but considered deprecated. Also, integration with the in-process lua interpreter in Nvim is now supported. Changes since 0.2.4: * debcde0 clean up remote object implementation (#313) * e880fe7 Guidelines for local plugin development (#317) * 1ab98e8 Update the working directory on DirChanged for legacy plugins (#296) * d53415d Fix SyntaxError in py3.7: use `async_` instead of `async` (#274) * b65f62d Use `pytest` as `nosetests` are not longer maintained (#266) * d9aed96 Support using lua functions. `buf.update_higlights` as an internal example (#325)
This appears to cause problems with initial use of |
This is implementing #295 and neovim/neovim#3636.