Skip to content

Commit

Permalink
cache external import map if loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwhit committed Dec 12, 2024
1 parent d2c55b0 commit 4603997
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 37 deletions.
28 changes: 14 additions & 14 deletions cli/args/deno_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use std::collections::HashSet;

use deno_config::deno_json::TsConfigForEmit;
use deno_core::error::AnyError;
use deno_core::serde_json;
use deno_semver::jsr::JsrDepPackageReq;
use deno_semver::jsr::JsrPackageReqReference;
Expand Down Expand Up @@ -65,20 +64,21 @@ impl<'a> deno_config::fs::DenoConfigFs for DenoConfigFsAdapter<'a> {
}
}

pub fn import_map_deps(
import_map: &serde_json::Value,
) -> HashSet<JsrDepPackageReq> {
let values = imports_values(import_map.get("imports"))
.into_iter()
.chain(scope_values(import_map.get("scopes")));
values_to_set(values)
}

pub fn deno_json_deps(
config: &deno_config::deno_json::ConfigFile,
) -> Result<HashSet<JsrDepPackageReq>, AnyError> {
let import_map = config
.to_import_map_value(|path| {
std::fs::read_to_string(path).map_err(Into::into)
})?
.map(|(_, value)| value);
let values =
imports_values(import_map.as_ref().and_then(|m| m.get("imports")))
.into_iter()
.chain(scope_values(
import_map.as_ref().and_then(|m| m.get("scopes")),
));
) -> HashSet<JsrDepPackageReq> {
let values = imports_values(config.json.imports.as_ref())
.into_iter()
.chain(scope_values(config.json.scopes.as_ref()));
let mut set = values_to_set(values);

if let Some(serde_json::Value::Object(compiler_options)) =
Expand Down Expand Up @@ -113,7 +113,7 @@ pub fn deno_json_deps(
}
}

Ok(set)
set
}

