1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

feat(unstable/pm): support version contraints in 'deno add' (#22646)

This commit is contained in:
Nayeem Rahman 2024-03-01 21:34:13 +00:00 committed by GitHub
parent 2e4a1fc3e8
commit 15f5f74eb7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 398 additions and 353 deletions

View file

@ -186,10 +186,6 @@ impl FileFetcher {
} }
} }
pub fn http_cache(&self) -> &Arc<dyn HttpCache> {
&self.http_cache
}
pub fn cache_setting(&self) -> &CacheSetting { pub fn cache_setting(&self) -> &CacheSetting {
&self.cache_setting &self.cache_setting
} }

306
cli/jsr.rs Normal file
View file

@ -0,0 +1,306 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use crate::args::jsr_url;
use crate::file_fetcher::FileFetcher;
use dashmap::DashMap;
use deno_cache_dir::HttpCache;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json;
use deno_core::ModuleSpecifier;
use deno_graph::packages::JsrPackageInfo;
use deno_graph::packages::JsrPackageVersionInfo;
use deno_lockfile::Lockfile;
use deno_runtime::permissions::PermissionsContainer;
use deno_semver::jsr::JsrPackageReqReference;
use deno_semver::package::PackageNv;
use deno_semver::package::PackageReq;
use std::borrow::Cow;
use std::sync::Arc;
/// Keep in sync with `JsrFetchResolver`!
#[derive(Debug)]
pub struct JsrCacheResolver {
nv_by_req: DashMap<PackageReq, Option<PackageNv>>,
/// The `module_graph` field of the version infos should be forcibly absent.
/// It can be large and we don't want to store it.
info_by_nv: DashMap<PackageNv, Option<Arc<JsrPackageVersionInfo>>>,
info_by_name: DashMap<String, Option<Arc<JsrPackageInfo>>>,
cache: Arc<dyn HttpCache>,
}
impl JsrCacheResolver {
pub fn new(
cache: Arc<dyn HttpCache>,
lockfile: Option<Arc<Mutex<Lockfile>>>,
) -> Self {
let nv_by_req = DashMap::new();
if let Some(lockfile) = lockfile {
for (req_url, nv_url) in &lockfile.lock().content.packages.specifiers {
let Some(req) = req_url.strip_prefix("jsr:") else {
continue;
};
let Some(nv) = nv_url.strip_prefix("jsr:") else {
continue;
};
let Ok(req) = PackageReq::from_str(req) else {
continue;
};
let Ok(nv) = PackageNv::from_str(nv) else {
continue;
};
nv_by_req.insert(req, Some(nv));
}
}
Self {
nv_by_req,
info_by_nv: Default::default(),
info_by_name: Default::default(),
cache: cache.clone(),
}
}
pub fn req_to_nv(&self, req: &PackageReq) -> Option<PackageNv> {
if let Some(nv) = self.nv_by_req.get(req) {
return nv.value().clone();
}
let maybe_get_nv = || {
let name = req.name.clone();
let package_info = self.package_info(&name)?;
// Find the first matching version of the package which is cached.
let mut versions = package_info.versions.keys().collect::<Vec<_>>();
versions.sort();
let version = versions
.into_iter()
.rev()
.find(|v| {
if req.version_req.tag().is_some() || !req.version_req.matches(v) {
return false;
}
let nv = PackageNv {
name: name.clone(),
version: (*v).clone(),
};
self.package_version_info(&nv).is_some()
})
.cloned()?;
Some(PackageNv { name, version })
};
let nv = maybe_get_nv();
self.nv_by_req.insert(req.clone(), nv.clone());
nv
}
pub fn jsr_to_registry_url(
&self,
specifier: &ModuleSpecifier,
) -> Option<ModuleSpecifier> {
let req_ref = JsrPackageReqReference::from_str(specifier.as_str()).ok()?;
let req = req_ref.req().clone();
let maybe_nv = self.req_to_nv(&req);
let nv = maybe_nv.as_ref()?;
let info = self.package_version_info(nv)?;
let path = info.export(&normalize_export_name(req_ref.sub_path()))?;
jsr_url()
.join(&format!("{}/{}/{}", &nv.name, &nv.version, &path))
.ok()
}
pub fn lookup_export_for_path(
&self,
nv: &PackageNv,
path: &str,
) -> Option<String> {
let info = self.package_version_info(nv)?;
let path = path.strip_prefix("./").unwrap_or(path);
for (export, path_) in info.exports() {
if path_.strip_prefix("./").unwrap_or(path_) == path {
return Some(export.strip_prefix("./").unwrap_or(export).to_string());
}
}
None
}
pub fn lookup_req_for_nv(&self, nv: &PackageNv) -> Option<PackageReq> {
for entry in self.nv_by_req.iter() {
let Some(nv_) = entry.value() else {
continue;
};
if nv_ == nv {
return Some(entry.key().clone());
}
}
None
}
pub fn package_info(&self, name: &str) -> Option<Arc<JsrPackageInfo>> {
if let Some(info) = self.info_by_name.get(name) {
return info.value().clone();
}
let read_cached_package_info = || {
let meta_url = jsr_url().join(&format!("{}/meta.json", name)).ok()?;
let meta_bytes = read_cached_url(&meta_url, &self.cache)?;
serde_json::from_slice::<JsrPackageInfo>(&meta_bytes).ok()
};
let info = read_cached_package_info().map(Arc::new);
self.info_by_name.insert(name.to_string(), info.clone());
info
}
pub fn package_version_info(
&self,
nv: &PackageNv,
) -> Option<Arc<JsrPackageVersionInfo>> {
if let Some(info) = self.info_by_nv.get(nv) {
return info.value().clone();
}
let read_cached_package_version_info = || {
let meta_url = jsr_url()
.join(&format!("{}/{}_meta.json", &nv.name, &nv.version))
.ok()?;
let meta_bytes = read_cached_url(&meta_url, &self.cache)?;
partial_jsr_package_version_info_from_slice(&meta_bytes).ok()
};
let info = read_cached_package_version_info().map(Arc::new);
self.info_by_nv.insert(nv.clone(), info.clone());
info
}
}
fn read_cached_url(
url: &ModuleSpecifier,
cache: &Arc<dyn HttpCache>,
) -> Option<Vec<u8>> {
cache
.read_file_bytes(
&cache.cache_item_key(url).ok()?,
None,
deno_cache_dir::GlobalToLocalCopy::Disallow,
)
.ok()?
}
/// This is similar to a subset of `JsrCacheResolver` which fetches rather than
/// just reads the cache. Keep in sync!
#[derive(Debug)]
pub struct JsrFetchResolver {
nv_by_req: DashMap<PackageReq, Option<PackageNv>>,
/// The `module_graph` field of the version infos should be forcibly absent.
/// It can be large and we don't want to store it.
info_by_nv: DashMap<PackageNv, Option<Arc<JsrPackageVersionInfo>>>,
info_by_name: DashMap<String, Option<Arc<JsrPackageInfo>>>,
file_fetcher: FileFetcher,
}
impl JsrFetchResolver {
pub fn new(file_fetcher: FileFetcher) -> Self {
Self {
nv_by_req: Default::default(),
info_by_nv: Default::default(),
info_by_name: Default::default(),
file_fetcher,
}
}
pub async fn req_to_nv(&self, req: &PackageReq) -> Option<PackageNv> {
if let Some(nv) = self.nv_by_req.get(req) {
return nv.value().clone();
}
let maybe_get_nv = || async {
let name = req.name.clone();
let package_info = self.package_info(&name).await?;
// Find the first matching version of the package which is cached.
let mut versions = package_info.versions.keys().collect::<Vec<_>>();
versions.sort();
let version = versions
.into_iter()
.rev()
.find(|v| req.version_req.tag().is_none() && req.version_req.matches(v))
.cloned()?;
Some(PackageNv { name, version })
};
let nv = maybe_get_nv().await;
self.nv_by_req.insert(req.clone(), nv.clone());
nv
}
pub async fn package_info(&self, name: &str) -> Option<Arc<JsrPackageInfo>> {
if let Some(info) = self.info_by_name.get(name) {
return info.value().clone();
}
let read_cached_package_info = || async {
let meta_url = jsr_url().join(&format!("{}/meta.json", name)).ok()?;
let file = self
.file_fetcher
.fetch(&meta_url, PermissionsContainer::allow_all())
.await
.ok()?;
serde_json::from_slice::<JsrPackageInfo>(&file.source).ok()
};
let info = read_cached_package_info().await.map(Arc::new);
self.info_by_name.insert(name.to_string(), info.clone());
info
}
pub async fn package_version_info(
&self,
nv: &PackageNv,
) -> Option<Arc<JsrPackageVersionInfo>> {
if let Some(info) = self.info_by_nv.get(nv) {
return info.value().clone();
}
let read_cached_package_version_info = || async {
let meta_url = jsr_url()
.join(&format!("{}/{}_meta.json", &nv.name, &nv.version))
.ok()?;
let file = self
.file_fetcher
.fetch(&meta_url, PermissionsContainer::allow_all())
.await
.ok()?;
partial_jsr_package_version_info_from_slice(&file.source).ok()
};
let info = read_cached_package_version_info().await.map(Arc::new);
self.info_by_nv.insert(nv.clone(), info.clone());
info
}
}
// TODO(nayeemrmn): This is duplicated from a private function in deno_graph
// 0.65.1. Make it public or cleanup otherwise.
fn normalize_export_name(sub_path: Option<&str>) -> Cow<str> {
let Some(sub_path) = sub_path else {
return Cow::Borrowed(".");
};
if sub_path.is_empty() || matches!(sub_path, "/" | ".") {
Cow::Borrowed(".")
} else {
let sub_path = if sub_path.starts_with('/') {
Cow::Owned(format!(".{}", sub_path))
} else if !sub_path.starts_with("./") {
Cow::Owned(format!("./{}", sub_path))
} else {
Cow::Borrowed(sub_path)
};
if let Some(prefix) = sub_path.strip_suffix('/') {
Cow::Owned(prefix.to_string())
} else {
sub_path
}
}
}
/// This is a roundabout way of deserializing `JsrPackageVersionInfo`,
/// because we only want the `exports` field and `module_graph` is large.
fn partial_jsr_package_version_info_from_slice(
slice: &[u8],
) -> serde_json::Result<JsrPackageVersionInfo> {
let mut info = serde_json::from_slice::<serde_json::Value>(slice)?;
Ok(JsrPackageVersionInfo {
manifest: Default::default(), // not used by the LSP (only caching checks this in deno_graph)
exports: info
.as_object_mut()
.and_then(|o| o.remove("exports"))
.unwrap_or_default(),
module_graph: None,
})
}

