Project.validate() can't detect content changes served by a custom fscommands
維護者通常 7 天內回覆
還沒有人認領這個 Issue。
評估
研究方向
Start from the reproduction script in the issue, which shows that Project.validate() keeps the cached module after an fscommands read() override changes the content. Read ChangeIndicator, which compares os.path.getmtime() and os.path.getsize(), and the Project.validate() path that uses it. The issue offers two designs (a forced invalidation for one resource, or an optional change-indicator hook on fscommands), so the maintainers need to choose one before a patch. Done means the cached module reflects the overlay text after validate(), with existing fscommands implementations still working.
由索引模型根據 Issue 內容生成。
描述
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
import pathlib
import tempfile
from rope.base import fscommands
from rope.base.project import Project
class Overlay(fscommands.FileSystemCommands):
"""Serve unsaved editor buffers, fall back to the disk."""
def __init__(self):
self.buffers = {}
def read(self, path):
if path in self.buffers:
return self.buffers[path].encode()
return super().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())
Output on master (46ebf37), Python 3.13:
['f']
['f']
def g():
return 2
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 defaultautomatic_soa=Trueit 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)orproject.validate(resource, force=True). This only needs to notify the resource observers, without the analysis thatreport_change()adds. - An optional hook, for example
fscommands.get_change_indicator(path), whichChangeIndicatoruses when the fscommands object provides it, and falls back toos.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.
Editor information
- Python 3.13
- Rope master 46ebf37 (also 1.14.0)
- Context: rope-language-server issue #19
- 主要語言
- Python
- 星號
- 2.2k
- 分支
- 197
- 平均合併
- 2 天 13 小時
- 30 天內合併 PR
- 5
環境準備
- 沒有 Dockerfile 或 Docker Compose 檔案
- 有 Pull Request 範本
- 閱讀貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
python-rope/rope 的其他 Issue
-
難度 1/5 1-3 小時 新手友好度 88/100
python-rope/rope#927 ·
維護者通常 7 天內回覆
-
難度 2/5 1-3 小時 新手友好度 78/100
python-rope/rope#926 ·
維護者通常 7 天內回覆
-
simplify.real_code blanks t-strings, so renames inside them edit the wrong offset可能已有人在做 @sean0407 於 10 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 83/100
python-rope/rope#913 ·
維護者通常 7 天內回覆
-
Extract method/variable and introduce parameter don't check whether the new name is already in use未關閉
難度 3/5 1-2 天 新手友好度 62/100
python-rope/rope#924 ·
維護者通常 7 天內回覆
-
難度 4/5 3-5 天 新手友好度 35/100
python-rope/rope#923 ·
維護者通常 7 天內回覆
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 82/100
microsoft/markitdown#2820 ·
維護者通常 1 天內回覆
-
bug
難度 2/5 1-3 小時 新手友好度 66/100
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
yt-project/yt#5624 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 72/100
-
bug
難度 2/5 1-3 小時 新手友好度 82/100
NousResearch/hermes-agent#148376 ·
維護者通常 1 天內回覆