fn imports_values(value: Option<&serde_json::Value>) -> Vec<&String> {
Expand Down
33 changes: 13 additions & 20 deletions cli/args/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::parking_lot::Mutex;
use deno_core::parking_lot::MutexGuard;
use deno_core::serde_json;
use deno_lockfile::WorkspaceMemberConfig;
use deno_package_json::PackageJsonDepValue;
use deno_runtime::deno_node::PackageJson;
use deno_semver::jsr::JsrDepPackageReq;

use crate::args::deno_json::import_map_deps;
use crate::cache;
use crate::util::fs::atomic_write_file_with_retries;
use crate::Flags;
Expand Down Expand Up @@ -101,6 +103,7 @@ impl CliLockfile {
pub fn discover(
flags: &Flags,
workspace: &Workspace,
maybe_external_import_map: Option<&serde_json::Value>,
) -> Result<Option<CliLockfile>, AnyError> {
fn pkg_json_deps(
maybe_pkg_json: Option<&PackageJson>,
Expand All @@ -126,13 +129,10 @@ impl CliLockfile {

fn deno_json_deps(
maybe_deno_json: Option<&ConfigFile>,
) -> Result<HashSet<JsrDepPackageReq>, AnyError> {
Ok(
maybe_deno_json
.map(crate::args::deno_json::deno_json_deps)
.transpose()?
.unwrap_or_default(),
)
) -> HashSet<JsrDepPackageReq> {
maybe_deno_json
.map(crate::args::deno_json::deno_json_deps)
.unwrap_or_default()
}

if flags.no_lock
Expand Down Expand Up @@ -174,7 +174,11 @@ impl CliLockfile {
let config = deno_lockfile::WorkspaceConfig {
root: WorkspaceMemberConfig {
package_json_deps: pkg_json_deps(root_folder.pkg_json.as_deref()),
dependencies: deno_json_deps(root_folder.deno_json.as_deref())?,
dependencies: if let Some(map) = maybe_external_import_map {
import_map_deps(map)
} else {
deno_json_deps(root_folder.deno_json.as_deref())
},
},
members: workspace
.config_folders()
Expand All @@ -195,18 +199,7 @@ impl CliLockfile {
{
let config = WorkspaceMemberConfig {
package_json_deps: pkg_json_deps(folder.pkg_json.as_deref()),
dependencies: deno_json_deps(folder.deno_json.as_deref())
.inspect_err(|err| {
log::warn!(
"failed to read dependencies from {}: {err}",
folder
.deno_json
.as_ref()
.map(|s| s.specifier.as_str())
.unwrap_or("config file")
)
})
.unwrap_or_default(),
dependencies: deno_json_deps(folder.deno_json.as_deref()),
};
if config.package_json_deps.is_empty()
&& config.dependencies.is_empty()
Expand Down
47 changes: 44 additions & 3 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,7 @@ impl CliOptions {
npmrc: Arc<ResolvedNpmRc>,
start_dir: Arc<WorkspaceDirectory>,
force_global_cache: bool,
maybe_external_import_map: Option<(PathBuf, serde_json::Value)>,
) -> Result<Self, AnyError> {
if let Some(insecure_allowlist) =
flags.unsafely_ignore_certificate_errors.as_ref()
Expand Down Expand Up @@ -858,6 +859,7 @@ impl CliOptions {
maybe_node_modules_folder,
overrides: Default::default(),
main_module_cell: std::sync::OnceLock::new(),
maybe_external_import_map,
start_dir,
deno_dir_provider,
})
Expand Down Expand Up @@ -933,7 +935,33 @@ impl CliOptions {

let (npmrc, _) = discover_npmrc_from_workspace(&start_dir.workspace)?;

let maybe_lock_file = CliLockfile::discover(&flags, &start_dir.workspace)?;
fn load_external_import_map(
deno_json: &ConfigFile,
) -> Result<Option<(PathBuf, serde_json::Value)>, AnyError> {
if !deno_json.is_an_import_map() {
if let Some(path) = deno_json.to_import_map_path()? {
let contents = std::fs::read_to_string(&path).with_context(|| {
format!("Unable to read import map at '{}'", path.display())
})?;
let map = serde_json::from_str(&contents)?;
return Ok(Some((path, map)));
}
}
Ok(None)
}

let external_import_map =
if let Some(deno_json) = start_dir.workspace.root_deno_json() {
load_external_import_map(deno_json)?
} else {
None
};

let maybe_lock_file = CliLockfile::discover(
&flags,
&start_dir.workspace,
external_import_map.as_ref().map(|(_, v)| v),
)?;

log::debug!("Finished config loading.");

Expand All @@ -944,6 +972,7 @@ impl CliOptions {
npmrc,
Arc::new(start_dir),
false,
external_import_map,
)
}

Expand Down Expand Up @@ -1064,7 +1093,7 @@ impl CliOptions {
file_fetcher: &FileFetcher,
pkg_json_dep_resolution: PackageJsonDepResolution,
) -> Result<WorkspaceResolver, AnyError> {
let overrode_no_import_map = self
let overrode_no_import_map: bool = self
.overrides
.import_map_specifier
.as_ref()
Expand Down Expand Up @@ -1092,7 +1121,19 @@ impl CliOptions {
value,
})
}
None => None,
None => {
if let Some((path, import_map)) =
self.maybe_external_import_map.as_ref()
{
let path_url = deno_path_util::url_from_file_path(&path)?;
Some(deno_config::workspace::SpecifiedImportMap {
base_url: path_url,
value: import_map.clone(),
})
} else {
None
}
}
}
};
Ok(self.workspace().create_resolver(
Expand Down
1 change: 1 addition & 0 deletions cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3676,6 +3676,7 @@ impl Inner {
.unwrap_or_else(create_default_npmrc),
workspace,
force_global_cache,
None,
)?;

let open_docs = self.documents.documents(DocumentsFilter::OpenDiagnosable);
Expand Down

0 comments on commit 4603997

Please sign in to comment.