Skip to content

fix Use unsaved contents when reference imports disappear #2194 - #5655

Open
asukaminato0721 wants to merge 1 commit into
pyrefly-org:mainfrom
asukaminato0721:2194-unsaved-references
Open

asukaminato0721 wants to merge 1 commit into
pyrefly-org:mainfrom
asukaminato0721:2194-unsaved-references

Conversation

@asukaminato0721

Copy link
Copy Markdown
Collaborator

Summary

Fixes part of #2194

A removed import can leave a filesystem reverse dependency without a memory counterpart in the dependency graph.

Search the editor contents instead of returning stale disk references, while preserving the definition module used by local lookup.

  • I am a human, and I fully understand the code I am submitting.

Test Plan

add test

A removed import can leave a filesystem reverse dependency without a memory counterpart in the dependency graph. Search the editor contents instead of returning stale disk references, while preserving the definition module used by local lookup.
@stroxler stroxler self-assigned this Nov 16, 2026

@stroxler stroxler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Breaking this up is a big help.

My AI reviewer thinks there's an edge case in here - it's concerned about cyclic import handling. Could you look into this and see if it makes sense to fix?

I'm not super concerned IDE methods being fully edge-case proof, on the other hand we should probably try to avoid any known issues in import cycles.

This needs a rebase regardless, since we had that outage importing diffs


The defining-file exception restores stale references in a cyclic import.

Location: pyrefly/lib/state/lsp.rs:5843–5847, especially handle.path() != definition.module.path().

Saved files used in the executed counterexample:

# definitions.py
from usage import marker
def target() -> None:
    pass
target()
# usage.py
from definitions import target
marker = 1
target()

Analyze both, then open definitions.py and delete its final target() without saving. Query global references for the filesystem definition of target, excluding the declaration. A reference request started from the imported target in usage.py resolves to this filesystem definition as well.

Expected: only the import and call in usage.py. The current definitions.py contains no use.

Actual on the PR: those two references plus the deleted line-4 call from the filesystem definitions.py. The returned module is explicitly filesystem-backed. The same supplementary assertion passes on the base and fails on the PR.

The memory definition imports usage, which imports the filesystem definition, so the memory definition is already in the reverse-dependency closure. Base therefore removes its filesystem counterpart. The replacement logic exempts the filesystem definition, retaining the stale module and its old local reference.

Contract and impact: LSP 3.17 didOpen makes open-document contents client-managed and says the server must not read them through the URI. A reference absent from that synchronized buffer is not a precision limitation. Rename uses this same lookup with textual-reference options, exposing the client to edits based on obsolete text/ranges; the executed test validates reference discovery, not a client-side rename application.

Why correction belongs in this PR: this particular false-positive result is newly introduced by the changed exception. Documentation or a later broader buffer-visibility project would not prevent the regression. Preserve local-definition semantics without restoring a shadowed disk candidate, and add a buffered-definition cyclic regression test. This does not require redesigning cross-module typing or supporting arbitrary dynamic Python behavior.

Non-blocking / scope boundaries: stale defining-file behavior without a cycle already exists on the base. It is not a second regression or a reason to expand this PR’s scope. The fix must avoid extending it to a case that previously suppressed the stale result.

This branch has not been deployed

No deployments
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

Successfully merging this pull request may close these issues.

2 participants