feat(lsp): jsr support first pass (#22382)

This implementation heavily depends on there being a lockfile, meaning
JSR specifiers will always diagnose as uncached unless it's there. In
practice this affects cases where a `deno.json` isn't being used. Our
NPM specifier support isn't subject to this.

The reason for this is that the version constraint solving code is
currently buried in `deno_graph` and not usable from the LSP, so the
only way to reuse that logic is the solved-version map in the lockfile's
`packages.specifiers`.
This commit is contained in:
Nayeem Rahman 2024-02-12 22:12:49 +00:00 committed by GitHub
parent f60720090c
commit 49d82e609f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 265 additions and 15 deletions

View file

@ -362,9 +362,24 @@ impl LanguageServer {
.client
.show_message(MessageType::WARNING, err);
}
// do npm resolution in a write—we should have everything
// cached by this point anyway
self.0.write().await.refresh_npm_specifiers().await;
let mut lockfile_content_changed = false;
if let Some(lockfile) = self.0.read().await.config.maybe_lockfile() {
let lockfile = lockfile.lock();
let path = lockfile.filename.clone();
if let Ok(new_lockfile) = Lockfile::new(path, false) {
lockfile_content_changed = FastInsecureHasher::hash(&*lockfile)
!= FastInsecureHasher::hash(new_lockfile);
} else {
lockfile_content_changed = true;
}
}
if lockfile_content_changed {
// TODO(nayeemrmn): Remove this branch when the documents config no
// longer depends on the lockfile for JSR resolution.
self.0.write().await.refresh_documents_config().await;
} else {
self.0.write().await.refresh_npm_specifiers().await;
}
// now refresh the data in a read
self.0.read().await.post_cache(result.mark).await;
}
@ -1330,6 +1345,7 @@ impl Inner {
maybe_import_map: self.maybe_import_map.clone(),
maybe_config_file: self.config.maybe_config_file(),
maybe_package_json: self.maybe_package_json.as_ref(),
maybe_lockfile: self.config.maybe_lockfile().cloned(),
node_resolver: self.npm.node_resolver.clone(),
npm_resolver: self.npm.resolver.clone(),
});
@ -2009,6 +2025,8 @@ impl Inner {
Some("deno") => {
if diagnostic.code
== Some(NumberOrString::String("no-cache".to_string()))
|| diagnostic.code
== Some(NumberOrString::String("no-cache-jsr".to_string()))
|| diagnostic.code
== Some(NumberOrString::String("no-cache-npm".to_string()))
{