Skip to content

Comments

[ty] Teach the resolver that vendored copies of typeshed in the ty cache are in fact typeshed#21012

Closed
Gankra wants to merge 1 commit intomainfrom
gankra/goto-typeshed
Closed

[ty] Teach the resolver that vendored copies of typeshed in the ty cache are in fact typeshed#21012
Gankra wants to merge 1 commit intomainfrom
gankra/goto-typeshed

Conversation

@Gankra
Copy link
Contributor

@Gankra Gankra commented Oct 21, 2025

Fixes astral-sh/ty#1054

I've confirmed it works on my machine in vscode, but it's not clear to me if there's a good way to test this in our test suite.

@Gankra Gankra added the server Related to the LSP server label Oct 21, 2025
@Gankra Gankra added the ty Multi-file analysis & type inference label Oct 21, 2025
Comment on lines +737 to +751
pub fn vendored_path_for_cache(path: &VendoredPath) -> SystemPathBuf {
SystemPathBuf::from(format!(
"vendored/typeshed/{}/{}",
// The vendored files are uniquely identified by the source commit.
ty_vendored::SOURCE_COMMIT,
path.as_str()
))
}

/// Get the full `SystemPath` in the cache that a `VendoredPath` refers to
pub(crate) fn cached_vendored_path(db: &dyn Db, path: &VendoredPath) -> Option<SystemPathBuf> {
let writable = db.system().as_writable()?;
let system_path = vendored_path_for_cache(path);
Some(writable.cache_dir()?.join(system_path))
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We invoke these a lot now, so they might need to be cached...

@github-actions
Copy link
Contributor

Diagnostic diff on typing conformance tests

No changes detected when running ty on typing conformance tests ✅

@github-actions
Copy link
Contributor

mypy_primer results

No ecosystem changes detected ✅
No memory usage changes detected ✅

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Could we achive the same if the LSP registered an additional search path for the cached vendored files (that comes after the standard library?) That would require less custom casing for which we have not tests for.

Another alternative would be to remap cached vendored file path to the corresponding vendored path within the LSP request handlers.

Could any of those solution lead to problems if the content of the cached vendored file and the vendored file diverge (e.g. because the user starts to make edits?)

}

/// Get the cache-relative `SystemPath` that a `VendoredPath` refers to
pub fn vendored_path_for_cache(path: &VendoredPath) -> SystemPathBuf {
Copy link
Member

@MichaReiser MichaReiser Oct 21, 2025

Choose a reason for hiding this comment

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

Hmm. I'd prefer if we can keep this logic in ty_server, if possible because not all clients have a cache location or they use a different cache location (e.g. playground)

@AlexWaygood AlexWaygood changed the title Teach the resolver that vendored copies of typeshed in the ty cache are in fact typeshed [ty] Teach the resolver that vendored copies of typeshed in the ty cache are in fact typeshed Oct 21, 2025
@Gankra
Copy link
Contributor Author

Gankra commented Oct 21, 2025

Could we achive the same if the LSP registered an additional search path for the cached vendored files (that comes after the standard library?) That would require less custom casing for which we have not tests for.

The additional search-path still requires something like path_is_equivalent logic to not get rejected on the file.path() == module.file().path() logic. I'd also be worried that "the stdlib can't be shadowed" rules might get in the way? But probably it would do the right thing for this code specifically so maybe it's fine?

Another alternative would be to remap cached vendored file path to the corresponding vendored path within the LSP request handlers.

Hmm that was concerning to me as an approach earlier but maybe it's not actually that bad? We already do that mapping in the other direction. I'll give it a try.

Gankra added a commit that referenced this pull request Oct 21, 2025
This is an alternative to #21012 that more narrowly handles this logic
in the stub-mapping machinery rather than pervasively allowing us to
identify cached files as typeshed stubs. Much of the logic is the same
(pulling the logic out of ty_server so it can be reused).

I don't have a good sense for if one approach is "better" or "worse" in
terms of like, semantics and Weird Bugs that this can cause. This one is
just "less spooky in its broad consequences" and "less muddying of
separation of concerns" and puts the extra logic on a much colder path.
I won't be surprised if one day the previous implementation needs to be
revisited for its more sweeping effects but for now this is good.

Fixes astral-sh/ty#1054
@Gankra Gankra closed this Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

server Related to the LSP server ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

goto-definition on vendored typeshed definitions doesn't work

2 participants