You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Project.validate() can't detect content changes served by a custom fscommands #925
A custom fscommands.read() lets an editor or a language server give Rope the contents of unsaved buffers. pylsp-rope's WorkspaceFileCommands does this, and so will rope-language-server. Rope caches parsed modules, though, and Project.validate() decides whether a file changed only from ChangeIndicator, which calls os.path.getmtime() and os.path.getsize() on the real file. An edit to a buffer changes neither, so Rope keeps analysing the old module even after validate().
To Reproduce
importpathlibimporttempfilefromrope.baseimportfscommandsfromrope.base.projectimportProjectclassOverlay(fscommands.FileSystemCommands):
"""Serve unsaved editor buffers, fall back to the disk."""def__init__(self):
self.buffers= {}
defread(self, path):
ifpathinself.buffers:
returnself.buffers[path].encode()
returnsuper().read(path)
root=pathlib.Path(tempfile.mkdtemp())
(root/"m.py").write_text("def f():\n return 1\n")
overlay=Overlay()
project=Project(str(root), ropefolder=None, fscommands=overlay)
resource=project.get_resource("m.py")
print(sorted(project.get_pymodule(resource).get_attributes()))
overlay.buffers[resource.real_path] ="def g():\n return 2\n"project.validate(resource)
print(sorted(project.get_pymodule(resource).get_attributes()))
print(resource.read())
resource.read() returns the new text, but the cached module still has f and not g. Refactorings then compute offsets against text the user no longer has.
Workarounds today
pylsp-rope calls the private project.pycore._invalidate_resource_cache(resource) for every open document on every request.
libutils.report_change() does invalidate, but the documentation describes it as the call to make after saving, and with the default automatic_soa=True it also runs static object analysis on every call. That's too slow to run on every keystroke.
Proposal
Either of these would be enough, and neither changes the existing fscommands contract:
A public way to invalidate one resource, for example project.invalidate(resource) or project.validate(resource, force=True). This only needs to notify the resource observers, without the analysis that report_change() adds.
An optional hook, for example fscommands.get_change_indicator(path), which ChangeIndicator uses when the fscommands object provides it, and falls back to os.stat() otherwise. An overlay could then return the buffer version.
The second option is close to part of #911, but kept optional, which matches the concern raised in the review of #911 that new fscommands methods must not break existing implementations such as pylsp-rope's.
I think report_change() might be the correct place here. If you have an fscommand redirecting file accesses to the editor's unsaved buffer, I think for as far as rope is concerned, the state of the file in the editor buffer is what's relevant for any file operations. It's essentially the same as auto-saving editor.
The issue is just performance when report_change() might be doing SOA on every keystroke and that's expensive. Maybe what we needed is a way to tell report_change() to skip SOA or we can implement a debouncer for report_changes() so that SOA only runs when the user stopped typing.
Automatic SOA should probably run as a background thread in the first place.
Activity
lieryan commentedon Nov 10, 2026
I think
report_change()might be the correct place here. If you have an fscommand redirecting file accesses to the editor's unsaved buffer, I think for as far as rope is concerned, the state of the file in the editor buffer is what's relevant for any file operations. It's essentially the same as auto-saving editor.The issue is just performance when
report_change()might be doing SOA on every keystroke and that's expensive. Maybe what we needed is a way to tellreport_change()to skip SOA or we can implement a debouncer forreport_changes()so that SOA only runs when the user stopped typing.Automatic SOA should probably run as a background thread in the first place.