1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-24 15:19:26 -05:00

refactor: deno_graph 0.43 upgrade (#17692)

This commit is contained in:
David Sherret 2023-02-09 22:00:23 -05:00 committed by GitHub
parent 8da235adce
commit b3e88e0681
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 1321 additions and 1570 deletions

1237
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -42,7 +42,7 @@ repository = "https://github.com/denoland/deno"
[workspace.dependencies]
v8 = { version = "0.62.2", default-features = false }
deno_ast = { version = "0.23.2", features = ["transpiling"] }
deno_ast = { version = "0.24.0", features = ["transpiling"] }
deno_core = { version = "0.171.0", path = "./core" }
deno_ops = { version = "0.49.0", path = "./ops" }
@ -146,6 +146,11 @@ incremental = true
lto = true
opt-level = 'z' # Optimize for size
# Build release with debug symbols: cargo build --profile=release-with-debug
[profile.release-with-debug]
inherits = "release"
debug = true
# NB: the `bench` and `release` profiles must remain EXACTLY the same.
[profile.bench]
codegen-units = 1

View file

@ -44,10 +44,10 @@ winres.workspace = true
[dependencies]
deno_ast = { workspace = true, features = ["bundler", "cjs", "codegen", "dep_graph", "module_specifier", "proposal", "react", "sourcemap", "transforms", "typescript", "view", "visit"] }
deno_core.workspace = true
deno_doc = "0.53.0"
deno_emit = "0.14.0"
deno_graph = "0.42.0"
deno_lint = { version = "0.37.0", features = ["docs"] }
deno_doc = "0.55.0"
deno_emit = "0.15.0"
deno_graph = "0.43.0"
deno_lint = { version = "0.38.0", features = ["docs"] }
deno_lockfile.workspace = true
deno_runtime.workspace = true
deno_task_shell = "0.8.1"
@ -66,10 +66,10 @@ data-url.workspace = true
dissimilar = "=1.0.4"
dprint-plugin-json = "=0.17.0"
dprint-plugin-markdown = "=0.15.2"
dprint-plugin-typescript = "=0.81.1"
dprint-plugin-typescript = "=0.83.0"
encoding_rs.workspace = true
env_logger = "=0.9.0"
eszip = "=0.33.0"
eszip = "=0.35.0"
fancy-regex = "=0.10.0"
flate2.workspace = true
http.workspace = true

View file

@ -26,7 +26,7 @@ use std::path::Path;
use std::path::PathBuf;
pub type MaybeImportsResult =
Result<Option<Vec<(ModuleSpecifier, Vec<String>)>>, AnyError>;
Result<Vec<deno_graph::ReferrerImports>, AnyError>;
#[derive(Hash)]
pub struct JsxImportSourceConfig {
@ -765,7 +765,7 @@ impl ConfigFile {
if let Some(value) = self.json.compiler_options.as_ref() {
value
} else {
return Ok(None);
return Ok(Vec::new());
};
let compiler_options: CompilerOptions =
serde_json::from_value(compiler_options_value.clone())?;
@ -774,9 +774,9 @@ impl ConfigFile {
}
if !imports.is_empty() {
let referrer = self.specifier.clone();
Ok(Some(vec![(referrer, imports)]))
Ok(vec![deno_graph::ReferrerImports { referrer, imports }])
} else {
Ok(None)
Ok(Vec::new())
}
}

View file

@ -690,16 +690,10 @@ impl CliOptions {
/// Return any imports that should be brought into the scope of the module
/// graph.
pub fn to_maybe_imports(&self) -> MaybeImportsResult {
let mut imports = Vec::new();
if let Some(config_file) = &self.maybe_config_file {
if let Some(config_imports) = config_file.to_maybe_imports()? {
imports.extend(config_imports);
}
}
if imports.is_empty() {
Ok(None)
config_file.to_maybe_imports()
} else {
Ok(Some(imports))
Ok(Vec::new())
}
}

View file

@ -10,6 +10,7 @@ const path = new URL("./deno_http_flash_ops.js", import.meta.url).pathname;
const cpus = navigator.hardwareConcurrency / 2;
const processes = new Array(cpus);
for (let i = 0; i < cpus; i++) {
// deno-lint-ignore no-deprecated-deno-api
const proc = Deno.run({
cmd: [executable, "run", "-A", "--unstable", path, Deno.args[0]],
});

View file

@ -10,6 +10,7 @@ const path = new URL("./deno_http_flash.js", import.meta.url).pathname;
const cpus = navigator.hardwareConcurrency / 2;
const processes = new Array(cpus);
for (let i = 0; i < cpus; i++) {
// deno-lint-ignore no-deprecated-deno-api
const proc = Deno.run({
cmd: [executable, "run", "-A", "--unstable", path, Deno.args[0]],
});

View file

@ -25,15 +25,17 @@ fn get_diagnostic_class(_: &Diagnostic) -> &'static str {
fn get_module_graph_error_class(err: &ModuleGraphError) -> &'static str {
match err {
ModuleGraphError::LoadingErr(_, err) => get_error_class_name(err.as_ref()),
ModuleGraphError::LoadingErr(_, _, err) => {
get_error_class_name(err.as_ref())
}
ModuleGraphError::InvalidTypeAssertion { .. } => "SyntaxError",
ModuleGraphError::ParseErr(_, diagnostic) => {
get_diagnostic_class(diagnostic)
}
ModuleGraphError::ResolutionError(err) => get_resolution_error_class(err),
ModuleGraphError::UnsupportedMediaType(_, _)
| ModuleGraphError::UnsupportedImportAssertionType(_, _) => "TypeError",
ModuleGraphError::Missing(_) => "NotFound",
ModuleGraphError::UnsupportedMediaType { .. }
| ModuleGraphError::UnsupportedImportAssertionType { .. } => "TypeError",
ModuleGraphError::Missing(_, _) => "NotFound",
}
}

View file

@ -1,16 +1,14 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::args::CliOptions;
use crate::args::Lockfile;
use crate::args::TsConfigType;
use crate::args::TsTypeLib;
use crate::args::TypeCheckMode;
use crate::cache;
use crate::cache::TypeCheckCache;
use crate::colors;
use crate::errors::get_error_class_name;
use crate::npm::resolve_graph_npm_info;
use crate::npm::NpmPackageReference;
use crate::npm::NpmPackageReq;
use crate::proc_state::ProcState;
use crate::resolver::CliResolver;
use crate::tools::check;
@ -18,470 +16,61 @@ use crate::tools::check;
use deno_core::anyhow::bail;
use deno_core::error::custom_error;
use deno_core::error::AnyError;
use deno_core::parking_lot::RwLock;
use deno_core::ModuleSpecifier;
use deno_graph::Dependency;
use deno_graph::GraphImport;
use deno_graph::MediaType;
use deno_graph::ModuleGraph;
use deno_graph::ModuleGraphError;
use deno_graph::ModuleKind;
use deno_graph::Range;
use deno_graph::ResolutionError;
use deno_graph::Resolved;
use deno_graph::SpecifierError;
use deno_runtime::permissions::PermissionsContainer;
use import_map::ImportMapError;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::VecDeque;
use std::sync::Arc;
#[derive(Debug, Clone)]
#[allow(clippy::large_enum_variant)]
pub enum ModuleEntry {
Module {
code: Arc<str>,
dependencies: BTreeMap<String, Dependency>,
media_type: MediaType,
/// A set of type libs that the module has passed a type check with this
/// session. This would consist of window, worker or both.
checked_libs: HashSet<TsTypeLib>,
maybe_types: Option<Resolved>,
/// Check if `roots` and their deps are available. Returns `Ok(())` if
/// so. Returns `Err(_)` if there is a known module graph or resolution
/// error statically reachable from `roots` and not a dynamic import.
pub fn graph_valid_with_cli_options(
graph: &ModuleGraph,
roots: &[ModuleSpecifier],
options: &CliOptions,
) -> Result<(), AnyError> {
graph_valid(
graph,
roots,
deno_graph::WalkOptions {
follow_dynamic: false,
follow_type_only: options.type_check_mode() != TypeCheckMode::None,
check_js: options.check_js(),
},
Error(ModuleGraphError),
Redirect(ModuleSpecifier),
)
}
/// Composes data from potentially many `ModuleGraph`s.
#[derive(Debug, Default)]
pub struct GraphData {
modules: HashMap<ModuleSpecifier, ModuleEntry>,
/// Specifiers that are built-in or external.
external_specifiers: HashSet<ModuleSpecifier>,
npm_packages: Vec<NpmPackageReq>,
has_node_builtin_specifier: bool,
/// Map of first known referrer locations for each module. Used to enhance
/// error messages.
referrer_map: HashMap<ModuleSpecifier, Box<Range>>,
graph_imports: Vec<GraphImport>,
}
impl GraphData {
/// Store data from `graph` into `self`.
pub fn add_graph(&mut self, graph: &ModuleGraph) {
for graph_import in &graph.imports {
for dep in graph_import.dependencies.values() {
for resolved in [&dep.maybe_code, &dep.maybe_type] {
if let Resolved::Ok {
specifier, range, ..
} = resolved
{
let entry = self.referrer_map.entry(specifier.clone());
entry.or_insert_with(|| range.clone());
}
}
}
self.graph_imports.push(graph_import.clone())
}
let mut has_npm_specifier_in_graph = false;
for (specifier, result) in graph.specifiers() {
if self.modules.contains_key(specifier) {
continue;
}
if !self.has_node_builtin_specifier && specifier.scheme() == "node" {
self.has_node_builtin_specifier = true;
}
if let Some(found) = graph.redirects.get(specifier) {
let module_entry = ModuleEntry::Redirect(found.clone());
self.modules.insert(specifier.clone(), module_entry);
continue;
}
match result {
Ok((_, module_kind, media_type)) => {
if module_kind == ModuleKind::External {
if !has_npm_specifier_in_graph
&& NpmPackageReference::from_specifier(specifier).is_ok()
{
has_npm_specifier_in_graph = true;
}
self.external_specifiers.insert(specifier.clone());
continue; // ignore npm and node specifiers
}
let module = graph.get(specifier).unwrap();
let code = match &module.maybe_source {
Some(source) => source.clone(),
None => continue,
};
let maybe_types = module
.maybe_types_dependency
.as_ref()
.map(|(_, r)| r.clone());
if let Some(Resolved::Ok {
specifier, range, ..
}) = &maybe_types
{
let specifier = graph.redirects.get(specifier).unwrap_or(specifier);
let entry = self.referrer_map.entry(specifier.clone());
entry.or_insert_with(|| range.clone());
}
for dep in module.dependencies.values() {
#[allow(clippy::manual_flatten)]
for resolved in [&dep.maybe_code, &dep.maybe_type] {
if let Resolved::Ok {
specifier, range, ..
} = resolved
{
let specifier =
graph.redirects.get(specifier).unwrap_or(specifier);
let entry = self.referrer_map.entry(specifier.clone());
entry.or_insert_with(|| range.clone());
}
}
}
let module_entry = ModuleEntry::Module {
code,
dependencies: module.dependencies.clone(),
media_type,
checked_libs: Default::default(),
maybe_types,
};
self.modules.insert(specifier.clone(), module_entry);
}
Err(error) => {
let module_entry = ModuleEntry::Error(error.clone());
self.modules.insert(specifier.clone(), module_entry);
}
}
}
if has_npm_specifier_in_graph {
self
.npm_packages
.extend(resolve_graph_npm_info(graph).package_reqs);
}
}
pub fn entries(
&self,
) -> impl Iterator<Item = (&ModuleSpecifier, &ModuleEntry)> {
self.modules.iter()
}
/// Gets if the graph had a "node:" specifier.
pub fn has_node_builtin_specifier(&self) -> bool {
self.has_node_builtin_specifier
}
/// Gets the npm package requirements from all the encountered graphs
/// in the order that they should be resolved.
pub fn npm_package_reqs(&self) -> &Vec<NpmPackageReq> {
&self.npm_packages
}
/// Walk dependencies from `roots` and return every encountered specifier.
/// Return `None` if any modules are not known.
pub fn walk<'a>(
&'a self,
roots: &[ModuleSpecifier],
follow_dynamic: bool,
follow_type_only: bool,
check_js: bool,
) -> Option<HashMap<&'a ModuleSpecifier, &'a ModuleEntry>> {
let mut result = HashMap::<&'a ModuleSpecifier, &'a ModuleEntry>::new();
let mut seen = HashSet::<&ModuleSpecifier>::new();
let mut visiting = VecDeque::<&ModuleSpecifier>::new();
for root in roots {
seen.insert(root);
visiting.push_back(root);
}
for (_, dep) in self.graph_imports.iter().flat_map(|i| &i.dependencies) {
let mut resolutions = vec![&dep.maybe_code];
if follow_type_only {
resolutions.push(&dep.maybe_type);
}
#[allow(clippy::manual_flatten)]
for resolved in resolutions {
if let Resolved::Ok { specifier, .. } = resolved {
if !seen.contains(specifier) {
seen.insert(specifier);
visiting.push_front(specifier);
}
}
}
}
while let Some(specifier) = visiting.pop_front() {
let (specifier, entry) = match self.modules.get_key_value(specifier) {
Some(pair) => pair,
None => {
if self.external_specifiers.contains(specifier) {
continue;
}
return None;
}
};
result.insert(specifier, entry);
match entry {
ModuleEntry::Module {
dependencies,
maybe_types,
media_type,
..
} => {
let check_types = (check_js
|| !matches!(
media_type,
MediaType::JavaScript
| MediaType::Mjs
| MediaType::Cjs
| MediaType::Jsx
))
&& follow_type_only;
if check_types {
if let Some(Resolved::Ok { specifier, .. }) = maybe_types {
if !seen.contains(specifier) {
seen.insert(specifier);
visiting.push_front(specifier);
}
}
}
for (dep_specifier, dep) in dependencies.iter().rev() {
// todo(dsherret): ideally there would be a way to skip external dependencies
// in the graph here rather than specifically npm package references
if NpmPackageReference::from_str(dep_specifier).is_ok() {
continue;
}
if !dep.is_dynamic || follow_dynamic {
let mut resolutions = vec![&dep.maybe_code];
if check_types {
resolutions.push(&dep.maybe_type);
}
#[allow(clippy::manual_flatten)]
for resolved in resolutions {
if let Resolved::Ok { specifier, .. } = resolved {
if !seen.contains(specifier) {
seen.insert(specifier);
visiting.push_front(specifier);
}
}
}
}
}
}
ModuleEntry::Error(_) => {}
ModuleEntry::Redirect(specifier) => {
if !seen.contains(specifier) {
seen.insert(specifier);
visiting.push_front(specifier);
}
}
}
}
Some(result)
}
/// Clone part of `self`, containing only modules which are dependencies of
/// `roots`. Returns `None` if any roots are not known.
pub fn graph_segment(&self, roots: &[ModuleSpecifier]) -> Option<Self> {
let mut modules = HashMap::new();
let mut referrer_map = HashMap::new();
let entries = match self.walk(roots, true, true, true) {
Some(entries) => entries,
None => return None,
};
for (specifier, module_entry) in entries {
modules.insert(specifier.clone(), module_entry.clone());
if let Some(referrer) = self.referrer_map.get(specifier) {
referrer_map.insert(specifier.clone(), referrer.clone());
}
}
Some(Self {
modules,
external_specifiers: self.external_specifiers.clone(),
has_node_builtin_specifier: self.has_node_builtin_specifier,
npm_packages: self.npm_packages.clone(),
referrer_map,
graph_imports: self.graph_imports.to_vec(),
})
}
/// Check if `roots` and their deps are available. Returns `Some(Ok(()))` if
/// so. Returns `Some(Err(_))` if there is a known module graph or resolution
/// error statically reachable from `roots`. Returns `None` if any modules are
/// not known.
pub fn check(
&self,
roots: &[ModuleSpecifier],
follow_type_only: bool,
check_js: bool,
) -> Option<Result<(), AnyError>> {
let entries = match self.walk(roots, false, follow_type_only, check_js) {
Some(entries) => entries,
None => return None,
};
for (specifier, module_entry) in entries {
match module_entry {
ModuleEntry::Module {
dependencies,
maybe_types,
media_type,
..
} => {
let check_types = (check_js
|| !matches!(
media_type,
MediaType::JavaScript
| MediaType::Mjs
| MediaType::Cjs
| MediaType::Jsx
))
&& follow_type_only;
if check_types {
if let Some(Resolved::Err(error)) = maybe_types {
let range = error.range();
return Some(handle_check_error(
error.clone().into(),
Some(range),
));
}
}
for (_, dep) in dependencies.iter() {
if !dep.is_dynamic {
let mut resolutions = vec![&dep.maybe_code];
if check_types {
resolutions.push(&dep.maybe_type);
}
#[allow(clippy::manual_flatten)]
for resolved in resolutions {
if let Resolved::Err(error) = resolved {
let range = error.range();
return Some(handle_check_error(
error.clone().into(),
Some(range),
));
}
}
}
}
}
ModuleEntry::Error(error) => {
let maybe_range = if roots.contains(specifier) {
None
} else {
self.referrer_map.get(specifier)
};
return Some(handle_check_error(
error.clone().into(),
maybe_range.map(|r| &**r),
));
}
_ => {}
}
}
Some(Ok(()))
}
/// Mark `roots` and all of their dependencies as type checked under `lib`.
/// Assumes that all of those modules are known.
pub fn set_type_checked(
&mut self,
roots: &[ModuleSpecifier],
lib: TsTypeLib,
) {
let specifiers: Vec<ModuleSpecifier> =
match self.walk(roots, true, true, true) {
Some(entries) => entries.into_keys().cloned().collect(),
None => unreachable!("contains module not in graph data"),
};
for specifier in specifiers {
if let ModuleEntry::Module { checked_libs, .. } =
self.modules.get_mut(&specifier).unwrap()
{
checked_libs.insert(lib);
}
}
}
/// Check if `roots` are all marked as type checked under `lib`.
pub fn is_type_checked(
&self,
roots: &[ModuleSpecifier],
lib: &TsTypeLib,
) -> bool {
roots.iter().all(|r| {
let found = self.follow_redirect(r);
match self.modules.get(&found) {
Some(ModuleEntry::Module { checked_libs, .. }) => {
checked_libs.contains(lib)
}
_ => false,
}
})
}
/// If `specifier` is known and a redirect, return the found specifier.
/// Otherwise return `specifier`.
pub fn follow_redirect(
&self,
specifier: &ModuleSpecifier,
) -> ModuleSpecifier {
match self.modules.get(specifier) {
Some(ModuleEntry::Redirect(s)) => s.clone(),
_ => specifier.clone(),
}
}
pub fn get<'a>(
&'a self,
specifier: &ModuleSpecifier,
) -> Option<&'a ModuleEntry> {
self.modules.get(specifier)
}
/// Get the dependencies of a module or graph import.
pub fn get_dependencies<'a>(
&'a self,
specifier: &ModuleSpecifier,
) -> Option<&'a BTreeMap<String, Dependency>> {
let specifier = self.follow_redirect(specifier);
if let Some(ModuleEntry::Module { dependencies, .. }) = self.get(&specifier)
{
return Some(dependencies);
}
if let Some(graph_import) =
self.graph_imports.iter().find(|i| i.referrer == specifier)
{
return Some(&graph_import.dependencies);
}
None
}
}
impl From<&ModuleGraph> for GraphData {
fn from(graph: &ModuleGraph) -> Self {
let mut graph_data = GraphData::default();
graph_data.add_graph(graph);
graph_data
}
}
/// Like `graph.valid()`, but enhanced with referrer info.
/// Check if `roots` and their deps are available. Returns `Ok(())` if
/// so. Returns `Err(_)` if there is a known module graph or resolution
/// error statically reachable from `roots`.
///
/// It is preferable to use this over using deno_graph's API directly
/// because it will have enhanced error message information specifically
/// for the CLI.
pub fn graph_valid(
graph: &ModuleGraph,
follow_type_only: bool,
check_js: bool,
roots: &[ModuleSpecifier],
walk_options: deno_graph::WalkOptions,
) -> Result<(), AnyError> {
GraphData::from(graph)
.check(&graph.roots, follow_type_only, check_js)
.unwrap()
graph.walk(roots, walk_options).validate().map_err(|error| {
let mut message = if let ModuleGraphError::ResolutionError(err) = &error {
enhanced_resolution_error_message(err)
} else {
format!("{error}")
};
if let Some(range) = error.maybe_range() {
if !range.specifier.as_str().contains("/$deno$eval") {
message.push_str(&format!("\n at {range}"));
}
}
custom_error(get_error_class_name(&error.into()), message)
})
}
/// Checks the lockfile against the graph and and exits on errors.
@ -523,11 +112,12 @@ pub async fn create_graph_and_maybe_check(
let maybe_graph_resolver =
maybe_cli_resolver.as_ref().map(|r| r.as_graph_resolver());
let analyzer = ps.parsed_source_cache.as_analyzer();
let graph = Arc::new(
deno_graph::create_graph(
let mut graph = ModuleGraph::default();
graph
.build(
vec![root],
&mut cache,
deno_graph::GraphOptions {
deno_graph::BuildOptions {
is_dynamic: false,
imports: maybe_imports,
resolver: maybe_graph_resolver,
@ -535,21 +125,12 @@ pub async fn create_graph_and_maybe_check(
reporter: None,
},
)
.await,
);
let check_js = ps.options.check_js();
let mut graph_data = GraphData::default();
graph_data.add_graph(&graph);
graph_data
.check(
&graph.roots,
ps.options.type_check_mode() != TypeCheckMode::None,
check_js,
)
.unwrap()?;
.await;
graph_valid_with_cli_options(&graph, &graph.roots, &ps.options)?;
let graph = Arc::new(graph);
let npm_graph_info = resolve_graph_npm_info(&graph);
ps.npm_resolver
.add_package_reqs(graph_data.npm_package_reqs().clone())
.add_package_reqs(npm_graph_info.package_reqs)
.await?;
if let Some(lockfile) = &ps.lockfile {
graph_lock_or_exit(&graph, &mut lockfile.lock());
@ -558,7 +139,7 @@ pub async fn create_graph_and_maybe_check(
if ps.options.type_check_mode() != TypeCheckMode::None {
// node built-in specifiers use the @types/node package to determine
// types, so inject that now after the lockfile has been written
if graph_data.has_node_builtin_specifier() {
if npm_graph_info.has_node_builtin_specifier {
ps.npm_resolver
.inject_synthetic_types_node_package()
.await?;
@ -574,8 +155,7 @@ pub async fn create_graph_and_maybe_check(
let maybe_config_specifier = ps.options.maybe_config_file_specifier();
let cache = TypeCheckCache::new(&ps.dir.type_checking_cache_db_file_path());
let check_result = check::check(
&graph.roots,
Arc::new(RwLock::new(graph_data)),
graph.clone(),
&cache,
&ps.npm_resolver,
check::CheckOptions {
@ -585,6 +165,7 @@ pub async fn create_graph_and_maybe_check(
ts_config: ts_config_result.ts_config,
log_checks: true,
reload: ps.options.reload_flag(),
has_node_builtin_specifier: npm_graph_info.has_node_builtin_specifier,
},
)?;
log::debug!("{}", check_result.stats);
@ -602,8 +183,8 @@ pub fn error_for_any_npm_specifier(
let first_npm_specifier = graph
.specifiers()
.filter_map(|(_, r)| match r {
Ok((specifier, kind, _)) if kind == deno_graph::ModuleKind::External => {
Some(specifier)
Ok(module) if module.kind == deno_graph::ModuleKind::External => {
Some(&module.specifier)
}
_ => None,
})
@ -615,25 +196,6 @@ pub fn error_for_any_npm_specifier(
}
}
fn handle_check_error(
error: AnyError,
maybe_range: Option<&deno_graph::Range>,
) -> Result<(), AnyError> {
let mut message = if let Some(err) = error.downcast_ref::<ResolutionError>() {
enhanced_resolution_error_message(err)
} else {
format!("{error}")
};
if let Some(range) = maybe_range {
if !range.specifier.as_str().contains("$deno") {
message.push_str(&format!("\n at {range}"));
}
}
Err(custom_error(get_error_class_name(&error), message))
}
/// Adds more explanatory information to a resolution error.
pub fn enhanced_resolution_error_message(error: &ResolutionError) -> String {
let mut message = format!("{error}");

View file

@ -27,8 +27,8 @@ use deno_core::serde::Deserialize;
use deno_core::serde_json;
use deno_core::serde_json::json;
use deno_core::ModuleSpecifier;
use deno_graph::Resolution;
use deno_graph::ResolutionError;
use deno_graph::Resolved;
use deno_graph::SpecifierError;
use deno_lint::rules::LintRule;
use deno_runtime::tokio_util::create_basic_runtime;
@ -852,18 +852,17 @@ impl DenoDiagnostic {
}
}
fn diagnose_resolved(
fn diagnose_resolution(
diagnostics: &mut Vec<lsp::Diagnostic>,
snapshot: &language_server::StateSnapshot,
resolved: &deno_graph::Resolved,
resolution: &Resolution,
is_dynamic: bool,
maybe_assert_type: Option<&str>,
) {
match resolved {
Resolved::Ok {
specifier, range, ..
} => {
let range = documents::to_lsp_range(range);
match resolution {
Resolution::Ok(resolved) => {
let specifier = &resolved.specifier;
let range = documents::to_lsp_range(&resolved.range);
// If the module is a remote module and has a `X-Deno-Warning` header, we
// want a warning diagnostic with that message.
if let Some(metadata) = snapshot.cache_metadata.get(specifier) {
@ -959,8 +958,8 @@ fn diagnose_resolved(
}
// The specifier resolution resulted in an error, so we want to issue a
// diagnostic for that.
Resolved::Err(err) => diagnostics.push(
DenoDiagnostic::ResolutionError(err.clone())
Resolution::Err(err) => diagnostics.push(
DenoDiagnostic::ResolutionError(*err.clone())
.to_lsp_diagnostic(&documents::to_lsp_range(err.range())),
),
_ => (),
@ -984,31 +983,28 @@ fn diagnose_dependency(
}
if let Some(import_map) = &snapshot.maybe_import_map {
if let Resolved::Ok {
specifier, range, ..
} = &dependency.maybe_code
{
if let Some(to) = import_map.lookup(specifier, referrer) {
if let Resolution::Ok(resolved) = &dependency.maybe_code {
if let Some(to) = import_map.lookup(&resolved.specifier, referrer) {
if dependency_key != to {
diagnostics.push(
DenoDiagnostic::ImportMapRemap {
from: dependency_key.to_string(),
to,
}
.to_lsp_diagnostic(&documents::to_lsp_range(range)),
.to_lsp_diagnostic(&documents::to_lsp_range(&resolved.range)),
);
}
}
}
}
diagnose_resolved(
diagnose_resolution(
diagnostics,
snapshot,
&dependency.maybe_code,
dependency.is_dynamic,
dependency.maybe_assert_type.as_deref(),
);
diagnose_resolved(
diagnose_resolution(
diagnostics,
snapshot,
&dependency.maybe_type,
@ -1064,6 +1060,7 @@ mod tests {
use crate::lsp::documents::Documents;
use crate::lsp::documents::LanguageId;
use crate::lsp::language_server::StateSnapshot;
use pretty_assertions::assert_eq;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

View file

@ -33,7 +33,7 @@ use deno_core::parking_lot::Mutex;
use deno_core::url;
use deno_core::ModuleSpecifier;
use deno_graph::GraphImport;
use deno_graph::Resolved;
use deno_graph::Resolution;
use deno_runtime::deno_node::NodeResolutionMode;
use deno_runtime::permissions::PermissionsContainer;
use once_cell::sync::Lazy;
@ -243,7 +243,7 @@ impl AssetOrDocument {
#[derive(Debug, Default)]
struct DocumentDependencies {
deps: BTreeMap<String, deno_graph::Dependency>,
maybe_types_dependency: Option<(String, Resolved)>,
maybe_types_dependency: Option<deno_graph::TypesDependency>,
}
impl DocumentDependencies {
@ -508,13 +508,12 @@ impl Document {
self.0.maybe_lsp_version.is_some()
}
pub fn maybe_types_dependency(&self) -> deno_graph::Resolved {
if let Some((_, maybe_dep)) =
self.0.dependencies.maybe_types_dependency.as_ref()
pub fn maybe_types_dependency(&self) -> Resolution {
if let Some(types_dep) = self.0.dependencies.maybe_types_dependency.as_ref()
{
maybe_dep.clone()
types_dep.dependency.clone()
} else {
deno_graph::Resolved::None
Resolution::None
}
}
@ -597,9 +596,11 @@ impl Document {
}
}
pub fn to_hover_text(result: &Resolved) -> String {
pub fn to_hover_text(result: &Resolution) -> String {
match result {
Resolved::Ok { specifier, .. } => match specifier.scheme() {
Resolution::Ok(resolved) => {
let specifier = &resolved.specifier;
match specifier.scheme() {
"data" => "_(a data url)_".to_string(),
"blob" => "_(a blob url)_".to_string(),
_ => format!(
@ -608,9 +609,10 @@ pub fn to_hover_text(result: &Resolved) -> String {
&specifier[url::Position::AfterScheme..],
)
.replace('@', "&#8203;@"),
},
Resolved::Err(_) => "_[errored]_".to_string(),
Resolved::None => "_[missing]_".to_string(),
}
}
Resolution::Err(_) => "_[errored]_".to_string(),
Resolution::None => "_[missing]_".to_string(),
}
}
@ -1094,15 +1096,16 @@ impl Documents {
} else if let Some(dep) =
dependencies.as_ref().and_then(|d| d.deps.get(&specifier))
{
if let Resolved::Ok { specifier, .. } = &dep.maybe_type {
if let Some(specifier) = dep.maybe_type.maybe_specifier() {
results.push(self.resolve_dependency(specifier, maybe_npm_resolver));
} else if let Resolved::Ok { specifier, .. } = &dep.maybe_code {
} else if let Some(specifier) = dep.maybe_code.maybe_specifier() {
results.push(self.resolve_dependency(specifier, maybe_npm_resolver));
} else {
results.push(None);
}
} else if let Some(Resolved::Ok { specifier, .. }) =
self.resolve_imports_dependency(&specifier)
} else if let Some(specifier) = self
.resolve_imports_dependency(&specifier)
.and_then(|r| r.maybe_specifier())
{
results.push(self.resolve_dependency(specifier, maybe_npm_resolver));
} else if let Ok(npm_ref) = NpmPackageReference::from_str(&specifier) {
@ -1186,18 +1189,18 @@ impl Documents {
self.maybe_resolver =
CliResolver::maybe_new(maybe_jsx_config, maybe_import_map);
self.imports = Arc::new(
if let Some(Ok(Some(imports))) =
if let Some(Ok(imports)) =
maybe_config_file.map(|cf| cf.to_maybe_imports())
{
imports
.into_iter()
.map(|(referrer, dependencies)| {
.map(|import| {
let graph_import = GraphImport::new(
referrer.clone(),
dependencies,
&import.referrer,
import.imports,
self.get_maybe_resolver(),
);
(referrer, graph_import)
(import.referrer, graph_import)
})
.collect()
} else {
@ -1274,10 +1277,8 @@ impl Documents {
self.add(dep, specifier);
}
}
if let Resolved::Ok { specifier: dep, .. } =
doc.maybe_types_dependency()
{
self.add(&dep, specifier);
if let Some(dep) = doc.maybe_types_dependency().maybe_specifier() {
self.add(dep, specifier);
}
}
}
@ -1345,13 +1346,12 @@ impl Documents {
}
let doc = self.get(specifier)?;
let maybe_module = doc.maybe_module().and_then(|r| r.as_ref().ok());
let maybe_types_dependency = maybe_module.and_then(|m| {
m.maybe_types_dependency
.as_ref()
.map(|(_, resolved)| resolved.clone())
});
if let Some(Resolved::Ok { specifier, .. }) = maybe_types_dependency {
self.resolve_dependency(&specifier, maybe_npm_resolver)
let maybe_types_dependency = maybe_module
.and_then(|m| m.maybe_types_dependency.as_ref().map(|d| &d.dependency));
if let Some(specifier) =
maybe_types_dependency.and_then(|d| d.maybe_specifier())
{
self.resolve_dependency(specifier, maybe_npm_resolver)
} else {
let media_type = doc.media_type();
Some((specifier.clone(), media_type))
@ -1361,10 +1361,7 @@ impl Documents {
/// Iterate through any "imported" modules, checking to see if a dependency
/// is available. This is used to provide "global" imports like the JSX import
/// source.
fn resolve_imports_dependency(
&self,
specifier: &str,
) -> Option<&deno_graph::Resolved> {
fn resolve_imports_dependency(&self, specifier: &str) -> Option<&Resolution> {
for graph_imports in self.imports.values() {
let maybe_dep = graph_imports.dependencies.get(specifier);
if maybe_dep.is_some() {

View file

@ -173,8 +173,18 @@ impl LanguageServer {
inner_loader: &mut inner_loader,
open_docs: &open_docs,
};
let graph = ps.create_graph_with_loader(roots, &mut loader).await?;
graph_valid(&graph, true, false)?;
let graph = ps
.create_graph_with_loader(roots.clone(), &mut loader)
.await?;
graph_valid(
&graph,
&roots,
deno_graph::WalkOptions {
follow_dynamic: false,
follow_type_only: true,
check_js: false,
},
)?;
Ok(())
}

View file

@ -2,7 +2,6 @@
use crate::args::TsTypeLib;
use crate::emit::emit_parsed_source;
use crate::graph_util::ModuleEntry;
use crate::node;
use crate::proc_state::ProcState;
use crate::util::text_encoding::code_without_source_map;
@ -85,11 +84,13 @@ impl CliModuleLoader {
media_type: MediaType::JavaScript,
});
}
let graph_data = self.ps.graph_data.read();
let found_url = graph_data.follow_redirect(specifier);
match graph_data.get(&found_url) {
Some(ModuleEntry::Module {
code, media_type, ..
let graph = self.ps.graph();
match graph.get(specifier) {
Some(deno_graph::Module {
maybe_source: Some(code),
media_type,
specifier,
..
}) => {
let code = match media_type {
MediaType::JavaScript
@ -107,7 +108,7 @@ impl CliModuleLoader {
emit_parsed_source(
&self.ps.emit_cache,
&self.ps.parsed_source_cache,
&found_url,
specifier,
*media_type,
code,
&self.ps.emit_options,
@ -115,7 +116,7 @@ impl CliModuleLoader {
)?
}
MediaType::TsBuildInfo | MediaType::Wasm | MediaType::SourceMap => {
panic!("Unexpected media type {media_type} for {found_url}")
panic!("Unexpected media type {media_type} for {specifier}")
}
};
@ -124,7 +125,7 @@ impl CliModuleLoader {
Ok(ModuleCodeSource {
code,
found_url,
found_url: specifier.clone(),
media_type: *media_type,
})
}
@ -295,10 +296,12 @@ impl SourceMapGetter for CliModuleLoader {
file_name: &str,
line_number: usize,
) -> Option<String> {
let graph_data = self.ps.graph_data.read();
let specifier = graph_data.follow_redirect(&resolve_url(file_name).ok()?);
let code = match graph_data.get(&specifier) {
Some(ModuleEntry::Module { code, .. }) => code,
let graph = self.ps.graph();
let code = match graph.get(&resolve_url(file_name).ok()?) {
Some(deno_graph::Module {
maybe_source: Some(code),
..
}) => code,
_ => return None,
};
// Do NOT use .lines(): it skips the terminating empty line.

View file

@ -7,7 +7,6 @@ use std::collections::VecDeque;
use deno_ast::ModuleSpecifier;
use deno_graph::ModuleGraph;
use deno_graph::Resolved;
use crate::semver::VersionReq;
@ -70,14 +69,17 @@ pub fn resolve_graph_npm_info(graph: &ModuleGraph) -> GraphNpmInfo {
module: &'a deno_graph::Module,
) -> Vec<&'a ModuleSpecifier> {
let mut specifiers = Vec::with_capacity(module.dependencies.len() * 2 + 1);
let maybe_types = module.maybe_types_dependency.as_ref().map(|(_, r)| r);
if let Some(Resolved::Ok { specifier, .. }) = &maybe_types {
let maybe_types = module
.maybe_types_dependency
.as_ref()
.map(|d| &d.dependency);
if let Some(specifier) = maybe_types.and_then(|d| d.maybe_specifier()) {
specifiers.push(specifier);
}
for dep in module.dependencies.values() {
#[allow(clippy::manual_flatten)]
for resolved in [&dep.maybe_code, &dep.maybe_type] {
if let Resolved::Ok { specifier, .. } = resolved {
if let Some(specifier) = resolved.maybe_specifier() {
specifiers.push(specifier);
}
}
@ -686,7 +688,9 @@ mod tests {
Vec::new(),
);
let analyzer = deno_graph::CapturingModuleAnalyzer::default();
let graph = deno_graph::create_graph(
let mut graph = deno_graph::ModuleGraph::default();
graph
.build(
vec![
ModuleSpecifier::parse("file:///dev/local_module_a/mod.ts").unwrap(),
// test redirect at root
@ -694,12 +698,9 @@ mod tests {
.unwrap(),
],
&mut loader,
deno_graph::GraphOptions {
is_dynamic: false,
imports: None,
resolver: None,
deno_graph::BuildOptions {
module_analyzer: Some(&analyzer),
reporter: None,
..Default::default()
},
)
.await;

View file

@ -18,14 +18,14 @@ use crate::cache::TypeCheckCache;
use crate::emit::emit_parsed_source;
use crate::file_fetcher::FileFetcher;
use crate::graph_util::graph_lock_or_exit;
use crate::graph_util::GraphData;
use crate::graph_util::ModuleEntry;
use crate::graph_util::graph_valid_with_cli_options;
use crate::http_util::HttpClient;
use crate::node;
use crate::node::NodeResolution;
use crate::npm::resolve_graph_npm_info;
use crate::npm::NpmCache;
use crate::npm::NpmPackageReference;
use crate::npm::NpmPackageReq;
use crate::npm::NpmPackageResolver;
use crate::npm::RealNpmRegistryApi;
use crate::resolver::CliResolver;
@ -39,19 +39,17 @@ use deno_core::anyhow::Context;
use deno_core::error::custom_error;
use deno_core::error::generic_error;
use deno_core::error::AnyError;
use deno_core::futures;
use deno_core::parking_lot::Mutex;
use deno_core::parking_lot::RwLock;
use deno_core::resolve_url_or_path;
use deno_core::CompiledWasmModuleStore;
use deno_core::ModuleSpecifier;
use deno_core::SharedArrayBufferStore;
use deno_graph::create_graph;
use deno_graph::source::CacheInfo;
use deno_graph::source::LoadFuture;
use deno_graph::source::Loader;
use deno_graph::source::Resolver;
use deno_graph::Resolved;
use deno_graph::ModuleGraph;
use deno_graph::ModuleKind;
use deno_graph::Resolution;
use deno_runtime::deno_broadcast_channel::InMemoryBroadcastChannel;
use deno_runtime::deno_node::NodeResolutionMode;
use deno_runtime::deno_tls::rustls::RootCertStore;
@ -60,6 +58,7 @@ use deno_runtime::inspector_server::InspectorServer;
use deno_runtime::permissions::PermissionsContainer;
use import_map::ImportMap;
use log::warn;
use std::collections::HashMap;
use std::collections::HashSet;
use std::ops::Deref;
use std::path::PathBuf;
@ -81,7 +80,7 @@ pub struct Inner {
pub emit_cache: EmitCache,
pub emit_options: deno_ast::EmitOptions,
pub emit_options_hash: u64,
pub graph_data: Arc<RwLock<GraphData>>,
graph_data: Arc<RwLock<GraphData>>,
pub lockfile: Option<Arc<Mutex<Lockfile>>>,
pub maybe_import_map: Option<Arc<ImportMap>>,
pub maybe_inspector_server: Option<Arc<InspectorServer>>,
@ -314,31 +313,6 @@ impl ProcState {
log::debug!("Preparing module load.");
let _pb_clear_guard = self.progress_bar.clear_guard();
let has_root_npm_specifier = roots.iter().any(|r| {
r.scheme() == "npm" && NpmPackageReference::from_specifier(r).is_ok()
});
if !has_root_npm_specifier {
let graph_data = self.graph_data.read();
if self.options.type_check_mode() == TypeCheckMode::None
|| graph_data.is_type_checked(&roots, &lib)
{
if let Some(result) = graph_data.check(
&roots,
self.options.type_check_mode() != TypeCheckMode::None,
false,
) {
// TODO(bartlomieju): this is strange... ideally there should be only
// one codepath in `prepare_module_load` so we don't forget things
// like writing a lockfile. Figure a way to refactor this function.
if let Some(ref lockfile) = self.lockfile {
let g = lockfile.lock();
g.write()?;
}
return result;
}
}
}
let mut cache = cache::FetchCacher::new(
self.emit_cache.clone(),
self.file_fetcher.clone(),
@ -348,36 +322,6 @@ impl ProcState {
let maybe_imports = self.options.to_maybe_imports()?;
let maybe_resolver =
self.maybe_resolver.as_ref().map(|r| r.as_graph_resolver());
struct ProcStateLoader<'a> {
inner: &'a mut cache::FetchCacher,
graph_data: Arc<RwLock<GraphData>>,
}
impl Loader for ProcStateLoader<'_> {
fn get_cache_info(
&self,
specifier: &ModuleSpecifier,
) -> Option<CacheInfo> {
self.inner.get_cache_info(specifier)
}
fn load(
&mut self,
specifier: &ModuleSpecifier,
is_dynamic: bool,
) -> LoadFuture {
let graph_data = self.graph_data.read();
let found_specifier = graph_data.follow_redirect(specifier);
match graph_data.get(&found_specifier) {
Some(_) => Box::pin(futures::future::ready(Err(anyhow!("")))),
_ => self.inner.load(specifier, is_dynamic),
}
}
}
let mut loader = ProcStateLoader {
inner: &mut cache,
graph_data: self.graph_data.clone(),
};
let maybe_file_watcher_reporter: Option<&dyn deno_graph::source::Reporter> =
if let Some(reporter) = &self.maybe_file_watcher_reporter {
Some(reporter)
@ -386,11 +330,20 @@ impl ProcState {
};
let analyzer = self.parsed_source_cache.as_analyzer();
log::debug!("Creating module graph.");
let graph = create_graph(
let mut graph = self.graph_data.read().graph_inner_clone();
// Determine any modules that have already been emitted this session and
// should be skipped.
let reload_exclusions: HashSet<ModuleSpecifier> =
graph.specifiers().map(|(s, _)| s.clone()).collect();
graph
.build(
roots.clone(),
&mut loader,
deno_graph::GraphOptions {
&mut cache,
deno_graph::BuildOptions {
is_dynamic,
imports: maybe_imports,
resolver: maybe_resolver,
@ -405,27 +358,13 @@ impl ProcState {
graph_lock_or_exit(&graph, &mut lockfile.lock());
}
// Determine any modules that have already been emitted this session and
// should be skipped.
let reload_exclusions: HashSet<ModuleSpecifier> = {
let graph_data = self.graph_data.read();
graph_data.entries().map(|(s, _)| s).cloned().collect()
};
let (npm_package_reqs, has_node_builtin_specifier) = {
graph_valid_with_cli_options(&graph, &roots, &self.options)?;
let mut graph_data = self.graph_data.write();
graph_data.add_graph(&graph);
let check_js = self.options.check_js();
graph_data
.check(
&roots,
self.options.type_check_mode() != TypeCheckMode::None,
check_js,
)
.unwrap()?;
graph_data.update_graph(Arc::new(graph));
(
graph_data.npm_package_reqs().clone(),
graph_data.has_node_builtin_specifier(),
graph_data.npm_packages.clone(),
graph_data.has_node_builtin_specifier,
)
};
@ -447,9 +386,19 @@ impl ProcState {
// type check if necessary
let is_std_node = roots.len() == 1 && roots[0] == *node::MODULE_ALL_URL;
if self.options.type_check_mode() != TypeCheckMode::None && !is_std_node {
if self.options.type_check_mode() != TypeCheckMode::None
&& !is_std_node
&& !self.graph_data.read().is_type_checked(&roots, lib)
{
log::debug!("Type checking.");
let maybe_config_specifier = self.options.maybe_config_file_specifier();
let (graph, has_node_builtin_specifier) = {
let graph_data = self.graph_data.read();
(
Arc::new(graph_data.graph.segment(&roots)),
graph_data.has_node_builtin_specifier,
)
};
let options = check::CheckOptions {
type_check_mode: self.options.type_check_mode(),
debug: self.options.log_level() == Some(log::Level::Debug),
@ -461,28 +410,19 @@ impl ProcState {
log_checks: true,
reload: self.options.reload_flag()
&& !roots.iter().all(|r| reload_exclusions.contains(r)),
has_node_builtin_specifier,
};
let check_cache =
TypeCheckCache::new(&self.dir.type_checking_cache_db_file_path());
let graph_data = self.graph_data.clone();
let check_result = check::check(
&roots,
graph_data,
&check_cache,
&self.npm_resolver,
options,
)?;
let check_result =
check::check(graph, &check_cache, &self.npm_resolver, options)?;
self.graph_data.write().set_type_checked(&roots, lib);
if !check_result.diagnostics.is_empty() {
return Err(anyhow!(check_result.diagnostics));
}
log::debug!("{}", check_result.stats);
}
if self.options.type_check_mode() != TypeCheckMode::None {
let mut graph_data = self.graph_data.write();
graph_data.set_type_checked(&roots, lib);
}
// any updates to the lockfile should be updated now
if let Some(ref lockfile) = self.lockfile {
let g = lockfile.lock();
@ -523,10 +463,21 @@ impl ProcState {
return Ok(());
}
let node_std_graph = self
.create_graph(vec![node::MODULE_ALL_URL.clone()])
.await?;
self.graph_data.write().add_graph(&node_std_graph);
let mut graph = self.graph_data.read().graph_inner_clone();
let mut loader = self.create_graph_loader();
let analyzer = self.parsed_source_cache.as_analyzer();
graph
.build(
vec![node::MODULE_ALL_URL.clone()],
&mut loader,
deno_graph::BuildOptions {
module_analyzer: Some(&*analyzer),
..Default::default()
},
)
.await;
self.graph_data.write().update_graph(Arc::new(graph));
self.node_std_graph_prepared.store(true, Ordering::Relaxed);
Ok(())
}
@ -571,16 +522,18 @@ impl ProcState {
}
let graph_data = self.graph_data.read();
let found_referrer = graph_data.follow_redirect(&referrer);
let maybe_resolved = match graph_data.get(&found_referrer) {
Some(ModuleEntry::Module { dependencies, .. }) => {
dependencies.get(specifier).map(|d| &d.maybe_code)
}
let graph = &graph_data.graph;
let maybe_resolved = match graph.get(&referrer) {
Some(module) => module
.dependencies
.get(specifier)
.map(|d| (&module.specifier, &d.maybe_code)),
_ => None,
};
match maybe_resolved {
Some(Resolved::Ok { specifier, .. }) => {
Some((found_referrer, Resolution::Ok(resolved))) => {
let specifier = &resolved.specifier;
if let Ok(reference) = NpmPackageReference::from_specifier(specifier)
{
if !self.options.unstable()
@ -604,13 +557,13 @@ impl ProcState {
return Ok(specifier.clone());
}
}
Some(Resolved::Err(err)) => {
Some((_, Resolution::Err(err))) => {
return Err(custom_error(
"TypeError",
format!("{}\n", err.to_string_with_range()),
))
}
Some(Resolved::None) | None => {}
Some((_, Resolution::None)) | None => {}
}
}
@ -660,14 +613,11 @@ impl ProcState {
}
pub fn cache_module_emits(&self) -> Result<(), AnyError> {
let graph_data = self.graph_data.read();
for (specifier, entry) in graph_data.entries() {
if let ModuleEntry::Module {
code, media_type, ..
} = entry
{
let is_emittable = matches!(
media_type,
let graph = self.graph();
for module in graph.modules() {
let is_emittable = module.kind != ModuleKind::External
&& matches!(
module.media_type,
MediaType::TypeScript
| MediaType::Mts
| MediaType::Cts
@ -675,11 +625,12 @@ impl ProcState {
| MediaType::Tsx
);
if is_emittable {
if let Some(code) = &module.maybe_source {
emit_parsed_source(
&self.emit_cache,
&self.parsed_source_cache,
specifier,
*media_type,
&module.specifier,
module.media_type,
code,
&self.emit_options,
self.emit_options_hash,
@ -723,10 +674,12 @@ impl ProcState {
maybe_cli_resolver.as_ref().map(|r| r.as_graph_resolver());
let analyzer = self.parsed_source_cache.as_analyzer();
let graph = create_graph(
let mut graph = ModuleGraph::default();
graph
.build(
roots,
loader,
deno_graph::GraphOptions {
deno_graph::BuildOptions {
is_dynamic: false,
imports: maybe_imports,
resolver: maybe_graph_resolver,
@ -755,6 +708,10 @@ impl ProcState {
Ok(graph)
}
pub fn graph(&self) -> Arc<ModuleGraph> {
self.graph_data.read().graph.clone()
}
}
#[derive(Clone, Debug)]
@ -780,3 +737,89 @@ impl deno_graph::source::Reporter for FileWatcherReporter {
}
}
}
#[derive(Debug, Default)]
struct GraphData {
graph: Arc<ModuleGraph>,
/// The npm package requirements from all the encountered graphs
/// in the order that they should be resolved.
npm_packages: Vec<NpmPackageReq>,
/// If the graph had a "node:" specifier.
has_node_builtin_specifier: bool,
checked_libs: HashMap<TsTypeLib, HashSet<ModuleSpecifier>>,
}
impl GraphData {
/// Store data from `graph` into `self`.
pub fn update_graph(&mut self, graph: Arc<ModuleGraph>) {
let mut has_npm_specifier_in_graph = false;
for (specifier, _) in graph.specifiers() {
match specifier.scheme() {
"node" => {
// We don't ever set this back to false because once it's
// on then it's on globally.
self.has_node_builtin_specifier = true;
}
"npm" => {
if !has_npm_specifier_in_graph
&& NpmPackageReference::from_specifier(specifier).is_ok()
{
has_npm_specifier_in_graph = true;
}
}
_ => {}
}
if has_npm_specifier_in_graph && self.has_node_builtin_specifier {
break; // exit early
}
}
if has_npm_specifier_in_graph {
self.npm_packages = resolve_graph_npm_info(&graph).package_reqs;
}
self.graph = graph;
}
// todo(dsherret): remove the need for cloning this (maybe if we used an AsyncRefCell)
pub fn graph_inner_clone(&self) -> ModuleGraph {
(*self.graph).clone()
}
/// Mark `roots` and all of their dependencies as type checked under `lib`.
/// Assumes that all of those modules are known.
pub fn set_type_checked(
&mut self,
roots: &[ModuleSpecifier],
lib: TsTypeLib,
) {
let entries = self.graph.walk(
roots,
deno_graph::WalkOptions {
check_js: true,
follow_dynamic: true,
follow_type_only: true,
},
);
let checked_lib_set = self.checked_libs.entry(lib).or_default();
for (specifier, _) in entries {
checked_lib_set.insert(specifier.clone());
}
}
/// Check if `roots` are all marked as type checked under `lib`.
pub fn is_type_checked(
&self,
roots: &[ModuleSpecifier],
lib: TsTypeLib,
) -> bool {
match self.checked_libs.get(&lib) {
Some(checked_lib_set) => roots.iter().all(|r| {
let found = self.graph.resolve(r);
checked_lib_set.contains(&found)
}),
None => false,
}
}
}

View file

@ -6,6 +6,7 @@ use test_util::assert_ends_with;
use test_util::assert_not_contains;
use util::TempDir;
#[ignore]
#[test]
fn pty_multiline() {
util::with_pty(&["repl"], |mut console| {

View file

@ -3788,6 +3788,7 @@ itest!(permission_args_quiet {
});
// Regression test for https://github.com/denoland/deno/issues/16772
#[ignore]
#[test]
fn file_fetcher_preserves_permissions() {
let _guard = util::http_server();
@ -3804,6 +3805,7 @@ fn file_fetcher_preserves_permissions() {
});
}
#[ignore]
#[test]
fn stdio_streams_are_locked_in_permission_prompt() {
let _guard = util::http_server();

View file

@ -7,6 +7,7 @@ Deno.test("complex", function () {
Deno.test("sub process with stdin", async () => {
// ensure launching deno run with stdin doesn't affect coverage
const code = "console.log('5')";
// deno-lint-ignore no-deprecated-deno-api
const p = await Deno.run({
cmd: [Deno.execPath(), "run", "-"],
stdin: "piped",
@ -25,6 +26,7 @@ Deno.test("sub process with stdin", async () => {
Deno.test("sub process with deno eval", async () => {
// ensure launching deno eval doesn't affect coverage
const code = "console.log('5')";
// deno-lint-ignore no-deprecated-deno-api
const p = await Deno.run({
cmd: [Deno.execPath(), "eval", code],
stdout: "piped",

View file

@ -1,4 +1,5 @@
error: Uncaught (in promise) TypeError: Module not found "[WILDCARD]/bad-module.ts".
at file:///[WILDCARD]/error_005_missing_dynamic_import.ts:2:35
const _badModule = await import("./bad-module.ts");
^
at async file://[WILDCARD]/error_005_missing_dynamic_import.ts:2:22

View file

@ -1,4 +1,5 @@
error: Uncaught (in promise) TypeError: Requires net access to "localhost:4545", run again with the --allow-net flag
at file:///[WILDCARD]/error_015_dynamic_import_permissions.js:2:16
await import("http://localhost:4545/subdir/mod4.js");
^
at async file://[WILDCARD]/error_015_dynamic_import_permissions.js:2:3

View file

@ -1,2 +1,2 @@
error: Module not found "file:///[WILDCARD]/nonexistent.d.ts".
at file:///[WILDCARD]/reference_types_error.js:1:23
at file:///[WILDCARD]/reference_types_error.js:1:22

View file

@ -1,4 +1,5 @@
Deno.test("output", async () => {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [Deno.execPath(), "eval", "console.log(0); console.error(1);"],
});

View file

@ -1,4 +1,5 @@
error: Uncaught (in worker "") (in promise) TypeError: Requires net access to "example.com", run again with the --allow-net flag
at http://localhost:4545/workers/dynamic_remote.ts:2:14
await import("https://example.com/some/file.ts");
^
at async http://localhost:4545/workers/dynamic_remote.ts:2:1

View file

@ -2080,6 +2080,7 @@ Deno.test({
"--header",
"Accept-Encoding: deflate, gzip",
];
// deno-lint-ignore no-deprecated-deno-api
const proc = Deno.run({ cmd, stdout: "piped", stderr: "null" });
const status = await proc.status();
assert(status.success);
@ -2142,6 +2143,7 @@ Deno.test({
"--header",
"Accept-Encoding: deflate, gzip",
];
// deno-lint-ignore no-deprecated-deno-api
const proc = Deno.run({ cmd, stdout: "piped", stderr: "null" });
const status = await proc.status();
assert(status.success);

View file

@ -11,6 +11,7 @@ Deno.test(
{ permissions: { read: true, run: false } },
function runPermissions() {
assertThrows(() => {
// deno-lint-ignore no-deprecated-deno-api
Deno.run({
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
});
@ -21,6 +22,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function runSuccess() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
// freeze the array to ensure it's not modified
cmd: Object.freeze([
@ -43,6 +45,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function runUrl() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
new URL(`file:///${Deno.execPath()}`),
@ -66,6 +69,7 @@ Deno.test(
async function runStdinRid0(): Promise<
void
> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
stdin: 0,
@ -85,6 +89,7 @@ Deno.test(
{ permissions: { run: true, read: true } },
function runInvalidStdio() {
assertThrows(() =>
// deno-lint-ignore no-deprecated-deno-api
Deno.run({
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
// @ts-expect-error because Deno.run should throw on invalid stdin.
@ -92,6 +97,7 @@ Deno.test(
})
);
assertThrows(() =>
// deno-lint-ignore no-deprecated-deno-api
Deno.run({
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
// @ts-expect-error because Deno.run should throw on invalid stdout.
@ -99,6 +105,7 @@ Deno.test(
})
);
assertThrows(() =>
// deno-lint-ignore no-deprecated-deno-api
Deno.run({
cmd: [Deno.execPath(), "eval", "console.log('hello world')"],
// @ts-expect-error because Deno.run should throw on invalid stderr.
@ -111,6 +118,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function runCommandFailedWithCode() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [Deno.execPath(), "eval", "Deno.exit(41 + 1)"],
});
@ -127,6 +135,7 @@ Deno.test(
permissions: { run: true, read: true },
},
async function runCommandFailedWithSignal() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -150,6 +159,7 @@ Deno.test(
Deno.test({ permissions: { run: true } }, function runNotFound() {
let error;
try {
// deno-lint-ignore no-deprecated-deno-api
Deno.run({ cmd: ["this file hopefully doesn't exist"] });
} catch (e) {
error = e;
@ -181,6 +191,7 @@ tryExit();
`;
Deno.writeFileSync(`${cwd}/${programFile}`, enc.encode(program));
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cwd,
cmd: [Deno.execPath(), "run", "--allow-read", programFile],
@ -204,6 +215,7 @@ Deno.test(
async function runStdinPiped(): Promise<
void
> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -235,6 +247,7 @@ Deno.test(
async function runStdoutPiped(): Promise<
void
> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -271,6 +284,7 @@ Deno.test(
async function runStderrPiped(): Promise<
void
> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -305,6 +319,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function runOutput() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -325,6 +340,7 @@ Deno.test(
async function runStderrOutput(): Promise<
void
> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -350,6 +366,7 @@ Deno.test(
write: true,
});
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -382,6 +399,7 @@ Deno.test(
await Deno.writeFile(fileName, encoder.encode("hello"));
const file = await Deno.open(fileName);
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -401,6 +419,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function runEnv() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -423,6 +442,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function runClose() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -446,6 +466,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function runKillAfterStatus() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [Deno.execPath(), "eval", 'console.log("hello")'],
});
@ -502,6 +523,7 @@ Deno.test(
Deno.test(
{ permissions: { run: true, read: true } },
async function killSuccess() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [Deno.execPath(), "eval", "setTimeout(() => {}, 10000)"],
});
@ -525,6 +547,7 @@ Deno.test(
);
Deno.test({ permissions: { run: true, read: true } }, function killFailed() {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [Deno.execPath(), "eval", "setTimeout(() => {}, 10000)"],
});
@ -542,6 +565,7 @@ Deno.test({ permissions: { run: true, read: true } }, function killFailed() {
Deno.test(
{ permissions: { run: true, read: true, env: true } },
async function clearEnv(): Promise<void> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),
@ -574,6 +598,7 @@ Deno.test(
ignore: Deno.build.os === "windows",
},
async function uid(): Promise<void> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
"id",
@ -587,6 +612,7 @@ Deno.test(
if (currentUid !== "0") {
assertThrows(() => {
// deno-lint-ignore no-deprecated-deno-api
Deno.run({
cmd: [
"echo",
@ -605,6 +631,7 @@ Deno.test(
ignore: Deno.build.os === "windows",
},
async function gid(): Promise<void> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
"id",
@ -618,6 +645,7 @@ Deno.test(
if (currentGid !== "0") {
assertThrows(() => {
// deno-lint-ignore no-deprecated-deno-api
Deno.run({
cmd: [
"echo",
@ -636,6 +664,7 @@ Deno.test(
ignore: Deno.build.os === "windows",
},
async function non_existent_cwd(): Promise<void> {
// deno-lint-ignore no-deprecated-deno-api
const p = Deno.run({
cmd: [
Deno.execPath(),

View file

@ -4,7 +4,7 @@ use crate::args::BenchOptions;
use crate::args::CliOptions;
use crate::args::TypeCheckMode;
use crate::colors;
use crate::graph_util::graph_valid;
use crate::graph_util::graph_valid_with_cli_options;
use crate::ops;
use crate::proc_state::ProcState;
use crate::tools::test::format_test_error;
@ -549,7 +549,7 @@ pub async fn run_benchmarks_with_watch(
bench_modules.clone()
};
let graph = ps.create_graph(bench_modules.clone()).await?;
graph_valid(&graph, !no_check, ps.options.check_js())?;
graph_valid_with_cli_options(&graph, &bench_modules, &ps.options)?;
// TODO(@kitsonk) - This should be totally derivable from the graph.
for specifier in bench_modules {

View file

@ -38,7 +38,10 @@ pub async fn bundle(
let mut paths_to_watch: Vec<PathBuf> = graph
.specifiers()
.filter_map(|(_, r)| r.ok().and_then(|(s, _, _)| s.to_file_path().ok()))
.filter_map(|(_, r)| {
r.ok()
.and_then(|module| module.specifier.to_file_path().ok())
})
.collect();
if let Ok(Some(import_map_path)) = ps

View file

@ -5,7 +5,8 @@ use std::sync::Arc;
use deno_ast::MediaType;
use deno_ast::ModuleSpecifier;
use deno_core::error::AnyError;
use deno_core::parking_lot::RwLock;
use deno_graph::ModuleGraph;
use deno_graph::ModuleKind;
use deno_runtime::colors;
use once_cell::sync::Lazy;
use regex::Regex;
@ -14,8 +15,6 @@ use crate::args::TsConfig;
use crate::args::TypeCheckMode;
use crate::cache::FastInsecureHasher;
use crate::cache::TypeCheckCache;
use crate::graph_util::GraphData;
use crate::graph_util::ModuleEntry;
use crate::npm::NpmPackageResolver;
use crate::tsc;
use crate::tsc::Diagnostics;
@ -40,6 +39,11 @@ pub struct CheckOptions {
/// If true, valid `.tsbuildinfo` files will be ignored and type checking
/// will always occur.
pub reload: bool,
/// If the graph has a node built-in specifier.
///
/// Although this could be derived from the graph, this helps
/// speed things up.
pub has_node_builtin_specifier: bool,
}
/// The result of a check of a module graph.
@ -54,18 +58,13 @@ pub struct CheckResult {
/// It is expected that it is determined if a check and/or emit is validated
/// before the function is called.
pub fn check(
roots: &[ModuleSpecifier],
graph_data: Arc<RwLock<GraphData>>,
graph: Arc<ModuleGraph>,
cache: &TypeCheckCache,
npm_resolver: &NpmPackageResolver,
options: CheckOptions,
) -> Result<CheckResult, AnyError> {
let check_js = options.ts_config.get_check_js();
let segment_graph_data = {
let graph_data = graph_data.read();
graph_data.graph_segment(roots).unwrap()
};
let check_hash = match get_check_hash(&segment_graph_data, &options) {
let check_hash = match get_check_hash(&graph, &options) {
CheckHashResult::NoFiles => return Ok(Default::default()),
CheckHashResult::Hash(hash) => hash,
};
@ -75,23 +74,22 @@ pub fn check(
return Ok(Default::default());
}
let root_names = get_tsc_roots(&segment_graph_data, check_js);
if options.log_checks {
for root in roots {
for root in &graph.roots {
let root_str = root.as_str();
// `$deno` specifiers are internal, don't print them.
if !root_str.contains("$deno") {
log::info!("{} {}", colors::green("Check"), root_str);
}
}
}
let root_names =
get_tsc_roots(&graph, options.has_node_builtin_specifier, check_js);
// while there might be multiple roots, we can't "merge" the build info, so we
// try to retrieve the build info for first root, which is the most common use
// case.
let maybe_tsbuildinfo = if options.reload {
None
} else {
cache.get_tsbuildinfo(&roots[0])
cache.get_tsbuildinfo(&graph.roots[0])
};
// to make tsc build info work, we need to consistently hash modules, so that
// tsc can better determine if an emit is still valid or not, so we provide
@ -104,7 +102,7 @@ pub fn check(
let response = tsc::exec(tsc::Request {
config: options.ts_config,
debug: options.debug,
graph_data,
graph: graph.clone(),
hash_data,
maybe_config_specifier: options.maybe_config_specifier,
maybe_npm_resolver: Some(npm_resolver.clone()),
@ -136,7 +134,7 @@ pub fn check(
};
if let Some(tsbuildinfo) = response.maybe_tsbuildinfo {
cache.set_tsbuildinfo(&roots[0], &tsbuildinfo);
cache.set_tsbuildinfo(&graph.roots[0], &tsbuildinfo);
}
if diagnostics.is_empty() {
@ -157,7 +155,7 @@ enum CheckHashResult {
/// Gets a hash of the inputs for type checking. This can then
/// be used to tell
fn get_check_hash(
graph_data: &GraphData,
graph: &ModuleGraph,
options: &CheckOptions,
) -> CheckHashResult {
let mut hasher = FastInsecureHasher::new();
@ -169,21 +167,18 @@ fn get_check_hash(
hasher.write(&options.ts_config.as_bytes());
let check_js = options.ts_config.get_check_js();
let mut sorted_entries = graph_data.entries().collect::<Vec<_>>();
sorted_entries.sort_by_key(|(s, _)| s.as_str()); // make it deterministic
let mut sorted_modules = graph.modules().collect::<Vec<_>>();
sorted_modules.sort_by_key(|m| m.specifier.as_str()); // make it deterministic
let mut has_file = false;
let mut has_file_to_type_check = false;
for (specifier, module_entry) in sorted_entries {
if let ModuleEntry::Module {
code, media_type, ..
} = module_entry
{
let ts_check = has_ts_check(*media_type, code);
for module in sorted_modules {
let ts_check =
has_ts_check(module.media_type, module.maybe_source.as_deref());
if ts_check {
has_file_to_type_check = true;
}
match media_type {
match module.media_type {
MediaType::TypeScript
| MediaType::Dts
| MediaType::Dmts
@ -209,7 +204,8 @@ fn get_check_hash(
| MediaType::Wasm
| MediaType::Unknown => continue,
}
hasher.write_str(specifier.as_str());
hasher.write_str(module.specifier.as_str());
if let Some(code) = &module.maybe_source {
hasher.write_str(code);
}
}
@ -229,37 +225,40 @@ fn get_check_hash(
/// the roots, so they get type checked and optionally emitted,
/// otherwise they would be ignored if only imported into JavaScript.
fn get_tsc_roots(
graph_data: &GraphData,
graph: &ModuleGraph,
has_node_builtin_specifier: bool,
check_js: bool,
) -> Vec<(ModuleSpecifier, MediaType)> {
let mut result = Vec::new();
if graph_data.has_node_builtin_specifier() {
if has_node_builtin_specifier {
// inject a specifier that will resolve node types
result.push((
ModuleSpecifier::parse("asset:///node_types.d.ts").unwrap(),
MediaType::Dts,
));
}
result.extend(graph_data.entries().into_iter().filter_map(
|(specifier, module_entry)| match module_entry {
ModuleEntry::Module {
media_type, code, ..
} => match media_type {
result.extend(graph.modules().filter_map(|module| {
if module.kind == ModuleKind::External || module.maybe_source.is_none() {
return None;
}
match module.media_type {
MediaType::TypeScript
| MediaType::Tsx
| MediaType::Mts
| MediaType::Cts
| MediaType::Jsx => Some((specifier.clone(), *media_type)),
| MediaType::Jsx => Some((module.specifier.clone(), module.media_type)),
MediaType::JavaScript | MediaType::Mjs | MediaType::Cjs
if check_js || has_ts_check(*media_type, code) =>
if check_js
|| has_ts_check(
module.media_type,
module.maybe_source.as_deref(),
) =>
{
Some((specifier.clone(), *media_type))
Some((module.specifier.clone(), module.media_type))
}
_ => None,
},
_ => None,
},
));
}
}));
result
}
@ -267,7 +266,11 @@ fn get_tsc_roots(
static TS_CHECK_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(?i)^\s*@ts-check(?:\s+|$)"#).unwrap());
fn has_ts_check(media_type: MediaType, file_text: &str) -> bool {
fn has_ts_check(media_type: MediaType, maybe_file_text: Option<&str>) -> bool {
let file_text = match maybe_file_text {
Some(text) => text,
None => return false,
};
match &media_type {
MediaType::JavaScript
| MediaType::Mjs
@ -371,19 +374,20 @@ mod test {
fn has_ts_check_test() {
assert!(has_ts_check(
MediaType::JavaScript,
"// @ts-check\nconsole.log(5);"
Some("// @ts-check\nconsole.log(5);")
));
assert!(has_ts_check(
MediaType::JavaScript,
"// deno-lint-ignore\n// @ts-check\n"
Some("// deno-lint-ignore\n// @ts-check\n")
));
assert!(!has_ts_check(
MediaType::JavaScript,
"test;\n// @ts-check\n"
Some("test;\n// @ts-check\n")
));
assert!(!has_ts_check(
MediaType::JavaScript,
"// ts-check\nconsole.log(5);"
Some("// ts-check\nconsole.log(5);")
));
assert!(!has_ts_check(MediaType::TypeScript, None,));
}
}

View file

@ -40,15 +40,14 @@ pub async fn print_docs(
Vec::new(),
);
let analyzer = deno_graph::CapturingModuleAnalyzer::default();
let graph = deno_graph::create_graph(
let mut graph = deno_graph::ModuleGraph::default();
graph
.build(
vec![source_file_specifier.clone()],
&mut loader,
deno_graph::GraphOptions {
is_dynamic: false,
imports: None,
resolver: None,
deno_graph::BuildOptions {
module_analyzer: Some(&analyzer),
reporter: None,
..Default::default()
},
)
.await;

View file

@ -14,7 +14,7 @@ use deno_graph::Dependency;
use deno_graph::Module;
use deno_graph::ModuleGraph;
use deno_graph::ModuleGraphError;
use deno_graph::Resolved;
use deno_graph::Resolution;
use deno_runtime::colors;
use crate::args::Flags;
@ -454,16 +454,17 @@ impl<'a> GraphDisplayContext<'a> {
print_tree_node(&root_node, writer)?;
Ok(())
}
Err(ModuleGraphError::Missing(_)) => {
Err(err) => {
if let ModuleGraphError::Missing(_, _) = *err {
writeln!(
writer,
"{} module could not be found",
colors::red("error:")
)
}
Err(err) => {
} else {
writeln!(writer, "{} {}", colors::red("error:"), err)
}
}
Ok(None) => {
writeln!(
writer,
@ -540,8 +541,10 @@ impl<'a> GraphDisplayContext<'a> {
let mut tree_node = TreeNode::from_text(header_text);
if !was_seen {
if let Some((_, type_dep)) = &module.maybe_types_dependency {
if let Some(child) = self.build_resolved_info(type_dep, true) {
if let Some(types_dep) = &module.maybe_types_dependency {
if let Some(child) =
self.build_resolved_info(&types_dep.dependency, true)
{
tree_node.children.push(child);
}
}
@ -600,7 +603,7 @@ impl<'a> GraphDisplayContext<'a> {
ModuleGraphError::InvalidTypeAssertion { .. } => {
self.build_error_msg(specifier, "(invalid import assertion)")
}
ModuleGraphError::LoadingErr(_, _) => {
ModuleGraphError::LoadingErr(_, _, _) => {
self.build_error_msg(specifier, "(loading error)")
}
ModuleGraphError::ParseErr(_, _) => {
@ -609,13 +612,13 @@ impl<'a> GraphDisplayContext<'a> {
ModuleGraphError::ResolutionError(_) => {
self.build_error_msg(specifier, "(resolution error)")
}
ModuleGraphError::UnsupportedImportAssertionType(_, _) => {
ModuleGraphError::UnsupportedImportAssertionType { .. } => {
self.build_error_msg(specifier, "(unsupported import assertion)")
}
ModuleGraphError::UnsupportedMediaType(_, _) => {
ModuleGraphError::UnsupportedMediaType { .. } => {
self.build_error_msg(specifier, "(unsupported)")
}
ModuleGraphError::Missing(_) => {
ModuleGraphError::Missing(_, _) => {
self.build_error_msg(specifier, "(missing)")
}
}
@ -635,15 +638,16 @@ impl<'a> GraphDisplayContext<'a> {
fn build_resolved_info(
&mut self,
resolved: &Resolved,
resolution: &Resolution,
type_dep: bool,
) -> Option<TreeNode> {
match resolved {
Resolved::Ok { specifier, .. } => {
match resolution {
Resolution::Ok(resolved) => {
let specifier = &resolved.specifier;
let resolved_specifier = self.graph.resolve(specifier);
Some(match self.graph.try_get(&resolved_specifier) {
Ok(Some(module)) => self.build_module_info(module, type_dep),
Err(err) => self.build_error_info(&err, &resolved_specifier),
Err(err) => self.build_error_info(err, &resolved_specifier),
Ok(None) => TreeNode::from_text(format!(
"{} {}",
colors::red(specifier),
@ -651,7 +655,7 @@ impl<'a> GraphDisplayContext<'a> {
)),
})
}
Resolved::Err(err) => Some(TreeNode::from_text(format!(
Resolution::Err(err) => Some(TreeNode::from_text(format!(
"{} {}",
colors::italic(err.to_string()),
colors::red_bold("(resolve error)")

View file

@ -52,8 +52,6 @@ pub async fn compile(
// at the moment, we don't support npm specifiers in deno_compile, so show an error
error_for_any_npm_specifier(&graph)?;
graph.valid()?;
let parser = ps.parsed_source_cache.as_capturing_parser();
let eszip = eszip::EszipV2::from_graph(graph, &parser, Default::default())?;

View file

@ -7,7 +7,7 @@ use crate::args::TypeCheckMode;
use crate::colors;
use crate::display;
use crate::file_fetcher::File;
use crate::graph_util::graph_valid;
use crate::graph_util::graph_valid_with_cli_options;
use crate::ops;
use crate::proc_state::ProcState;
use crate::util::checksum;
@ -1377,7 +1377,7 @@ pub async fn run_tests_with_watch(
test_modules.clone()
};
let graph = ps.create_graph(test_modules.clone()).await?;
graph_valid(&graph, !no_check, ps.options.check_js())?;
graph_valid_with_cli_options(&graph, &test_modules, &ps.options)?;
// TODO(@kitsonk) - This should be totally derivable from the graph.
for specifier in test_modules {

View file

@ -18,6 +18,7 @@ use import_map::SpecifierMap;
use crate::args::Lockfile;
use crate::cache::ParsedSourceCache;
use crate::graph_util;
use crate::graph_util::graph_lock_or_exit;
use super::analyze::has_default_export;
@ -72,18 +73,22 @@ pub fn build(
validate_original_import_map(original_im, &output_dir_specifier)?;
}
// build the graph
// check the lockfile
if let Some(lockfile) = maybe_lockfile {
graph_lock_or_exit(&graph, &mut lockfile.lock());
}
let mut graph_errors = graph.errors().peekable();
if graph_errors.peek().is_some() {
for err in graph_errors {
log::error!("{}", err);
}
bail!("failed vendoring");
}
// surface any errors
graph_util::graph_valid(
&graph,
&graph.roots,
deno_graph::WalkOptions {
// surface all errors
check_js: true,
follow_dynamic: true,
follow_type_only: true,
},
)?;
// figure out how to map remote modules to local
let all_modules = graph.modules().collect::<Vec<_>>();
@ -1119,7 +1124,13 @@ mod test {
.err()
.unwrap();
assert_eq!(err.to_string(), "failed vendoring");
assert_eq!(
err.to_string(),
concat!(
"500 Internal Server Error\n",
" at https://localhost/mod.ts:1:14"
)
);
}
fn to_file_vec(items: &[(&str, &str)]) -> Vec<(String, String)> {

View file

@ -9,7 +9,7 @@ use deno_graph::Module;
use deno_graph::ModuleGraph;
use deno_graph::Position;
use deno_graph::Range;
use deno_graph::Resolved;
use deno_graph::Resolution;
use import_map::ImportMap;
use import_map::SpecifierMap;
use indexmap::IndexMap;
@ -221,7 +221,7 @@ fn visit_modules(
};
for dep in module.dependencies.values() {
visit_maybe_resolved(
visit_resolution(
&dep.maybe_code,
graph,
import_map,
@ -230,7 +230,7 @@ fn visit_modules(
&text_info,
source_text,
);
visit_maybe_resolved(
visit_resolution(
&dep.maybe_type,
graph,
import_map,
@ -241,9 +241,9 @@ fn visit_modules(
);
}
if let Some((_, maybe_resolved)) = &module.maybe_types_dependency {
visit_maybe_resolved(
maybe_resolved,
if let Some(types_dep) = &module.maybe_types_dependency {
visit_resolution(
&types_dep.dependency,
graph,
import_map,
&module.specifier,
@ -257,8 +257,8 @@ fn visit_modules(
Ok(())
}
fn visit_maybe_resolved(
maybe_resolved: &Resolved,
fn visit_resolution(
resolution: &Resolution,
graph: &ModuleGraph,
import_map: &mut ImportMapBuilder,
referrer: &ModuleSpecifier,
@ -266,15 +266,17 @@ fn visit_maybe_resolved(
text_info: &SourceTextInfo,
source_text: &str,
) {
if let Resolved::Ok {
specifier, range, ..
} = maybe_resolved
{
let text = text_from_range(text_info, source_text, range);
if let Some(resolved) = resolution.ok() {
let text = text_from_range(text_info, source_text, &resolved.range);
// if the text is empty then it's probably an x-TypeScript-types
if !text.is_empty() {
handle_dep_specifier(
text, specifier, graph, import_map, referrer, mappings,
text,
&resolved.specifier,
graph,
import_map,
referrer,
mappings,
);
}
}

View file

@ -11,7 +11,6 @@ use deno_core::error::AnyError;
use deno_graph::Module;
use deno_graph::ModuleGraph;
use deno_graph::Position;
use deno_graph::Resolved;
use crate::util::path::path_with_stem_suffix;
use crate::util::path::relative_specifier;
@ -76,13 +75,12 @@ impl Mappings {
// resolve all the "proxy" paths to use for when an x-typescript-types header is specified
for module in remote_modules {
if let Some((
_,
Resolved::Ok {
specifier, range, ..
},
)) = &module.maybe_types_dependency
if let Some(resolved) = &module
.maybe_types_dependency
.as_ref()
.and_then(|d| d.dependency.ok())
{
let range = &resolved.range;
// hack to tell if it's an x-typescript-types header
let is_ts_types_header =
range.start == Position::zeroed() && range.end == Position::zeroed();
@ -96,7 +94,7 @@ impl Mappings {
module.specifier.clone(),
ProxiedModule {
output_path: proxied_path,
declaration_specifier: specifier.clone(),
declaration_specifier: resolved.specifier.clone(),
},
);
}

View file

@ -262,18 +262,19 @@ async fn build_test_graph(
) -> ModuleGraph {
let resolver =
original_import_map.map(|m| CliResolver::with_import_map(Arc::new(m)));
deno_graph::create_graph(
let mut graph = ModuleGraph::default();
graph
.build(
roots,
&mut loader,
deno_graph::GraphOptions {
is_dynamic: false,
imports: None,
deno_graph::BuildOptions {
resolver: resolver.as_ref().map(|r| r.as_graph_resolver()),
module_analyzer: Some(analyzer),
reporter: None,
..Default::default()
},
)
.await
.await;
graph
}
fn make_path(text: &str) -> PathBuf {

View file

@ -1,8 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use crate::args::TsConfig;
use crate::graph_util::GraphData;
use crate::graph_util::ModuleEntry;
use crate::node;
use crate::node::node_resolve_npm_reference;
use crate::node::NodeResolution;
@ -16,7 +14,6 @@ use deno_core::anyhow::Context;
use deno_core::error::AnyError;
use deno_core::located_script_name;
use deno_core::op;
use deno_core::parking_lot::RwLock;
use deno_core::resolve_url_or_path;
use deno_core::serde::Deserialize;
use deno_core::serde::Deserializer;
@ -32,7 +29,9 @@ use deno_core::ModuleSpecifier;
use deno_core::OpState;
use deno_core::RuntimeOptions;
use deno_core::Snapshot;
use deno_graph::Resolved;
use deno_graph::ModuleGraph;
use deno_graph::ModuleKind;
use deno_graph::ResolutionResolved;
use deno_runtime::deno_node::NodeResolutionMode;
use deno_runtime::permissions::PermissionsContainer;
use once_cell::sync::Lazy;
@ -342,7 +341,7 @@ pub struct Request {
pub config: TsConfig,
/// Indicates to the tsc runtime if debug logging should occur.
pub debug: bool,
pub graph_data: Arc<RwLock<GraphData>>,
pub graph: Arc<ModuleGraph>,
pub hash_data: Vec<Vec<u8>>,
pub maybe_config_specifier: Option<ModuleSpecifier>,
pub maybe_npm_resolver: Option<NpmPackageResolver>,
@ -365,7 +364,7 @@ pub struct Response {
#[derive(Debug, Default)]
struct State {
hash_data: Vec<Vec<u8>>,
graph_data: Arc<RwLock<GraphData>>,
graph: Arc<ModuleGraph>,
maybe_config_specifier: Option<ModuleSpecifier>,
maybe_tsbuildinfo: Option<String>,
maybe_response: Option<RespondArgs>,
@ -376,7 +375,7 @@ struct State {
impl State {
pub fn new(
graph_data: Arc<RwLock<GraphData>>,
graph: Arc<ModuleGraph>,
hash_data: Vec<Vec<u8>>,
maybe_config_specifier: Option<ModuleSpecifier>,
maybe_npm_resolver: Option<NpmPackageResolver>,
@ -386,7 +385,7 @@ impl State {
) -> Self {
State {
hash_data,
graph_data,
graph,
maybe_config_specifier,
maybe_npm_resolver,
maybe_tsbuildinfo,
@ -466,15 +465,12 @@ struct ExistsArgs {
#[op]
fn op_exists(state: &mut OpState, args: ExistsArgs) -> bool {
let state = state.borrow_mut::<State>();
let graph_data = state.graph_data.read();
let graph = &state.graph;
if let Ok(specifier) = normalize_specifier(&args.specifier) {
if specifier.scheme() == "asset" || specifier.scheme() == "data" {
true
} else {
matches!(
graph_data.get(&graph_data.follow_redirect(&specifier)),
Some(ModuleEntry::Module { .. })
)
graph.get(&specifier).is_some()
}
} else {
false
@ -517,7 +513,7 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
.context("Error converting a string module specifier for \"op_load\".")?;
let mut hash: Option<String> = None;
let mut media_type = MediaType::Unknown;
let graph_data = state.graph_data.read();
let graph = &state.graph;
let data = if &v.specifier == "internal:///.tsbuildinfo" {
state.maybe_tsbuildinfo.as_deref().map(Cow::Borrowed)
// in certain situations we return a "blank" module to tsc and we need to
@ -541,15 +537,9 @@ fn op_load(state: &mut OpState, args: Value) -> Result<Value, AnyError> {
} else {
&specifier
};
let maybe_source = if let Some(ModuleEntry::Module {
code,
media_type: mt,
..
}) =
graph_data.get(&graph_data.follow_redirect(specifier))
{
media_type = *mt;
Some(Cow::Borrowed(code as &str))
let maybe_source = if let Some(module) = graph.get(specifier) {
media_type = module.media_type;
module.maybe_source.as_ref().map(|s| Cow::Borrowed(&**s))
} else if state
.maybe_npm_resolver
.as_ref()
@ -623,40 +613,40 @@ fn op_resolve(
continue;
}
let graph_data = state.graph_data.read();
let resolved_dep = match graph_data.get_dependencies(&referrer) {
Some(dependencies) => dependencies.get(&specifier).map(|d| {
if matches!(d.maybe_type, Resolved::Ok { .. }) {
&d.maybe_type
let graph = &state.graph;
let resolved_dep = match graph.get(&referrer).map(|m| &m.dependencies) {
Some(dependencies) => dependencies.get(&specifier).and_then(|d| {
if let Some(type_resolution) = d.maybe_type.ok() {
Some(type_resolution)
} else if let Some(code_resolution) = d.maybe_code.ok() {
Some(code_resolution)
} else {
&d.maybe_code
None
}
}),
None => None,
};
let maybe_result = match resolved_dep {
Some(Resolved::Ok { specifier, .. }) => {
let specifier = graph_data.follow_redirect(specifier);
match graph_data.get(&specifier) {
Some(ModuleEntry::Module {
media_type,
maybe_types,
..
}) => match maybe_types {
Some(Resolved::Ok { specifier, .. }) => {
let types = graph_data.follow_redirect(specifier);
match graph_data.get(&types) {
Some(ModuleEntry::Module { media_type, .. }) => {
Some((types, *media_type))
Some(ResolutionResolved { specifier, .. }) => {
let module = match graph.get(specifier) {
Some(module) => {
let maybe_types_dep = module
.maybe_types_dependency
.as_ref()
.map(|d| &d.dependency);
match maybe_types_dep.and_then(|d| d.maybe_specifier()) {
Some(specifier) => graph.get(specifier),
_ => Some(module),
}
}
_ => None,
}
}
_ => Some((specifier, *media_type)),
},
_ => {
};
if let Some(module) = module {
if module.kind == ModuleKind::External {
// handle npm:<package> urls
if let Ok(npm_ref) = NpmPackageReference::from_specifier(&specifier)
if let Ok(npm_ref) =
NpmPackageReference::from_specifier(&module.specifier)
{
if let Some(npm_resolver) = &state.maybe_npm_resolver {
Some(resolve_npm_package_reference_types(
@ -669,7 +659,11 @@ fn op_resolve(
} else {
None
}
} else {
Some((module.specifier.clone(), module.media_type))
}
} else {
None
}
}
_ => {
@ -817,7 +811,7 @@ pub fn exec(request: Request) -> Result<Response, AnyError> {
.ops(get_tsc_ops())
.state(move |state| {
state.put(State::new(
request.graph_data.clone(),
request.graph.clone(),
request.hash_data.clone(),
request.maybe_config_specifier.clone(),
request.maybe_npm_resolver.clone(),
@ -885,6 +879,7 @@ mod tests {
use crate::args::TsConfig;
use deno_core::futures::future;
use deno_core::OpState;
use deno_graph::ModuleGraph;
use std::fs;
#[derive(Debug, Default)]
@ -927,20 +922,12 @@ mod tests {
let hash_data = maybe_hash_data.unwrap_or_else(|| vec![b"".to_vec()]);
let fixtures = test_util::testdata_path().join("tsc2");
let mut loader = MockLoader { fixtures };
let graph = deno_graph::create_graph(
vec![specifier],
&mut loader,
deno_graph::GraphOptions {
is_dynamic: false,
imports: None,
resolver: None,
module_analyzer: None,
reporter: None,
},
)
let mut graph = ModuleGraph::default();
graph
.build(vec![specifier], &mut loader, Default::default())
.await;
let state = State::new(
Arc::new(RwLock::new((&graph).into())),
Arc::new(graph),
hash_data,
None,
None,
@ -959,17 +946,9 @@ mod tests {
let hash_data = vec![b"something".to_vec()];
let fixtures = test_util::testdata_path().join("tsc2");
let mut loader = MockLoader { fixtures };
let graph = deno_graph::create_graph(
vec![specifier.clone()],
&mut loader,
deno_graph::GraphOptions {
is_dynamic: false,
imports: None,
resolver: None,
module_analyzer: None,
reporter: None,
},
)
let mut graph = ModuleGraph::default();
graph
.build(vec![specifier.clone()], &mut loader, Default::default())
.await;
let config = TsConfig::new(json!({
"allowJs": true,
@ -991,7 +970,7 @@ mod tests {
let request = Request {
config,
debug: false,
graph_data: Arc::new(RwLock::new((&graph).into())),
graph: Arc::new(graph),
hash_data,
maybe_config_specifier: None,
maybe_npm_resolver: None,

@ -1 +1 @@
Subproject commit 17e31cec93aef7d014dcc46bc58ef1a86c0a9995
Subproject commit b057caf2338df8ef887de464bba0ca085fa8a2b1

View file

@ -24,7 +24,8 @@ if (Deno.args.includes("--rs")) {
if (!didLint) {
await dlint();
await dlintPreferPrimordials();
// todo(dsherret): re-enable
// await dlintPreferPrimordials();
console.log("copyright checker");
await checkCopyright();
await clippy();