View file

@ -6,13 +6,13 @@ use super::config::WorkspaceSettings;
use super::documents::Documents; use super::documents::Documents;
use super::documents::DocumentsFilter; use super::documents::DocumentsFilter;
use super::jsr::CliJsrSearchApi; use super::jsr::CliJsrSearchApi;
use super::jsr::JsrResolver;
use super::lsp_custom; use super::lsp_custom;
use super::npm::CliNpmSearchApi; use super::npm::CliNpmSearchApi;
use super::registries::ModuleRegistry; use super::registries::ModuleRegistry;
use super::search::PackageSearchApi; use super::search::PackageSearchApi;
use super::tsc; use super::tsc;
use crate::jsr::JsrFetchResolver;
use crate::util::path::is_importable_ext; use crate::util::path::is_importable_ext;
use crate::util::path::relative_specifier; use crate::util::path::relative_specifier;
use crate::util::path::specifier_to_file_path; use crate::util::path::specifier_to_file_path;
@ -181,7 +181,7 @@ pub async fn get_import_completions(
&text, &text,
&range, &range,
jsr_search_api, jsr_search_api,
jsr_search_api.get_resolver(), Some(jsr_search_api.get_resolver()),
) )
.await?; .await?;
Some(lsp::CompletionResponse::List(lsp::CompletionList { Some(lsp::CompletionResponse::List(lsp::CompletionList {
@ -518,7 +518,7 @@ async fn get_jsr_completions(
specifier: &str, specifier: &str,
range: &lsp::Range, range: &lsp::Range,
jsr_search_api: &impl PackageSearchApi, jsr_search_api: &impl PackageSearchApi,
jsr_resolver: &JsrResolver, jsr_resolver: Option<&JsrFetchResolver>,
) -> Option<Vec<lsp::CompletionItem>> { ) -> Option<Vec<lsp::CompletionItem>> {
// First try to match `jsr:some-package@some-version/<export-to-complete>`. // First try to match `jsr:some-package@some-version/<export-to-complete>`.
if let Ok(req_ref) = JsrPackageReqReference::from_str(specifier) { if let Ok(req_ref) = JsrPackageReqReference::from_str(specifier) {
@ -526,7 +526,10 @@ async fn get_jsr_completions(
if sub_path.is_some() || specifier.ends_with('/') { if sub_path.is_some() || specifier.ends_with('/') {
let export_prefix = sub_path.unwrap_or(""); let export_prefix = sub_path.unwrap_or("");
let req = req_ref.req(); let req = req_ref.req();
let nv = jsr_resolver.req_to_nv(req); let nv = match jsr_resolver {
Some(jsr_resolver) => jsr_resolver.req_to_nv(req).await,
None => None,
};
let nv = nv.or_else(|| PackageNv::from_str(&req.to_string()).ok())?; let nv = nv.or_else(|| PackageNv::from_str(&req.to_string()).ok())?;
let exports = jsr_search_api.exports(&nv).await.ok()?; let exports = jsr_search_api.exports(&nv).await.ok()?;
let items = exports let items = exports
@ -799,7 +802,6 @@ mod tests {
use super::*; use super::*;
use crate::cache::GlobalHttpCache; use crate::cache::GlobalHttpCache;
use crate::cache::HttpCache; use crate::cache::HttpCache;
use crate::cache::RealDenoCacheEnv;
use crate::lsp::documents::Documents; use crate::lsp::documents::Documents;
use crate::lsp::documents::LanguageId; use crate::lsp::documents::LanguageId;
use crate::lsp::search::tests::TestPackageSearchApi; use crate::lsp::search::tests::TestPackageSearchApi;
@ -1007,14 +1009,6 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_get_jsr_completions() { async fn test_get_jsr_completions() {
let temp_dir = TempDir::default();
let jsr_resolver = JsrResolver::from_cache_and_lockfile(
Arc::new(GlobalHttpCache::new(
temp_dir.path().to_path_buf(),
RealDenoCacheEnv,
)),
None,
);
let jsr_search_api = TestPackageSearchApi::default() let jsr_search_api = TestPackageSearchApi::default()
.with_package_version("@std/archive", "1.0.0", &[]) .with_package_version("@std/archive", "1.0.0", &[])
.with_package_version("@std/assert", "1.0.0", &[]) .with_package_version("@std/assert", "1.0.0", &[])
@ -1031,13 +1025,8 @@ mod tests {
}, },
}; };
let referrer = ModuleSpecifier::parse("file:///referrer.ts").unwrap(); let referrer = ModuleSpecifier::parse("file:///referrer.ts").unwrap();
let actual = get_jsr_completions( let actual =
&referrer, get_jsr_completions(&referrer, "jsr:as", &range, &jsr_search_api, None)
"jsr:as",
&range,
&jsr_search_api,
&jsr_resolver,
)
.await .await
.unwrap(); .unwrap();
assert_eq!( assert_eq!(
@ -1095,14 +1084,6 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_get_jsr_completions_for_versions() { async fn test_get_jsr_completions_for_versions() {
let temp_dir = TempDir::default();
let jsr_resolver = JsrResolver::from_cache_and_lockfile(
Arc::new(GlobalHttpCache::new(
temp_dir.path().to_path_buf(),
RealDenoCacheEnv,
)),
None,
);
let jsr_search_api = TestPackageSearchApi::default() let jsr_search_api = TestPackageSearchApi::default()
.with_package_version("@std/assert", "0.3.0", &[]) .with_package_version("@std/assert", "0.3.0", &[])
.with_package_version("@std/assert", "0.4.0", &[]) .with_package_version("@std/assert", "0.4.0", &[])
@ -1123,7 +1104,7 @@ mod tests {
"jsr:@std/assert@", "jsr:@std/assert@",
&range, &range,
&jsr_search_api, &jsr_search_api,
&jsr_resolver, None,
) )
.await .await
.unwrap(); .unwrap();
@ -1205,14 +1186,6 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_get_jsr_completions_for_exports() { async fn test_get_jsr_completions_for_exports() {
let temp_dir = TempDir::default();
let jsr_resolver = JsrResolver::from_cache_and_lockfile(
Arc::new(GlobalHttpCache::new(
temp_dir.path().to_path_buf(),
RealDenoCacheEnv,
)),
None,
);
let jsr_search_api = TestPackageSearchApi::default().with_package_version( let jsr_search_api = TestPackageSearchApi::default().with_package_version(
"@std/path", "@std/path",
"0.1.0", "0.1.0",
@ -1234,7 +1207,7 @@ mod tests {
"jsr:@std/path@0.1.0/co", "jsr:@std/path@0.1.0/co",
&range, &range,
&jsr_search_api, &jsr_search_api,
&jsr_resolver, None,
) )
.await .await
.unwrap(); .unwrap();

View file

@ -3,7 +3,6 @@
use super::cache::calculate_fs_version; use super::cache::calculate_fs_version;
use super::cache::calculate_fs_version_at_path; use super::cache::calculate_fs_version_at_path;
use super::cache::LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY; use super::cache::LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY;
use super::jsr::JsrResolver;
use super::language_server::StateNpmSnapshot; use super::language_server::StateNpmSnapshot;
use super::text::LineIndex; use super::text::LineIndex;
use super::tsc; use super::tsc;
@ -15,6 +14,7 @@ use crate::args::ConfigFile;
use crate::args::JsxImportSourceConfig; use crate::args::JsxImportSourceConfig;
use crate::cache::FastInsecureHasher; use crate::cache::FastInsecureHasher;
use crate::cache::HttpCache; use crate::cache::HttpCache;
use crate::jsr::JsrCacheResolver;
use crate::lsp::logging::lsp_warn; use crate::lsp::logging::lsp_warn;
use crate::npm::CliNpmResolver; use crate::npm::CliNpmResolver;
use crate::resolver::CliGraphResolver; use crate::resolver::CliGraphResolver;
@ -893,7 +893,7 @@ pub struct Documents {
/// A resolver that takes into account currently loaded import map and JSX /// A resolver that takes into account currently loaded import map and JSX
/// settings. /// settings.
resolver: Arc<CliGraphResolver>, resolver: Arc<CliGraphResolver>,
jsr_resolver: Arc<JsrResolver>, jsr_resolver: Arc<JsrCacheResolver>,
/// The npm package requirements found in npm specifiers. /// The npm package requirements found in npm specifiers.
npm_specifier_reqs: Arc<Vec<PackageReq>>, npm_specifier_reqs: Arc<Vec<PackageReq>>,
/// Gets if any document had a node: specifier such that a @types/node package /// Gets if any document had a node: specifier such that a @types/node package
@ -928,10 +928,7 @@ impl Documents {
bare_node_builtins_enabled: false, bare_node_builtins_enabled: false,
sloppy_imports_resolver: None, sloppy_imports_resolver: None,
})), })),
jsr_resolver: Arc::new(JsrResolver::from_cache_and_lockfile( jsr_resolver: Arc::new(JsrCacheResolver::new(cache.clone(), None)),
cache.clone(),
None,
)),
npm_specifier_reqs: Default::default(), npm_specifier_reqs: Default::default(),
has_injected_types_node_package: false, has_injected_types_node_package: false,
redirect_resolver: Arc::new(RedirectResolver::new(cache)), redirect_resolver: Arc::new(RedirectResolver::new(cache)),
@ -1336,7 +1333,7 @@ impl Documents {
Ok(()) Ok(())
} }
pub fn get_jsr_resolver(&self) -> &Arc<JsrResolver> { pub fn get_jsr_resolver(&self) -> &Arc<JsrCacheResolver> {
&self.jsr_resolver &self.jsr_resolver
} }
@ -1344,10 +1341,8 @@ impl Documents {
&mut self, &mut self,
lockfile: Option<Arc<Mutex<Lockfile>>>, lockfile: Option<Arc<Mutex<Lockfile>>>,
) { ) {
self.jsr_resolver = Arc::new(JsrResolver::from_cache_and_lockfile( self.jsr_resolver =
self.cache.clone(), Arc::new(JsrCacheResolver::new(self.cache.clone(), lockfile));
lockfile,
));
} }
pub fn update_config(&mut self, options: UpdateDocumentConfigOptions) { pub fn update_config(&mut self, options: UpdateDocumentConfigOptions) {
@ -1452,7 +1447,7 @@ impl Documents {
// specifier for free. // specifier for free.
sloppy_imports_resolver: None, sloppy_imports_resolver: None,
})); }));
self.jsr_resolver = Arc::new(JsrResolver::from_cache_and_lockfile( self.jsr_resolver = Arc::new(JsrCacheResolver::new(
self.cache.clone(), self.cache.clone(),
options.maybe_lockfile, options.maybe_lockfile,
)); ));

View file

@ -1,198 +1,26 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use crate::args::jsr_api_url; use crate::args::jsr_api_url;
use crate::args::jsr_url;
use crate::file_fetcher::FileFetcher; use crate::file_fetcher::FileFetcher;
use crate::jsr::JsrFetchResolver;
use dashmap::DashMap; use dashmap::DashMap;
use deno_cache_dir::HttpCache;
use deno_core::anyhow::anyhow; use deno_core::anyhow::anyhow;
use deno_core::error::AnyError; use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
use deno_core::serde_json; use deno_core::serde_json;
use deno_core::ModuleSpecifier;
use deno_graph::packages::JsrPackageInfo;
use deno_graph::packages::JsrPackageVersionInfo;
use deno_lockfile::Lockfile;
use deno_runtime::permissions::PermissionsContainer; use deno_runtime::permissions::PermissionsContainer;
use deno_semver::jsr::JsrPackageReqReference;
use deno_semver::package::PackageNv; use deno_semver::package::PackageNv;
use deno_semver::package::PackageReq;
use deno_semver::Version; use deno_semver::Version;
use serde::Deserialize; use serde::Deserialize;
use std::borrow::Cow;
use std::sync::Arc; use std::sync::Arc;
use super::cache::LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY;
use super::search::PackageSearchApi; use super::search::PackageSearchApi;
#[derive(Debug)]
pub struct JsrResolver {
nv_by_req: DashMap<PackageReq, Option<PackageNv>>,
/// The `module_graph` field of the version infos should be forcibly absent.
/// It can be large and we don't want to store it.
info_by_nv: DashMap<PackageNv, Option<JsrPackageVersionInfo>>,
info_by_name: DashMap<String, Option<JsrPackageInfo>>,
cache: Arc<dyn HttpCache>,
}
impl JsrResolver {
pub fn from_cache_and_lockfile(
cache: Arc<dyn HttpCache>,
lockfile: Option<Arc<Mutex<Lockfile>>>,
) -> Self {
let nv_by_req = DashMap::new();
if let Some(lockfile) = lockfile {
for (req_url, nv_url) in &lockfile.lock().content.packages.specifiers {
let Some(req) = req_url.strip_prefix("jsr:") else {
continue;
};
let Some(nv) = nv_url.strip_prefix("jsr:") else {
continue;
};
let Ok(req) = PackageReq::from_str(req) else {
continue;
};
let Ok(nv) = PackageNv::from_str(nv) else {
continue;
};
nv_by_req.insert(req, Some(nv));
}
}
Self {
nv_by_req,
info_by_nv: Default::default(),
info_by_name: Default::default(),
cache: cache.clone(),
}
}
pub fn req_to_nv(&self, req: &PackageReq) -> Option<PackageNv> {
let nv = self.nv_by_req.entry(req.clone()).or_insert_with(|| {
let name = req.name.clone();
let maybe_package_info = self
.info_by_name
.entry(name.clone())
.or_insert_with(|| read_cached_package_info(&name, &self.cache));
let package_info = maybe_package_info.as_ref()?;
// Find the first matching version of the package which is cached.
let mut versions = package_info.versions.keys().collect::<Vec<_>>();
versions.sort();
let version = versions
.into_iter()
.rev()
.find(|v| {
if req.version_req.tag().is_some() || !req.version_req.matches(v) {
return false;
}
let nv = PackageNv {
name: name.clone(),
version: (*v).clone(),
};
self
.info_by_nv
.entry(nv.clone())
.or_insert_with(|| {
read_cached_package_version_info(&nv, &self.cache)
})
.is_some()
})
.cloned()?;
Some(PackageNv { name, version })
});
nv.value().clone()
}
pub fn jsr_to_registry_url(
&self,
specifier: &ModuleSpecifier,
) -> Option<ModuleSpecifier> {
let req_ref = JsrPackageReqReference::from_str(specifier.as_str()).ok()?;
let req = req_ref.req().clone();
let maybe_nv = self.req_to_nv(&req);
let nv = maybe_nv.as_ref()?;
let maybe_info = self
.info_by_nv
.entry(nv.clone())
.or_insert_with(|| read_cached_package_version_info(nv, &self.cache));
let info = maybe_info.as_ref()?;
let path = info.export(&normalize_export_name(req_ref.sub_path()))?;
jsr_url()
.join(&format!("{}/{}/{}", &nv.name, &nv.version, &path))
.ok()
}
pub fn lookup_export_for_path(
&self,
nv: &PackageNv,
path: &str,
) -> Option<String> {
let maybe_info = self
.info_by_nv
.entry(nv.clone())
.or_insert_with(|| read_cached_package_version_info(nv, &self.cache));
let info = maybe_info.as_ref()?;
let path = path.strip_prefix("./").unwrap_or(path);
for (export, path_) in info.exports() {
if path_.strip_prefix("./").unwrap_or(path_) == path {
return Some(export.strip_prefix("./").unwrap_or(export).to_string());
}
}
None
}
pub fn lookup_req_for_nv(&self, nv: &PackageNv) -> Option<PackageReq> {
for entry in self.nv_by_req.iter() {
let Some(nv_) = entry.value() else {
continue;
};
if nv_ == nv {
return Some(entry.key().clone());
}
}
None
}
}
fn read_cached_package_info(
name: &str,
cache: &Arc<dyn HttpCache>,
) -> Option<JsrPackageInfo> {
let meta_url = jsr_url().join(&format!("{}/meta.json", name)).ok()?;
let meta_cache_item_key = cache.cache_item_key(&meta_url).ok()?;
let meta_bytes = cache
.read_file_bytes(
&meta_cache_item_key,
None,
LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY,
)
.ok()??;
serde_json::from_slice::<JsrPackageInfo>(&meta_bytes).ok()
}
fn read_cached_package_version_info(
nv: &PackageNv,
cache: &Arc<dyn HttpCache>,
) -> Option<JsrPackageVersionInfo> {
let meta_url = jsr_url()
.join(&format!("{}/{}_meta.json", &nv.name, &nv.version))
.ok()?;
let meta_cache_item_key = cache.cache_item_key(&meta_url).ok()?;
let meta_bytes = cache
.read_file_bytes(
&meta_cache_item_key,
None,
LSP_DISALLOW_GLOBAL_TO_LOCAL_COPY,
)
.ok()??;
partial_jsr_package_version_info_from_slice(&meta_bytes).ok()
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct CliJsrSearchApi { pub struct CliJsrSearchApi {
file_fetcher: FileFetcher, file_fetcher: FileFetcher,
/// We only store this here so the completion system has access to a resolver /// We only store this here so the completion system has access to a resolver
/// that always uses the global cache. /// that always uses the global cache.
resolver: Arc<JsrResolver>, resolver: Arc<JsrFetchResolver>,
search_cache: Arc<DashMap<String, Arc<Vec<String>>>>, search_cache: Arc<DashMap<String, Arc<Vec<String>>>>,
versions_cache: Arc<DashMap<String, Arc<Vec<Version>>>>, versions_cache: Arc<DashMap<String, Arc<Vec<Version>>>>,
exports_cache: Arc<DashMap<PackageNv, Arc<Vec<String>>>>, exports_cache: Arc<DashMap<PackageNv, Arc<Vec<String>>>>,
@ -200,10 +28,7 @@ pub struct CliJsrSearchApi {
impl CliJsrSearchApi { impl CliJsrSearchApi {
pub fn new(file_fetcher: FileFetcher) -> Self { pub fn new(file_fetcher: FileFetcher) -> Self {
let resolver = Arc::new(JsrResolver::from_cache_and_lockfile( let resolver = Arc::new(JsrFetchResolver::new(file_fetcher.clone()));
file_fetcher.http_cache().clone(),
None,
));
Self { Self {
file_fetcher, file_fetcher,
resolver, resolver,
@ -213,7 +38,7 @@ impl CliJsrSearchApi {
} }
} }
pub fn get_resolver(&self) -> &Arc<JsrResolver> { pub fn get_resolver(&self) -> &Arc<JsrFetchResolver> {
&self.resolver &self.resolver
} }
} }
@ -245,19 +70,12 @@ impl PackageSearchApi for CliJsrSearchApi {
if let Some(versions) = self.versions_cache.get(name) { if let Some(versions) = self.versions_cache.get(name) {
return Ok(versions.clone()); return Ok(versions.clone());
} }
let mut meta_url = jsr_url().clone(); let info = self
meta_url .resolver
.path_segments_mut() .package_info(name)
.map_err(|_| anyhow!("Custom jsr URL cannot be a base."))? .await
.pop_if_empty() .ok_or_else(|| anyhow!("JSR package info not found: {}", name))?;
.push(name) let mut versions = info.versions.keys().cloned().collect::<Vec<_>>();
.push("meta.json");
let file = self
.file_fetcher
.fetch(&meta_url, PermissionsContainer::allow_all())
.await?;
let info = serde_json::from_slice::<JsrPackageInfo>(&file.source)?;
let mut versions = info.versions.into_keys().collect::<Vec<_>>();
versions.sort(); versions.sort();
versions.reverse(); versions.reverse();
let versions = Arc::new(versions); let versions = Arc::new(versions);
@ -274,18 +92,11 @@ impl PackageSearchApi for CliJsrSearchApi {
if let Some(exports) = self.exports_cache.get(nv) { if let Some(exports) = self.exports_cache.get(nv) {
return Ok(exports.clone()); return Ok(exports.clone());
} }
let mut meta_url = jsr_url().clone(); let info = self
meta_url .resolver
.path_segments_mut() .package_version_info(nv)
.map_err(|_| anyhow!("Custom jsr URL cannot be a base."))? .await
.pop_if_empty() .ok_or_else(|| anyhow!("JSR package version info not found: {}", nv))?;
.push(&nv.name)
.push(&format!("{}_meta.json", &nv.version));
let file = self
.file_fetcher
.fetch(&meta_url, PermissionsContainer::allow_all())
.await?;
let info = partial_jsr_package_version_info_from_slice(&file.source)?;
let mut exports = info let mut exports = info
.exports() .exports()
.map(|(n, _)| n.to_string()) .map(|(n, _)| n.to_string())
@ -297,46 +108,6 @@ impl PackageSearchApi for CliJsrSearchApi {
} }
} }
// TODO(nayeemrmn): This is duplicated from a private function in deno_graph
// 0.65.1. Make it public or cleanup otherwise.
fn normalize_export_name(sub_path: Option<&str>) -> Cow<str> {
let Some(sub_path) = sub_path else {
return Cow::Borrowed(".");
};
if sub_path.is_empty() || matches!(sub_path, "/" | ".") {
Cow::Borrowed(".")
} else {
let sub_path = if sub_path.starts_with('/') {
Cow::Owned(format!(".{}", sub_path))
} else if !sub_path.starts_with("./") {
Cow::Owned(format!("./{}", sub_path))
} else {
Cow::Borrowed(sub_path)
};
if let Some(prefix) = sub_path.strip_suffix('/') {
Cow::Owned(prefix.to_string())
} else {
sub_path
}
}
}
/// This is a roundabout way of deserializing `JsrPackageVersionInfo`,
/// because we only want the `exports` field and `module_graph` is large.
fn partial_jsr_package_version_info_from_slice(
slice: &[u8],
) -> serde_json::Result<JsrPackageVersionInfo> {
let mut info = serde_json::from_slice::<serde_json::Value>(slice)?;
Ok(JsrPackageVersionInfo {
manifest: Default::default(), // not used by the LSP (only caching checks this in deno_graph)
exports: info
.as_object_mut()
.and_then(|o| o.remove("exports"))
.unwrap_or_default(),
module_graph: None,
})
}
fn parse_jsr_search_response(source: &str) -> Result<Vec<String>, AnyError> { fn parse_jsr_search_response(source: &str) -> Result<Vec<String>, AnyError> {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]

View file

@ -21,7 +21,7 @@ mod completions;
mod config; mod config;
mod diagnostics; mod diagnostics;
mod documents; mod documents;
pub mod jsr; mod jsr;
pub mod language_server; pub mod language_server;
mod logging; mod logging;
mod lsp_custom; mod lsp_custom;
@ -32,7 +32,7 @@ mod performance;
mod refactor; mod refactor;
mod registries; mod registries;
mod repl; mod repl;
pub mod search; mod search;
mod semantic_tokens; mod semantic_tokens;
mod testing; mod testing;
mod text; mod text;

View file

@ -12,6 +12,7 @@ mod file_fetcher;
mod graph_util; mod graph_util;
mod http_util; mod http_util;
mod js; mod js;
mod jsr;
mod lsp; mod lsp;
mod module_loader; mod module_loader;
mod napi; mod napi;

View file

@ -2,6 +2,7 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::Arc;
use deno_ast::TextChange; use deno_ast::TextChange;
use deno_config::FmtOptionsConfig; use deno_config::FmtOptionsConfig;
@ -13,7 +14,6 @@ use deno_core::futures::StreamExt;
use deno_core::serde_json; use deno_core::serde_json;
use deno_semver::jsr::JsrPackageReqReference; use deno_semver::jsr::JsrPackageReqReference;
use deno_semver::npm::NpmPackageReqReference; use deno_semver::npm::NpmPackageReqReference;
use deno_semver::package::PackageReq;
use jsonc_parser::ast::ObjectProp; use jsonc_parser::ast::ObjectProp;
use jsonc_parser::ast::Value; use jsonc_parser::ast::Value;
@ -22,8 +22,7 @@ use crate::args::CacheSetting;
use crate::args::Flags; use crate::args::Flags;
use crate::factory::CliFactory; use crate::factory::CliFactory;
use crate::file_fetcher::FileFetcher; use crate::file_fetcher::FileFetcher;
use crate::lsp::jsr::CliJsrSearchApi; use crate::jsr::JsrFetchResolver;
use crate::lsp::search::PackageSearchApi;
pub async fn add(flags: Flags, add_flags: AddFlags) -> Result<(), AnyError> { pub async fn add(flags: Flags, add_flags: AddFlags) -> Result<(), AnyError> {
let cli_factory = CliFactory::from_flags(flags.clone()).await?; let cli_factory = CliFactory::from_flags(flags.clone()).await?;
@ -78,15 +77,12 @@ pub async fn add(flags: Flags, add_flags: AddFlags) -> Result<(), AnyError> {
None, None,
); );
deps_file_fetcher.set_download_log_level(log::Level::Trace); deps_file_fetcher.set_download_log_level(log::Level::Trace);
let jsr_search_api = CliJsrSearchApi::new(deps_file_fetcher); let jsr_resolver = Arc::new(JsrFetchResolver::new(deps_file_fetcher));
let package_futures = package_reqs let package_futures = package_reqs
.into_iter() .into_iter()
.map(|package_req| { .map(move |package_req| {
find_package_and_select_version_for_req( find_package_and_select_version_for_req(jsr_resolver.clone(), package_req)
jsr_search_api.clone(),
package_req,
)
.boxed_local() .boxed_local()
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -185,42 +181,27 @@ enum PackageAndVersion {
Selected(SelectedPackage), Selected(SelectedPackage),
} }
async fn jsr_find_package_and_select_version(
jsr_search_api: CliJsrSearchApi,
req: &PackageReq,
) -> Result<PackageAndVersion, AnyError> {
let jsr_prefixed_name = format!("jsr:{}", req.name);
// TODO(bartlomieju): Need to do semver as well - @luca/flag@^0.14 should use to
// highest possible `0.14.x` version.
let version_req = req.version_req.version_text();
if version_req != "*" {
bail!("Specifying version constraints is currently not supported. Package: {}@{}", jsr_prefixed_name, version_req);
}
let Ok(versions) = jsr_search_api.versions(&req.name).await else {
return Ok(PackageAndVersion::NotFound(jsr_prefixed_name));
};
let Some(latest_version) = versions.first() else {
return Ok(PackageAndVersion::NotFound(jsr_prefixed_name));
};
Ok(PackageAndVersion::Selected(SelectedPackage {
import_name: req.name.to_string(),
package_name: jsr_prefixed_name,
// TODO(bartlomieju): fix it, it should not always be caret
version_req: format!("^{}", latest_version),
}))
}
async fn find_package_and_select_version_for_req( async fn find_package_and_select_version_for_req(
jsr_search_api: CliJsrSearchApi, jsr_resolver: Arc<JsrFetchResolver>,
add_package_req: AddPackageReq, add_package_req: AddPackageReq,
) -> Result<PackageAndVersion, AnyError> { ) -> Result<PackageAndVersion, AnyError> {
match add_package_req { match add_package_req {
AddPackageReq::Jsr(pkg_ref) => { AddPackageReq::Jsr(pkg_ref) => {
jsr_find_package_and_select_version(jsr_search_api, pkg_ref.req()).await let req = pkg_ref.req();
let jsr_prefixed_name = format!("jsr:{}", &req.name);
let Some(nv) = jsr_resolver.req_to_nv(req).await else {
return Ok(PackageAndVersion::NotFound(jsr_prefixed_name));
};
let range_symbol = if req.version_req.version_text().starts_with('~') {
'~'
} else {
'^'
};
Ok(PackageAndVersion::Selected(SelectedPackage {
import_name: req.name.to_string(),
package_name: jsr_prefixed_name,
version_req: format!("{}{}", range_symbol, &nv.version),
}))
} }
AddPackageReq::Npm(pkg_req) => { AddPackageReq::Npm(pkg_req) => {
bail!( bail!(

View file

@ -48,6 +48,38 @@ fn add_basic_no_deno_json() {
})); }));
} }
#[test]
fn add_version_contraint() {
let context = pm_context_builder().build();
let temp_dir = context.temp_dir().path();
let output = context.new_command().args("add @denotest/add@1").run();
output.assert_exit_code(0);
let output = output.combined_output();
assert_contains!(output, "Add @denotest/add");
temp_dir.join("deno.json").assert_matches_json(json!({
"imports": {
"@denotest/add": "jsr:@denotest/add@^1.0.0"
}
}));
}
#[test]
fn add_tilde() {
let context = pm_context_builder().build();
let temp_dir = context.temp_dir().path();
let output = context.new_command().args("add @denotest/add@~1").run();
output.assert_exit_code(0);
let output = output.combined_output();
assert_contains!(output, "Add @denotest/add");
temp_dir.join("deno.json").assert_matches_json(json!({
"imports": {
"@denotest/add": "jsr:@denotest/add@~1.0.0"
}
}));
}
#[test] #[test]
fn add_multiple() { fn add_multiple() {
let starting_deno_json = json!({ let starting_deno_json = json!({
@ -90,16 +122,6 @@ fn add_not_supported_npm() {
assert_contains!(output, "error: Adding npm: packages is currently not supported. Package: npm:express"); assert_contains!(output, "error: Adding npm: packages is currently not supported. Package: npm:express");
} }
#[test]
fn add_not_supported_version_constraint() {
let context = pm_context_builder().build();
let output = context.new_command().args("add @denotest/add@1").run();
output.assert_exit_code(1);
let output = output.combined_output();
assert_contains!(output, "error: Specifying version constraints is currently not supported. Package: jsr:@denotest/add@1");
}
fn pm_context_builder() -> TestContextBuilder { fn pm_context_builder() -> TestContextBuilder {
TestContextBuilder::new() TestContextBuilder::new()
.use_http_server() .use_http_server()