2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::TsConfig;
|
2023-03-21 18:19:42 -04:00
|
|
|
use crate::args::TypeCheckMode;
|
2023-04-01 10:12:40 -04:00
|
|
|
use crate::cache::FastInsecureHasher;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::cache::ModuleInfoCache;
|
2022-10-21 11:20:18 -04:00
|
|
|
use crate::node;
|
2023-09-28 16:43:45 -04:00
|
|
|
use crate::npm::CliNpmResolver;
|
2024-11-01 12:27:00 -04:00
|
|
|
use crate::resolver::CjsTracker;
|
2022-11-28 17:28:54 -05:00
|
|
|
use crate::util::checksum;
|
2023-03-21 11:46:40 -04:00
|
|
|
use crate::util::path::mapped_specifier_for_tsc;
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2021-09-07 10:39:32 -04:00
|
|
|
use deno_ast::MediaType;
|
2021-11-16 09:02:28 -05:00
|
|
|
use deno_core::anyhow::anyhow;
|
|
|
|
use deno_core::anyhow::Context;
|
2023-04-04 08:46:31 -04:00
|
|
|
use deno_core::ascii_str;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::error::AnyError;
|
2021-06-21 19:45:41 -04:00
|
|
|
use deno_core::located_script_name;
|
2023-09-12 07:14:45 -04:00
|
|
|
use deno_core::op2;
|
2023-03-14 13:18:01 -04:00
|
|
|
use deno_core::resolve_url_or_path;
|
2021-02-25 15:18:35 -05:00
|
|
|
use deno_core::serde::Deserialize;
|
2022-09-02 10:54:40 -04:00
|
|
|
use deno_core::serde::Deserializer;
|
2021-05-11 00:54:10 -04:00
|
|
|
use deno_core::serde::Serialize;
|
2022-09-02 10:54:40 -04:00
|
|
|
use deno_core::serde::Serializer;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::serde_json::json;
|
2023-01-14 09:36:19 -05:00
|
|
|
use deno_core::serde_v8;
|
2024-08-23 20:21:21 -04:00
|
|
|
use deno_core::url::Url;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::JsRuntime;
|
|
|
|
use deno_core::ModuleSpecifier;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::OpState;
|
2020-10-13 19:52:49 -04:00
|
|
|
use deno_core::RuntimeOptions;
|
2023-07-26 17:23:07 -04:00
|
|
|
use deno_graph::GraphKind;
|
2023-02-22 14:15:25 -05:00
|
|
|
use deno_graph::Module;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
|
|
|
use deno_graph::ResolutionResolved;
|
2024-11-14 15:24:25 -05:00
|
|
|
use deno_resolver::npm::ResolvePkgFolderFromDenoReqError;
|
2024-11-01 12:27:00 -04:00
|
|
|
use deno_runtime::deno_fs;
|
2023-04-24 19:44:35 -04:00
|
|
|
use deno_runtime::deno_node::NodeResolver;
|
2023-04-06 18:46:44 -04:00
|
|
|
use deno_semver::npm::NpmPackageReqReference;
|
2024-07-25 19:08:14 -04:00
|
|
|
use node_resolver::errors::NodeJsErrorCode;
|
|
|
|
use node_resolver::errors::NodeJsErrorCoded;
|
2024-11-01 12:27:00 -04:00
|
|
|
use node_resolver::errors::PackageSubpathResolveError;
|
2024-07-25 19:08:14 -04:00
|
|
|
use node_resolver::NodeModuleKind;
|
|
|
|
use node_resolver::NodeResolutionMode;
|
2021-12-18 16:14:42 -05:00
|
|
|
use once_cell::sync::Lazy;
|
2022-10-21 11:20:18 -04:00
|
|
|
use std::borrow::Cow;
|
2020-10-25 16:17:58 -04:00
|
|
|
use std::collections::HashMap;
|
2022-09-02 10:54:40 -04:00
|
|
|
use std::fmt;
|
2023-03-14 13:18:01 -04:00
|
|
|
use std::path::Path;
|
2020-11-09 18:10:41 -05:00
|
|
|
use std::path::PathBuf;
|
2020-12-29 23:17:17 -05:00
|
|
|
use std::sync::Arc;
|
2024-10-25 13:56:40 -04:00
|
|
|
use thiserror::Error;
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2022-11-25 18:29:48 -05:00
|
|
|
mod diagnostics;
|
|
|
|
|
|
|
|
pub use self::diagnostics::Diagnostic;
|
|
|
|
pub use self::diagnostics::DiagnosticCategory;
|
|
|
|
pub use self::diagnostics::Diagnostics;
|
|
|
|
pub use self::diagnostics::Position;
|
|
|
|
|
2023-03-21 15:11:22 -04:00
|
|
|
pub static COMPILER_SNAPSHOT: Lazy<Box<[u8]>> = Lazy::new(
|
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
|
|
|
|| {
|
|
|
|
static COMPRESSED_COMPILER_SNAPSHOT: &[u8] =
|
|
|
|
include_bytes!(concat!(env!("OUT_DIR"), "/COMPILER_SNAPSHOT.bin"));
|
|
|
|
|
|
|
|
// NOTE(bartlomieju): Compressing the TSC snapshot in debug build took
|
|
|
|
// ~45s on M1 MacBook Pro; without compression it took ~1s.
|
2024-04-12 10:33:43 -04:00
|
|
|
// Thus we're not using compressed snapshot, trading off
|
2023-03-21 15:11:22 -04:00
|
|
|
// a lot of build time for some startup time in debug build.
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
return COMPRESSED_COMPILER_SNAPSHOT.to_vec().into_boxed_slice();
|
|
|
|
|
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
zstd::bulk::decompress(
|
|
|
|
&COMPRESSED_COMPILER_SNAPSHOT[4..],
|
|
|
|
u32::from_le_bytes(COMPRESSED_COMPILER_SNAPSHOT[0..4].try_into().unwrap())
|
|
|
|
as usize,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.into_boxed_slice()
|
|
|
|
},
|
|
|
|
);
|
2022-07-04 18:12:41 -04:00
|
|
|
|
2024-01-14 12:29:17 -05:00
|
|
|
pub fn get_types_declaration_file_text() -> String {
|
2023-01-14 09:36:19 -05:00
|
|
|
let mut assets = get_asset_texts_from_new_runtime()
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|a| (a.specifier, a.text))
|
|
|
|
.collect::<HashMap<_, _>>();
|
|
|
|
|
2024-01-14 12:29:17 -05:00
|
|
|
let lib_names = vec![
|
2023-01-14 09:36:19 -05:00
|
|
|
"deno.ns",
|
|
|
|
"deno.console",
|
|
|
|
"deno.url",
|
|
|
|
"deno.web",
|
|
|
|
"deno.fetch",
|
2023-12-08 19:19:16 -05:00
|
|
|
"deno.webgpu",
|
2023-01-14 09:36:19 -05:00
|
|
|
"deno.websocket",
|
|
|
|
"deno.webstorage",
|
2024-01-22 06:08:01 -05:00
|
|
|
"deno.canvas",
|
2023-01-14 09:36:19 -05:00
|
|
|
"deno.crypto",
|
|
|
|
"deno.broadcast_channel",
|
|
|
|
"deno.net",
|
|
|
|
"deno.shared_globals",
|
|
|
|
"deno.cache",
|
|
|
|
"deno.window",
|
2024-01-14 12:29:17 -05:00
|
|
|
"deno.unstable",
|
2022-12-08 11:50:09 -05:00
|
|
|
];
|
|
|
|
|
2023-01-14 09:36:19 -05:00
|
|
|
lib_names
|
|
|
|
.into_iter()
|
|
|
|
.map(|name| {
|
2023-01-27 10:43:16 -05:00
|
|
|
let asset_url = format!("asset:///lib.{name}.d.ts");
|
2023-01-14 09:36:19 -05:00
|
|
|
assets.remove(&asset_url).unwrap()
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_asset_texts_from_new_runtime() -> Result<Vec<AssetText>, AnyError> {
|
2024-01-23 16:56:56 -05:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_cli_tsc,
|
|
|
|
ops = [
|
|
|
|
op_create_hash,
|
|
|
|
op_emit,
|
|
|
|
op_is_node_file,
|
|
|
|
op_load,
|
|
|
|
op_resolve,
|
|
|
|
op_respond,
|
|
|
|
]
|
|
|
|
);
|
2023-03-17 14:22:15 -04:00
|
|
|
|
2023-01-14 09:36:19 -05:00
|
|
|
// the assets are stored within the typescript isolate, so take them out of there
|
|
|
|
let mut runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
startup_snapshot: Some(compiler_snapshot()),
|
2023-06-26 07:54:10 -04:00
|
|
|
extensions: vec![deno_cli_tsc::init_ops()],
|
2023-01-14 09:36:19 -05:00
|
|
|
..Default::default()
|
|
|
|
});
|
2023-04-04 08:46:31 -04:00
|
|
|
let global = runtime
|
|
|
|
.execute_script("get_assets.js", ascii_str!("globalThis.getAssets()"))?;
|
2023-01-14 09:36:19 -05:00
|
|
|
let scope = &mut runtime.handle_scope();
|
|
|
|
let local = deno_core::v8::Local::new(scope, global);
|
|
|
|
Ok(serde_v8::from_v8::<Vec<AssetText>>(scope, local)?)
|
2022-12-08 11:50:09 -05:00
|
|
|
}
|
|
|
|
|
2024-02-27 10:05:57 -05:00
|
|
|
pub fn compiler_snapshot() -> &'static [u8] {
|
|
|
|
&COMPILER_SNAPSHOT
|
2022-07-04 18:12:41 -04:00
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
macro_rules! inc {
|
|
|
|
($e:expr) => {
|
2022-11-25 18:29:48 -05:00
|
|
|
include_str!(concat!("./dts/", $e))
|
2021-02-12 06:49:42 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-18 16:14:42 -05:00
|
|
|
/// Contains static assets that are not preloaded in the compiler snapshot.
|
2023-01-14 09:36:19 -05:00
|
|
|
///
|
|
|
|
/// We lazily load these because putting them in the compiler snapshot will
|
|
|
|
/// increase memory usage when not used (last time checked by about 0.5MB).
|
|
|
|
pub static LAZILY_LOADED_STATIC_ASSETS: Lazy<
|
|
|
|
HashMap<&'static str, &'static str>,
|
|
|
|
> = Lazy::new(|| {
|
|
|
|
([
|
|
|
|
(
|
|
|
|
"lib.dom.asynciterable.d.ts",
|
|
|
|
inc!("lib.dom.asynciterable.d.ts"),
|
|
|
|
),
|
|
|
|
("lib.dom.d.ts", inc!("lib.dom.d.ts")),
|
|
|
|
("lib.dom.extras.d.ts", inc!("lib.dom.extras.d.ts")),
|
|
|
|
("lib.dom.iterable.d.ts", inc!("lib.dom.iterable.d.ts")),
|
|
|
|
("lib.es6.d.ts", inc!("lib.es6.d.ts")),
|
|
|
|
("lib.es2016.full.d.ts", inc!("lib.es2016.full.d.ts")),
|
|
|
|
("lib.es2017.full.d.ts", inc!("lib.es2017.full.d.ts")),
|
|
|
|
("lib.es2018.full.d.ts", inc!("lib.es2018.full.d.ts")),
|
|
|
|
("lib.es2019.full.d.ts", inc!("lib.es2019.full.d.ts")),
|
|
|
|
("lib.es2020.full.d.ts", inc!("lib.es2020.full.d.ts")),
|
|
|
|
("lib.es2021.full.d.ts", inc!("lib.es2021.full.d.ts")),
|
|
|
|
("lib.es2022.full.d.ts", inc!("lib.es2022.full.d.ts")),
|
|
|
|
("lib.esnext.full.d.ts", inc!("lib.esnext.full.d.ts")),
|
|
|
|
("lib.scripthost.d.ts", inc!("lib.scripthost.d.ts")),
|
|
|
|
("lib.webworker.d.ts", inc!("lib.webworker.d.ts")),
|
|
|
|
(
|
|
|
|
"lib.webworker.importscripts.d.ts",
|
|
|
|
inc!("lib.webworker.importscripts.d.ts"),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"lib.webworker.iterable.d.ts",
|
|
|
|
inc!("lib.webworker.iterable.d.ts"),
|
|
|
|
),
|
2023-01-24 09:05:54 -05:00
|
|
|
(
|
|
|
|
// Special file that can be used to inject the @types/node package.
|
|
|
|
// This is used for `node:` specifiers.
|
|
|
|
"node_types.d.ts",
|
|
|
|
"/// <reference types=\"npm:@types/node\" />\n",
|
|
|
|
),
|
2023-01-14 09:36:19 -05:00
|
|
|
])
|
|
|
|
.iter()
|
|
|
|
.cloned()
|
|
|
|
.collect()
|
|
|
|
});
|
2021-02-12 06:49:42 -05:00
|
|
|
|
2022-09-02 10:54:40 -04:00
|
|
|
/// A structure representing stats from a type check operation for a graph.
|
|
|
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
|
|
|
pub struct Stats(pub Vec<(String, u32)>);
|
|
|
|
|
|
|
|
impl<'de> Deserialize<'de> for Stats {
|
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
|
|
|
{
|
|
|
|
let items: Vec<(String, u32)> = Deserialize::deserialize(deserializer)?;
|
|
|
|
Ok(Stats(items))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Serialize for Stats {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
Serialize::serialize(&self.0, serializer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Stats {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
writeln!(f, "Compilation statistics:")?;
|
|
|
|
for (key, value) in self.0.clone() {
|
2023-01-27 10:43:16 -05:00
|
|
|
writeln!(f, " {key}: {value}")?;
|
2022-09-02 10:54:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 09:36:19 -05:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct AssetText {
|
|
|
|
pub specifier: String,
|
|
|
|
pub text: String,
|
|
|
|
}
|
|
|
|
|
2021-02-12 06:49:42 -05:00
|
|
|
/// Retrieve a static asset that are included in the binary.
|
2023-01-14 09:36:19 -05:00
|
|
|
fn get_lazily_loaded_asset(asset: &str) -> Option<&'static str> {
|
|
|
|
LAZILY_LOADED_STATIC_ASSETS.get(asset).map(|s| s.to_owned())
|
2020-11-02 14:41:20 -05:00
|
|
|
}
|
|
|
|
|
2020-10-27 20:52:20 -04:00
|
|
|
fn get_maybe_hash(
|
2022-05-20 16:40:55 -04:00
|
|
|
maybe_source: Option<&str>,
|
2023-04-01 10:12:40 -04:00
|
|
|
hash_data: u64,
|
2020-10-27 20:52:20 -04:00
|
|
|
) -> Option<String> {
|
2023-04-01 10:12:40 -04:00
|
|
|
maybe_source.map(|source| get_hash(source, hash_data))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_hash(source: &str, hash_data: u64) -> String {
|
2024-05-29 14:38:18 -04:00
|
|
|
FastInsecureHasher::new_without_deno_version()
|
2023-07-10 17:45:09 -04:00
|
|
|
.write_str(source)
|
|
|
|
.write_u64(hash_data)
|
|
|
|
.finish()
|
|
|
|
.to_string()
|
2020-10-27 20:52:20 -04:00
|
|
|
}
|
|
|
|
|
2021-10-10 17:26:22 -04:00
|
|
|
/// Hash the URL so it can be sent to `tsc` in a supportable way
|
2022-10-21 11:20:18 -04:00
|
|
|
fn hash_url(specifier: &ModuleSpecifier, media_type: MediaType) -> String {
|
2022-11-28 17:28:54 -05:00
|
|
|
let hash = checksum::gen(&[specifier.path().as_bytes()]);
|
2021-10-10 17:26:22 -04:00
|
|
|
format!(
|
|
|
|
"{}:///{}{}",
|
2021-04-07 09:22:14 -04:00
|
|
|
specifier.scheme(),
|
2021-10-10 17:26:22 -04:00
|
|
|
hash,
|
|
|
|
media_type.as_ts_extension()
|
|
|
|
)
|
2021-04-07 09:22:14 -04:00
|
|
|
}
|
|
|
|
|
2022-01-31 04:32:49 -05:00
|
|
|
/// If the provided URLs derivable tsc media type doesn't match the media type,
|
|
|
|
/// we will add an extension to the output. This is to avoid issues with
|
|
|
|
/// specifiers that don't have extensions, that tsc refuses to emit because they
|
|
|
|
/// think a `.js` version exists, when it doesn't.
|
|
|
|
fn maybe_remap_specifier(
|
|
|
|
specifier: &ModuleSpecifier,
|
2022-10-21 11:20:18 -04:00
|
|
|
media_type: MediaType,
|
2022-01-31 04:32:49 -05:00
|
|
|
) -> Option<String> {
|
|
|
|
let path = if specifier.scheme() == "file" {
|
|
|
|
if let Ok(path) = specifier.to_file_path() {
|
|
|
|
path
|
|
|
|
} else {
|
|
|
|
PathBuf::from(specifier.path())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
PathBuf::from(specifier.path())
|
|
|
|
};
|
|
|
|
if path.extension().is_none() {
|
|
|
|
Some(format!("{}{}", specifier, media_type.as_ts_extension()))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[derive(Debug, Clone, Default, Eq, PartialEq)]
|
|
|
|
pub struct EmittedFile {
|
|
|
|
pub data: String,
|
|
|
|
pub maybe_specifiers: Option<Vec<ModuleSpecifier>>,
|
|
|
|
pub media_type: MediaType,
|
|
|
|
}
|
|
|
|
|
2024-11-01 12:27:00 -04:00
|
|
|
pub fn into_specifier_and_media_type(
|
|
|
|
specifier: Option<ModuleSpecifier>,
|
|
|
|
) -> (ModuleSpecifier, MediaType) {
|
|
|
|
match specifier {
|
|
|
|
Some(specifier) => {
|
|
|
|
let media_type = MediaType::from_specifier(&specifier);
|
|
|
|
|
|
|
|
(specifier, media_type)
|
|
|
|
}
|
|
|
|
None => (
|
|
|
|
Url::parse("internal:///missing_dependency.d.ts").unwrap(),
|
|
|
|
MediaType::Dts,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TypeCheckingCjsTracker {
|
|
|
|
cjs_tracker: Arc<CjsTracker>,
|
|
|
|
module_info_cache: Arc<ModuleInfoCache>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TypeCheckingCjsTracker {
|
|
|
|
pub fn new(
|
|
|
|
cjs_tracker: Arc<CjsTracker>,
|
|
|
|
module_info_cache: Arc<ModuleInfoCache>,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
cjs_tracker,
|
|
|
|
module_info_cache,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_cjs(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
code: &Arc<str>,
|
|
|
|
) -> bool {
|
2024-11-13 10:10:09 -05:00
|
|
|
let maybe_is_script = self
|
|
|
|
.module_info_cache
|
|
|
|
.as_module_analyzer()
|
|
|
|
.analyze_sync(specifier, media_type, code)
|
|
|
|
.ok()
|
|
|
|
.map(|info| info.is_script);
|
|
|
|
maybe_is_script
|
|
|
|
.and_then(|is_script| {
|
|
|
|
self
|
|
|
|
.cjs_tracker
|
|
|
|
.is_cjs_with_known_is_script(specifier, media_type, is_script)
|
|
|
|
.ok()
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
self
|
|
|
|
.cjs_tracker
|
|
|
|
.is_maybe_cjs(specifier, media_type)
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_cjs_with_known_is_script(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
media_type: MediaType,
|
|
|
|
is_script: bool,
|
|
|
|
) -> Result<bool, node_resolver::errors::ClosestPkgJsonError> {
|
|
|
|
self
|
|
|
|
.cjs_tracker
|
|
|
|
.is_cjs_with_known_is_script(specifier, media_type, is_script)
|
2024-11-01 12:27:00 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-28 16:43:45 -04:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RequestNpmState {
|
2024-11-01 12:27:00 -04:00
|
|
|
pub cjs_tracker: Arc<TypeCheckingCjsTracker>,
|
2023-09-28 16:43:45 -04:00
|
|
|
pub node_resolver: Arc<NodeResolver>,
|
2023-09-29 09:26:25 -04:00
|
|
|
pub npm_resolver: Arc<dyn CliNpmResolver>,
|
2023-09-28 16:43:45 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
/// A structure representing a request to be sent to the tsc runtime.
|
2020-10-25 16:17:58 -04:00
|
|
|
#[derive(Debug)]
|
2020-10-13 19:52:49 -04:00
|
|
|
pub struct Request {
|
|
|
|
/// The TypeScript compiler options which will be serialized and sent to
|
|
|
|
/// tsc.
|
|
|
|
pub config: TsConfig,
|
|
|
|
/// Indicates to the tsc runtime if debug logging should occur.
|
|
|
|
pub debug: bool,
|
2023-02-09 22:00:23 -05:00
|
|
|
pub graph: Arc<ModuleGraph>,
|
2023-04-01 10:12:40 -04:00
|
|
|
pub hash_data: u64,
|
2023-09-28 16:43:45 -04:00
|
|
|
pub maybe_npm: Option<RequestNpmState>,
|
2020-10-13 19:52:49 -04:00
|
|
|
pub maybe_tsbuildinfo: Option<String>,
|
|
|
|
/// A vector of strings that represent the root/entry point modules for the
|
|
|
|
/// program.
|
2020-10-25 16:17:58 -04:00
|
|
|
pub root_names: Vec<(ModuleSpecifier, MediaType)>,
|
2023-03-21 18:19:42 -04:00
|
|
|
pub check_mode: TypeCheckMode,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Eq, PartialEq)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct Response {
|
2020-10-13 19:52:49 -04:00
|
|
|
/// Any diagnostics that have been returned from the checker.
|
|
|
|
pub diagnostics: Diagnostics,
|
|
|
|
/// If there was any build info associated with the exec request.
|
|
|
|
pub maybe_tsbuildinfo: Option<String>,
|
|
|
|
/// Statistics from the check.
|
2022-09-02 10:54:40 -04:00
|
|
|
pub stats: Stats,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2023-12-11 10:59:09 -05:00
|
|
|
// TODO(bartlomieju): we have similar struct in `tsc.rs` - maybe at least change
|
|
|
|
// the name of the struct to avoid confusion?
|
2023-07-26 17:23:07 -04:00
|
|
|
#[derive(Debug)]
|
2020-10-13 19:52:49 -04:00
|
|
|
struct State {
|
2023-04-01 10:12:40 -04:00
|
|
|
hash_data: u64,
|
2023-02-09 22:00:23 -05:00
|
|
|
graph: Arc<ModuleGraph>,
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_tsbuildinfo: Option<String>,
|
|
|
|
maybe_response: Option<RespondArgs>,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: Option<RequestNpmState>,
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers: HashMap<String, ModuleSpecifier>,
|
2020-10-25 16:17:58 -04:00
|
|
|
root_map: HashMap<String, ModuleSpecifier>,
|
2023-03-14 13:18:01 -04:00
|
|
|
current_dir: PathBuf,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2023-07-26 17:23:07 -04:00
|
|
|
impl Default for State {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
hash_data: Default::default(),
|
|
|
|
graph: Arc::new(ModuleGraph::new(GraphKind::All)),
|
|
|
|
maybe_tsbuildinfo: Default::default(),
|
|
|
|
maybe_response: Default::default(),
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: Default::default(),
|
2023-07-26 17:23:07 -04:00
|
|
|
remapped_specifiers: Default::default(),
|
|
|
|
root_map: Default::default(),
|
|
|
|
current_dir: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
impl State {
|
|
|
|
pub fn new(
|
2023-02-09 22:00:23 -05:00
|
|
|
graph: Arc<ModuleGraph>,
|
2023-04-01 10:12:40 -04:00
|
|
|
hash_data: u64,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: Option<RequestNpmState>,
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_tsbuildinfo: Option<String>,
|
2020-10-25 16:17:58 -04:00
|
|
|
root_map: HashMap<String, ModuleSpecifier>,
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers: HashMap<String, ModuleSpecifier>,
|
2023-03-14 13:18:01 -04:00
|
|
|
current_dir: PathBuf,
|
2020-10-13 19:52:49 -04:00
|
|
|
) -> Self {
|
|
|
|
State {
|
|
|
|
hash_data,
|
2023-02-09 22:00:23 -05:00
|
|
|
graph,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm,
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_tsbuildinfo,
|
|
|
|
maybe_response: None,
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers,
|
2020-10-25 16:17:58 -04:00
|
|
|
root_map,
|
2023-03-14 13:18:01 -04:00
|
|
|
current_dir,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 13:18:01 -04:00
|
|
|
fn normalize_specifier(
|
|
|
|
specifier: &str,
|
|
|
|
current_dir: &Path,
|
|
|
|
) -> Result<ModuleSpecifier, AnyError> {
|
|
|
|
resolve_url_or_path(specifier, current_dir).map_err(|err| err.into())
|
2021-06-21 17:18:32 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
|
|
|
#[string]
|
|
|
|
fn op_create_hash(s: &mut OpState, #[string] text: &str) -> String {
|
2024-04-12 15:45:38 -04:00
|
|
|
op_create_hash_inner(s, text)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn op_create_hash_inner(s: &mut OpState, text: &str) -> String {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = s.borrow_mut::<State>();
|
2023-04-01 10:12:40 -04:00
|
|
|
get_hash(text, state.hash_data)
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct EmitArgs {
|
|
|
|
/// The text data/contents of the file.
|
|
|
|
data: String,
|
|
|
|
/// The _internal_ filename for the file. This will be used to determine how
|
|
|
|
/// the file is cached and stored.
|
|
|
|
file_name: String,
|
|
|
|
}
|
|
|
|
|
2024-04-19 13:11:17 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_emit(
|
|
|
|
state: &mut OpState,
|
|
|
|
#[string] data: String,
|
|
|
|
#[string] file_name: String,
|
|
|
|
) -> bool {
|
|
|
|
op_emit_inner(state, EmitArgs { data, file_name })
|
2024-04-12 15:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn op_emit_inner(state: &mut OpState, args: EmitArgs) -> bool {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2022-02-02 14:48:54 -05:00
|
|
|
match args.file_name.as_ref() {
|
2023-02-05 11:49:20 -05:00
|
|
|
"internal:///.tsbuildinfo" => state.maybe_tsbuildinfo = Some(args.data),
|
2022-02-02 14:48:54 -05:00
|
|
|
_ => {
|
2022-07-12 18:58:39 -04:00
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
panic!("Unhandled emit write: {}", args.file_name);
|
|
|
|
}
|
2022-02-02 14:48:54 -05:00
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 04:36:31 -04:00
|
|
|
true
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-10-21 11:20:18 -04:00
|
|
|
pub fn as_ts_script_kind(media_type: MediaType) -> i32 {
|
2021-12-15 13:22:36 -05:00
|
|
|
match media_type {
|
|
|
|
MediaType::JavaScript => 1,
|
|
|
|
MediaType::Jsx => 2,
|
|
|
|
MediaType::Mjs => 1,
|
|
|
|
MediaType::Cjs => 1,
|
|
|
|
MediaType::TypeScript => 3,
|
|
|
|
MediaType::Mts => 3,
|
|
|
|
MediaType::Cts => 3,
|
|
|
|
MediaType::Dts => 3,
|
|
|
|
MediaType::Dmts => 3,
|
|
|
|
MediaType::Dcts => 3,
|
|
|
|
MediaType::Tsx => 4,
|
|
|
|
MediaType::Json => 6,
|
2022-10-21 11:20:18 -04:00
|
|
|
MediaType::SourceMap
|
2024-11-01 12:27:00 -04:00
|
|
|
| MediaType::Css
|
2022-10-21 11:20:18 -04:00
|
|
|
| MediaType::Wasm
|
|
|
|
| MediaType::Unknown => 0,
|
2021-12-15 13:22:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 16:55:27 -04:00
|
|
|
pub const MISSING_DEPENDENCY_SPECIFIER: &str =
|
|
|
|
"internal:///missing_dependency.d.ts";
|
|
|
|
|
2023-10-08 15:00:41 -04:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct LoadResponse {
|
2023-12-12 05:26:27 -05:00
|
|
|
data: String,
|
2023-10-08 15:00:41 -04:00
|
|
|
version: Option<String>,
|
|
|
|
script_kind: i32,
|
2024-09-13 09:36:26 -04:00
|
|
|
is_cjs: bool,
|
2023-10-08 15:00:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[op2]
|
|
|
|
#[serde]
|
|
|
|
fn op_load(
|
|
|
|
state: &mut OpState,
|
2023-12-02 21:07:04 -05:00
|
|
|
#[string] load_specifier: &str,
|
2024-04-12 15:45:38 -04:00
|
|
|
) -> Result<Option<LoadResponse>, AnyError> {
|
|
|
|
op_load_inner(state, load_specifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_load_inner(
|
|
|
|
state: &mut OpState,
|
|
|
|
load_specifier: &str,
|
2023-12-12 05:26:27 -05:00
|
|
|
) -> Result<Option<LoadResponse>, AnyError> {
|
2024-09-13 09:36:26 -04:00
|
|
|
fn load_from_node_modules(
|
|
|
|
specifier: &ModuleSpecifier,
|
2024-11-01 12:27:00 -04:00
|
|
|
npm_state: Option<&RequestNpmState>,
|
2024-09-13 09:36:26 -04:00
|
|
|
media_type: &mut MediaType,
|
|
|
|
is_cjs: &mut bool,
|
|
|
|
) -> Result<String, AnyError> {
|
|
|
|
*media_type = MediaType::from_specifier(specifier);
|
|
|
|
let file_path = specifier.to_file_path().unwrap();
|
|
|
|
let code = std::fs::read_to_string(&file_path)
|
|
|
|
.with_context(|| format!("Unable to load {}", file_path.display()))?;
|
2024-11-01 12:27:00 -04:00
|
|
|
let code: Arc<str> = code.into();
|
|
|
|
*is_cjs = npm_state
|
|
|
|
.map(|npm_state| {
|
|
|
|
npm_state.cjs_tracker.is_cjs(specifier, *media_type, &code)
|
|
|
|
})
|
|
|
|
.unwrap_or(false);
|
|
|
|
// todo(dsherret): how to avoid cloning here?
|
|
|
|
Ok(code.to_string())
|
2024-09-13 09:36:26 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2023-10-08 15:00:41 -04:00
|
|
|
|
2023-12-02 21:07:04 -05:00
|
|
|
let specifier = normalize_specifier(load_specifier, &state.current_dir)
|
2020-10-13 19:52:49 -04:00
|
|
|
.context("Error converting a string module specifier for \"op_load\".")?;
|
2023-10-08 15:00:41 -04:00
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
let mut hash: Option<String> = None;
|
2020-10-22 20:50:15 -04:00
|
|
|
let mut media_type = MediaType::Unknown;
|
2023-02-09 22:00:23 -05:00
|
|
|
let graph = &state.graph;
|
2024-09-13 09:36:26 -04:00
|
|
|
let mut is_cjs = false;
|
2023-10-08 15:00:41 -04:00
|
|
|
|
2023-12-02 21:07:04 -05:00
|
|
|
let data = if load_specifier == "internal:///.tsbuildinfo" {
|
2022-10-21 11:20:18 -04:00
|
|
|
state.maybe_tsbuildinfo.as_deref().map(Cow::Borrowed)
|
2020-10-22 20:50:15 -04:00
|
|
|
// in certain situations we return a "blank" module to tsc and we need to
|
|
|
|
// handle the request for that module here.
|
2024-04-11 16:55:27 -04:00
|
|
|
} else if load_specifier == MISSING_DEPENDENCY_SPECIFIER {
|
2023-12-12 05:26:27 -05:00
|
|
|
None
|
2023-12-02 21:07:04 -05:00
|
|
|
} else if let Some(name) = load_specifier.strip_prefix("asset:///") {
|
2023-01-14 09:36:19 -05:00
|
|
|
let maybe_source = get_lazily_loaded_asset(name);
|
2023-04-01 10:12:40 -04:00
|
|
|
hash = get_maybe_hash(maybe_source, state.hash_data);
|
2023-12-02 21:07:04 -05:00
|
|
|
media_type = MediaType::from_str(load_specifier);
|
2022-10-21 11:20:18 -04:00
|
|
|
maybe_source.map(Cow::Borrowed)
|
2020-10-13 19:52:49 -04:00
|
|
|
} else {
|
2022-01-31 04:32:49 -05:00
|
|
|
let specifier = if let Some(remapped_specifier) =
|
2023-12-02 21:07:04 -05:00
|
|
|
state.remapped_specifiers.get(load_specifier)
|
2021-01-05 21:22:38 -05:00
|
|
|
{
|
2023-01-05 12:33:09 -05:00
|
|
|
remapped_specifier
|
2023-12-02 21:07:04 -05:00
|
|
|
} else if let Some(remapped_specifier) = state.root_map.get(load_specifier)
|
|
|
|
{
|
2023-01-05 12:33:09 -05:00
|
|
|
remapped_specifier
|
2021-01-05 21:22:38 -05:00
|
|
|
} else {
|
2023-01-05 12:33:09 -05:00
|
|
|
&specifier
|
2021-01-05 21:22:38 -05:00
|
|
|
};
|
2024-04-16 16:46:31 -04:00
|
|
|
let maybe_module = match graph.try_get(specifier) {
|
|
|
|
Ok(maybe_module) => maybe_module,
|
|
|
|
Err(err) => match err {
|
|
|
|
deno_graph::ModuleError::UnsupportedMediaType(_, media_type, _) => {
|
|
|
|
return Ok(Some(LoadResponse {
|
|
|
|
data: "".to_string(),
|
|
|
|
version: Some("1".to_string()),
|
|
|
|
script_kind: as_ts_script_kind(*media_type),
|
2024-09-13 09:36:26 -04:00
|
|
|
is_cjs: false,
|
2024-04-16 16:46:31 -04:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let maybe_source = if let Some(module) = maybe_module {
|
2023-02-22 14:15:25 -05:00
|
|
|
match module {
|
2024-01-31 22:15:22 -05:00
|
|
|
Module::Js(module) => {
|
2023-02-22 14:15:25 -05:00
|
|
|
media_type = module.media_type;
|
2024-11-13 10:10:09 -05:00
|
|
|
if let Some(npm_state) = &state.maybe_npm {
|
|
|
|
is_cjs = npm_state.cjs_tracker.is_cjs_with_known_is_script(
|
|
|
|
specifier,
|
|
|
|
module.media_type,
|
|
|
|
module.is_script,
|
|
|
|
)?;
|
2024-11-01 12:27:00 -04:00
|
|
|
}
|
2024-01-10 17:40:30 -05:00
|
|
|
let source = module
|
|
|
|
.fast_check_module()
|
|
|
|
.map(|m| &*m.source)
|
|
|
|
.unwrap_or(&*module.source);
|
|
|
|
Some(Cow::Borrowed(source))
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
Module::Json(module) => {
|
|
|
|
media_type = MediaType::Json;
|
|
|
|
Some(Cow::Borrowed(&*module.source))
|
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
Module::Npm(_) | Module::Node(_) => None,
|
|
|
|
Module::External(module) => {
|
|
|
|
// means it's Deno code importing an npm module
|
2024-11-01 12:27:00 -04:00
|
|
|
let specifier = node::resolve_specifier_into_node_modules(
|
|
|
|
&module.specifier,
|
|
|
|
&deno_fs::RealFs,
|
|
|
|
);
|
2024-09-13 09:36:26 -04:00
|
|
|
Some(Cow::Owned(load_from_node_modules(
|
|
|
|
&specifier,
|
2024-11-01 12:27:00 -04:00
|
|
|
state.maybe_npm.as_ref(),
|
2024-09-13 09:36:26 -04:00
|
|
|
&mut media_type,
|
|
|
|
&mut is_cjs,
|
|
|
|
)?))
|
2023-02-23 10:58:10 -05:00
|
|
|
}
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
2024-09-13 09:36:26 -04:00
|
|
|
} else if let Some(npm) = state
|
2023-09-28 16:43:45 -04:00
|
|
|
.maybe_npm
|
2022-10-21 11:20:18 -04:00
|
|
|
.as_ref()
|
2024-09-13 09:36:26 -04:00
|
|
|
.filter(|npm| npm.node_resolver.in_npm_package(specifier))
|
2022-10-21 11:20:18 -04:00
|
|
|
{
|
2024-09-13 09:36:26 -04:00
|
|
|
Some(Cow::Owned(load_from_node_modules(
|
|
|
|
specifier,
|
2024-11-01 12:27:00 -04:00
|
|
|
Some(npm),
|
2024-09-13 09:36:26 -04:00
|
|
|
&mut media_type,
|
|
|
|
&mut is_cjs,
|
|
|
|
)?))
|
2020-10-22 20:50:15 -04:00
|
|
|
} else {
|
2021-10-10 17:26:22 -04:00
|
|
|
None
|
2020-10-22 20:50:15 -04:00
|
|
|
};
|
2023-04-01 10:12:40 -04:00
|
|
|
hash = get_maybe_hash(maybe_source.as_deref(), state.hash_data);
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_source
|
|
|
|
};
|
2023-12-12 05:26:27 -05:00
|
|
|
let Some(data) = data else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
Ok(Some(LoadResponse {
|
|
|
|
data: data.into_owned(),
|
2023-10-08 15:00:41 -04:00
|
|
|
version: hash,
|
|
|
|
script_kind: as_ts_script_kind(media_type),
|
2024-09-13 09:36:26 -04:00
|
|
|
is_cjs,
|
2023-12-12 05:26:27 -05:00
|
|
|
}))
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2021-05-11 00:54:10 -04:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2020-10-13 19:52:49 -04:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2020-12-07 05:46:39 -05:00
|
|
|
pub struct ResolveArgs {
|
2020-10-13 19:52:49 -04:00
|
|
|
/// The base specifier that the supplied specifier strings should be resolved
|
|
|
|
/// relative to.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub base: String,
|
2024-07-10 14:46:25 -04:00
|
|
|
/// If the base is cjs.
|
|
|
|
pub is_base_cjs: bool,
|
2020-10-13 19:52:49 -04:00
|
|
|
/// A list of specifiers that should be resolved.
|
2020-12-07 05:46:39 -05:00
|
|
|
pub specifiers: Vec<String>,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2022-03-14 13:44:15 -04:00
|
|
|
fn op_resolve(
|
|
|
|
state: &mut OpState,
|
2024-04-19 13:11:17 -04:00
|
|
|
#[string] base: String,
|
2024-07-10 14:46:25 -04:00
|
|
|
is_base_cjs: bool,
|
2024-04-19 13:11:17 -04:00
|
|
|
#[serde] specifiers: Vec<String>,
|
2024-09-13 09:36:26 -04:00
|
|
|
) -> Result<Vec<(String, &'static str)>, AnyError> {
|
2024-07-10 14:46:25 -04:00
|
|
|
op_resolve_inner(
|
|
|
|
state,
|
|
|
|
ResolveArgs {
|
|
|
|
base,
|
|
|
|
is_base_cjs,
|
|
|
|
specifiers,
|
|
|
|
},
|
|
|
|
)
|
2024-04-12 15:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn op_resolve_inner(
|
|
|
|
state: &mut OpState,
|
|
|
|
args: ResolveArgs,
|
2024-09-13 09:36:26 -04:00
|
|
|
) -> Result<Vec<(String, &'static str)>, AnyError> {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2024-09-13 09:36:26 -04:00
|
|
|
let mut resolved: Vec<(String, &'static str)> =
|
2022-12-05 20:09:31 -05:00
|
|
|
Vec::with_capacity(args.specifiers.len());
|
2024-07-10 14:46:25 -04:00
|
|
|
let referrer_kind = if args.is_base_cjs {
|
|
|
|
NodeModuleKind::Cjs
|
|
|
|
} else {
|
|
|
|
NodeModuleKind::Esm
|
|
|
|
};
|
2022-01-31 04:32:49 -05:00
|
|
|
let referrer = if let Some(remapped_specifier) =
|
|
|
|
state.remapped_specifiers.get(&args.base)
|
|
|
|
{
|
|
|
|
remapped_specifier.clone()
|
|
|
|
} else if let Some(remapped_base) = state.root_map.get(&args.base) {
|
2020-10-25 16:17:58 -04:00
|
|
|
remapped_base.clone()
|
|
|
|
} else {
|
2023-03-14 13:18:01 -04:00
|
|
|
normalize_specifier(&args.base, &state.current_dir).context(
|
2020-10-25 16:17:58 -04:00
|
|
|
"Error converting a string module specifier for \"op_resolve\".",
|
|
|
|
)?
|
|
|
|
};
|
2024-11-13 10:10:09 -05:00
|
|
|
let referrer_module = state.graph.get(&referrer);
|
2023-01-24 09:05:54 -05:00
|
|
|
for specifier in args.specifiers {
|
2024-04-11 16:55:27 -04:00
|
|
|
if specifier.starts_with("node:") {
|
|
|
|
resolved.push((
|
|
|
|
MISSING_DEPENDENCY_SPECIFIER.to_string(),
|
2024-09-13 09:36:26 -04:00
|
|
|
MediaType::Dts.as_ts_extension(),
|
2024-04-11 16:55:27 -04:00
|
|
|
));
|
|
|
|
continue;
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
if specifier.starts_with("asset:///") {
|
2024-09-13 09:36:26 -04:00
|
|
|
let ext = MediaType::from_str(&specifier).as_ts_extension();
|
|
|
|
resolved.push((specifier, ext));
|
2023-01-24 09:05:54 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-11-13 10:10:09 -05:00
|
|
|
let resolved_dep = referrer_module
|
2024-01-31 22:15:22 -05:00
|
|
|
.and_then(|m| m.js())
|
2024-01-10 17:40:30 -05:00
|
|
|
.and_then(|m| m.dependencies_prefer_fast_check().get(&specifier))
|
2023-02-22 14:15:25 -05:00
|
|
|
.and_then(|d| d.maybe_type.ok().or_else(|| d.maybe_code.ok()));
|
2023-02-09 22:00:23 -05:00
|
|
|
|
2023-01-24 09:05:54 -05:00
|
|
|
let maybe_result = match resolved_dep {
|
2023-02-09 22:00:23 -05:00
|
|
|
Some(ResolutionResolved { specifier, .. }) => {
|
2024-11-13 10:10:09 -05:00
|
|
|
resolve_graph_specifier_types(
|
|
|
|
specifier,
|
|
|
|
&referrer,
|
|
|
|
referrer_kind,
|
|
|
|
state,
|
|
|
|
)?
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
2024-10-25 13:56:40 -04:00
|
|
|
_ => {
|
|
|
|
match resolve_non_graph_specifier_types(
|
|
|
|
&specifier,
|
|
|
|
&referrer,
|
|
|
|
referrer_kind,
|
|
|
|
state,
|
|
|
|
) {
|
|
|
|
Ok(maybe_result) => maybe_result,
|
|
|
|
Err(
|
|
|
|
err @ ResolveNonGraphSpecifierTypesError::ResolvePkgFolderFromDenoReq(
|
|
|
|
ResolvePkgFolderFromDenoReqError::Managed(_),
|
|
|
|
),
|
|
|
|
) => {
|
|
|
|
// it's most likely requesting the jsxImportSource, which isn't loaded
|
|
|
|
// into the graph when not using jsx, so just ignore this error
|
|
|
|
if specifier.ends_with("/jsx-runtime") {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
return Err(err.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(err) => return Err(err.into()),
|
|
|
|
}
|
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
};
|
|
|
|
let result = match maybe_result {
|
|
|
|
Some((specifier, media_type)) => {
|
|
|
|
let specifier_str = match specifier.scheme() {
|
|
|
|
"data" | "blob" => {
|
|
|
|
let specifier_str = hash_url(&specifier, media_type);
|
|
|
|
state
|
|
|
|
.remapped_specifiers
|
|
|
|
.insert(specifier_str.clone(), specifier);
|
|
|
|
specifier_str
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if let Some(specifier_str) =
|
|
|
|
maybe_remap_specifier(&specifier, media_type)
|
|
|
|
{
|
2022-01-31 04:32:49 -05:00
|
|
|
state
|
|
|
|
.remapped_specifiers
|
|
|
|
.insert(specifier_str.clone(), specifier);
|
2021-12-16 05:45:41 -05:00
|
|
|
specifier_str
|
2023-01-24 09:05:54 -05:00
|
|
|
} else {
|
|
|
|
specifier.to_string()
|
2021-12-16 05:45:41 -05:00
|
|
|
}
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
|
|
|
};
|
2024-11-01 12:27:00 -04:00
|
|
|
(
|
|
|
|
specifier_str,
|
|
|
|
match media_type {
|
|
|
|
MediaType::Css => ".js", // surface these as .js for typescript
|
|
|
|
media_type => media_type.as_ts_extension(),
|
|
|
|
},
|
|
|
|
)
|
2023-01-24 09:05:54 -05:00
|
|
|
}
|
|
|
|
None => (
|
2024-04-11 16:55:27 -04:00
|
|
|
MISSING_DEPENDENCY_SPECIFIER.to_string(),
|
2024-09-13 09:36:26 -04:00
|
|
|
MediaType::Dts.as_ts_extension(),
|
2023-01-24 09:05:54 -05:00
|
|
|
),
|
|
|
|
};
|
|
|
|
log::debug!("Resolved {} to {:?}", specifier, result);
|
|
|
|
resolved.push(result);
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2022-05-13 04:36:31 -04:00
|
|
|
Ok(resolved)
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
fn resolve_graph_specifier_types(
|
|
|
|
specifier: &ModuleSpecifier,
|
2023-10-05 16:18:29 -04:00
|
|
|
referrer: &ModuleSpecifier,
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind: NodeModuleKind,
|
2023-02-22 14:15:25 -05:00
|
|
|
state: &State,
|
|
|
|
) -> Result<Option<(ModuleSpecifier, MediaType)>, AnyError> {
|
|
|
|
let graph = &state.graph;
|
2024-04-16 16:46:31 -04:00
|
|
|
let maybe_module = match graph.try_get(specifier) {
|
|
|
|
Ok(Some(module)) => Some(module),
|
|
|
|
Ok(None) => None,
|
|
|
|
Err(err) => match err {
|
|
|
|
deno_graph::ModuleError::UnsupportedMediaType(
|
|
|
|
specifier,
|
|
|
|
media_type,
|
|
|
|
_,
|
|
|
|
) => {
|
|
|
|
return Ok(Some((specifier.clone(), *media_type)));
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
},
|
|
|
|
};
|
2023-02-22 14:15:25 -05:00
|
|
|
// follow the types reference directive, which may be pointing at an npm package
|
|
|
|
let maybe_module = match maybe_module {
|
2024-01-31 22:15:22 -05:00
|
|
|
Some(Module::Js(module)) => {
|
2023-02-22 14:15:25 -05:00
|
|
|
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),
|
|
|
|
_ => maybe_module,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
maybe_module => maybe_module,
|
|
|
|
};
|
|
|
|
|
|
|
|
// now get the types from the resolved module
|
|
|
|
match maybe_module {
|
2024-01-31 22:15:22 -05:00
|
|
|
Some(Module::Js(module)) => {
|
2023-02-22 14:15:25 -05:00
|
|
|
Ok(Some((module.specifier.clone(), module.media_type)))
|
|
|
|
}
|
|
|
|
Some(Module::Json(module)) => {
|
|
|
|
Ok(Some((module.specifier.clone(), module.media_type)))
|
|
|
|
}
|
|
|
|
Some(Module::Npm(module)) => {
|
2023-10-03 19:05:06 -04:00
|
|
|
if let Some(npm) = &state.maybe_npm.as_ref() {
|
2023-09-28 16:43:45 -04:00
|
|
|
let package_folder = npm
|
|
|
|
.npm_resolver
|
2023-10-03 19:05:06 -04:00
|
|
|
.as_managed()
|
|
|
|
.unwrap() // should never be byonm because it won't create Module::Npm
|
2023-09-28 16:43:45 -04:00
|
|
|
.resolve_pkg_folder_from_deno_module(module.nv_reference.nv())?;
|
2024-07-23 20:22:24 -04:00
|
|
|
let res_result =
|
2023-10-05 16:18:29 -04:00
|
|
|
npm.node_resolver.resolve_package_subpath_from_deno_module(
|
|
|
|
&package_folder,
|
|
|
|
module.nv_reference.sub_path(),
|
2024-07-09 12:15:03 -04:00
|
|
|
Some(referrer),
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind,
|
2023-10-05 16:18:29 -04:00
|
|
|
NodeResolutionMode::Types,
|
2024-07-23 20:22:24 -04:00
|
|
|
);
|
2024-11-01 12:27:00 -04:00
|
|
|
let maybe_url = match res_result {
|
|
|
|
Ok(url) => Some(url),
|
2024-07-23 20:22:24 -04:00
|
|
|
Err(err) => match err.code() {
|
|
|
|
NodeJsErrorCode::ERR_TYPES_NOT_FOUND
|
|
|
|
| NodeJsErrorCode::ERR_MODULE_NOT_FOUND => None,
|
|
|
|
_ => return Err(err.into()),
|
|
|
|
},
|
|
|
|
};
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(Some(into_specifier_and_media_type(maybe_url)))
|
2023-02-22 14:15:25 -05:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
2023-02-23 10:58:10 -05:00
|
|
|
Some(Module::External(module)) => {
|
|
|
|
// we currently only use "External" for when the module is in an npm package
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(state.maybe_npm.as_ref().map(|_| {
|
|
|
|
let specifier = node::resolve_specifier_into_node_modules(
|
|
|
|
&module.specifier,
|
|
|
|
&deno_fs::RealFs,
|
|
|
|
);
|
|
|
|
into_specifier_and_media_type(Some(specifier))
|
2023-02-23 10:58:10 -05:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
Some(Module::Node(_)) | None => Ok(None),
|
2023-02-22 14:15:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 13:56:40 -04:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
enum ResolveNonGraphSpecifierTypesError {
|
|
|
|
#[error(transparent)]
|
|
|
|
ResolvePkgFolderFromDenoReq(#[from] ResolvePkgFolderFromDenoReqError),
|
|
|
|
#[error(transparent)]
|
2024-11-01 12:27:00 -04:00
|
|
|
PackageSubpathResolve(#[from] PackageSubpathResolveError),
|
2024-10-25 13:56:40 -04:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:15:25 -05:00
|
|
|
fn resolve_non_graph_specifier_types(
|
2024-09-04 10:00:44 -04:00
|
|
|
raw_specifier: &str,
|
2023-02-22 14:15:25 -05:00
|
|
|
referrer: &ModuleSpecifier,
|
2024-07-10 14:46:25 -04:00
|
|
|
referrer_kind: NodeModuleKind,
|
2023-02-22 14:15:25 -05:00
|
|
|
state: &State,
|
2024-10-25 13:56:40 -04:00
|
|
|
) -> Result<
|
|
|
|
Option<(ModuleSpecifier, MediaType)>,
|
|
|
|
ResolveNonGraphSpecifierTypesError,
|
|
|
|
> {
|
2023-09-28 16:43:45 -04:00
|
|
|
let npm = match state.maybe_npm.as_ref() {
|
|
|
|
Some(npm) => npm,
|
2023-02-22 14:15:25 -05:00
|
|
|
None => return Ok(None), // we only support non-graph types for npm packages
|
|
|
|
};
|
2023-09-28 16:43:45 -04:00
|
|
|
let node_resolver = &npm.node_resolver;
|
2023-04-17 15:36:23 -04:00
|
|
|
if node_resolver.in_npm_package(referrer) {
|
2023-02-22 14:15:25 -05:00
|
|
|
// we're in an npm package, so use node resolution
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(Some(into_specifier_and_media_type(
|
2023-04-17 15:36:23 -04:00
|
|
|
node_resolver
|
2024-07-10 14:46:25 -04:00
|
|
|
.resolve(
|
2024-09-04 10:00:44 -04:00
|
|
|
raw_specifier,
|
2024-07-10 14:46:25 -04:00
|
|
|
referrer,
|
|
|
|
referrer_kind,
|
|
|
|
NodeResolutionMode::Types,
|
|
|
|
)
|
2024-11-01 12:27:00 -04:00
|
|
|
.ok()
|
|
|
|
.map(|res| res.into_url()),
|
2023-02-22 14:15:25 -05:00
|
|
|
)))
|
2024-09-04 10:00:44 -04:00
|
|
|
} else if let Ok(npm_req_ref) =
|
|
|
|
NpmPackageReqReference::from_str(raw_specifier)
|
|
|
|
{
|
2024-07-10 14:46:25 -04:00
|
|
|
debug_assert_eq!(referrer_kind, NodeModuleKind::Esm);
|
2023-02-22 14:15:25 -05:00
|
|
|
// todo(dsherret): add support for injecting this in the graph so
|
|
|
|
// we don't need this special code here.
|
|
|
|
// This could occur when resolving npm:@types/node when it is
|
|
|
|
// injected and not part of the graph
|
2023-09-28 16:43:45 -04:00
|
|
|
let package_folder = npm
|
|
|
|
.npm_resolver
|
2023-10-03 19:05:06 -04:00
|
|
|
.resolve_pkg_folder_from_deno_module_req(npm_req_ref.req(), referrer)?;
|
2024-07-23 20:22:24 -04:00
|
|
|
let res_result = node_resolver.resolve_package_subpath_from_deno_module(
|
|
|
|
&package_folder,
|
|
|
|
npm_req_ref.sub_path(),
|
|
|
|
Some(referrer),
|
2024-11-13 10:10:09 -05:00
|
|
|
referrer_kind,
|
2024-07-23 20:22:24 -04:00
|
|
|
NodeResolutionMode::Types,
|
|
|
|
);
|
2024-11-01 12:27:00 -04:00
|
|
|
let maybe_url = match res_result {
|
|
|
|
Ok(url) => Some(url),
|
2024-07-23 20:22:24 -04:00
|
|
|
Err(err) => match err.code() {
|
|
|
|
NodeJsErrorCode::ERR_TYPES_NOT_FOUND
|
|
|
|
| NodeJsErrorCode::ERR_MODULE_NOT_FOUND => None,
|
|
|
|
_ => return Err(err.into()),
|
|
|
|
},
|
|
|
|
};
|
2024-11-01 12:27:00 -04:00
|
|
|
Ok(Some(into_specifier_and_media_type(maybe_url)))
|
2023-02-22 14:15:25 -05:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-12 07:14:45 -04:00
|
|
|
#[op2(fast)]
|
|
|
|
fn op_is_node_file(state: &mut OpState, #[string] path: &str) -> bool {
|
2022-10-21 11:20:18 -04:00
|
|
|
let state = state.borrow::<State>();
|
2024-09-13 09:36:26 -04:00
|
|
|
ModuleSpecifier::parse(path)
|
|
|
|
.ok()
|
|
|
|
.and_then(|specifier| {
|
|
|
|
state
|
|
|
|
.maybe_npm
|
|
|
|
.as_ref()
|
|
|
|
.map(|n| n.node_resolver.in_npm_package(&specifier))
|
|
|
|
})
|
|
|
|
.unwrap_or(false)
|
2022-10-21 11:20:18 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[derive(Debug, Deserialize, Eq, PartialEq)]
|
2020-10-22 20:50:15 -04:00
|
|
|
struct RespondArgs {
|
2020-10-13 19:52:49 -04:00
|
|
|
pub diagnostics: Diagnostics,
|
2022-09-02 10:54:40 -04:00
|
|
|
pub stats: Stats,
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2023-12-11 10:59:09 -05:00
|
|
|
// TODO(bartlomieju): this mechanism is questionable.
|
|
|
|
// Can't we use something more efficient here?
|
2023-09-21 10:08:23 -04:00
|
|
|
#[op2]
|
|
|
|
fn op_respond(state: &mut OpState, #[serde] args: RespondArgs) {
|
2024-04-12 15:45:38 -04:00
|
|
|
op_respond_inner(state, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn op_respond_inner(state: &mut OpState, args: RespondArgs) {
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow_mut::<State>();
|
2023-09-11 20:55:57 -04:00
|
|
|
state.maybe_response = Some(args);
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Execute a request on the supplied snapshot, returning a response which
|
|
|
|
/// contains information, like any emitted files, diagnostics, statistics and
|
|
|
|
/// optionally an updated TypeScript build info.
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn exec(request: Request) -> Result<Response, AnyError> {
|
2020-10-25 16:17:58 -04:00
|
|
|
// tsc cannot handle root specifiers that don't have one of the "acceptable"
|
|
|
|
// extensions. Therefore, we have to check the root modules against their
|
|
|
|
// extensions and remap any that are unacceptable to tsc and add them to the
|
|
|
|
// op state so when requested, we can remap to the original specifier.
|
|
|
|
let mut root_map = HashMap::new();
|
2022-01-31 04:32:49 -05:00
|
|
|
let mut remapped_specifiers = HashMap::new();
|
2020-10-25 16:17:58 -04:00
|
|
|
let root_names: Vec<String> = request
|
|
|
|
.root_names
|
|
|
|
.iter()
|
2021-04-07 09:22:14 -04:00
|
|
|
.map(|(s, mt)| match s.scheme() {
|
|
|
|
"data" | "blob" => {
|
2022-10-21 11:20:18 -04:00
|
|
|
let specifier_str = hash_url(s, *mt);
|
2022-01-31 04:32:49 -05:00
|
|
|
remapped_specifiers.insert(specifier_str.clone(), s.clone());
|
2021-01-05 21:22:38 -05:00
|
|
|
specifier_str
|
2021-04-07 09:22:14 -04:00
|
|
|
}
|
|
|
|
_ => {
|
2023-03-21 11:46:40 -04:00
|
|
|
if let Some(new_specifier) = mapped_specifier_for_tsc(s, *mt) {
|
2021-01-05 21:22:38 -05:00
|
|
|
root_map.insert(new_specifier.clone(), s.clone());
|
|
|
|
new_specifier
|
|
|
|
} else {
|
2023-03-21 11:46:40 -04:00
|
|
|
s.to_string()
|
2021-01-05 21:22:38 -05:00
|
|
|
}
|
2020-10-25 16:17:58 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2023-03-17 14:22:15 -04:00
|
|
|
|
|
|
|
deno_core::extension!(deno_cli_tsc,
|
2024-01-23 16:56:56 -05:00
|
|
|
ops = [
|
|
|
|
op_create_hash,
|
|
|
|
op_emit,
|
|
|
|
op_is_node_file,
|
|
|
|
op_load,
|
|
|
|
op_resolve,
|
|
|
|
op_respond,
|
|
|
|
],
|
2023-03-17 18:15:27 -04:00
|
|
|
options = {
|
|
|
|
request: Request,
|
2023-03-17 14:22:15 -04:00
|
|
|
root_map: HashMap<String, Url>,
|
|
|
|
remapped_specifiers: HashMap<String, Url>,
|
|
|
|
},
|
2023-03-17 18:15:27 -04:00
|
|
|
state = |state, options| {
|
2023-03-17 14:22:15 -04:00
|
|
|
state.put(State::new(
|
2023-03-17 18:15:27 -04:00
|
|
|
options.request.graph,
|
|
|
|
options.request.hash_data,
|
2023-09-28 16:43:45 -04:00
|
|
|
options.request.maybe_npm,
|
2023-03-17 18:15:27 -04:00
|
|
|
options.request.maybe_tsbuildinfo,
|
|
|
|
options.root_map,
|
|
|
|
options.remapped_specifiers,
|
2023-03-17 14:22:15 -04:00
|
|
|
std::env::current_dir()
|
|
|
|
.context("Unable to get CWD")
|
|
|
|
.unwrap(),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
);
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2020-10-25 16:17:58 -04:00
|
|
|
let request_value = json!({
|
|
|
|
"config": request.config,
|
|
|
|
"debug": request.debug,
|
|
|
|
"rootNames": root_names,
|
2023-03-21 18:19:42 -04:00
|
|
|
"localOnly": request.check_mode == TypeCheckMode::Local,
|
2020-10-25 16:17:58 -04:00
|
|
|
});
|
2024-03-04 20:17:39 -05:00
|
|
|
let exec_source = format!("globalThis.exec({request_value})");
|
2020-10-13 19:52:49 -04:00
|
|
|
|
2023-03-17 14:22:15 -04:00
|
|
|
let mut runtime = JsRuntime::new(RuntimeOptions {
|
|
|
|
startup_snapshot: Some(compiler_snapshot()),
|
2023-06-26 07:54:10 -04:00
|
|
|
extensions: vec![deno_cli_tsc::init_ops(
|
2023-03-17 18:15:27 -04:00
|
|
|
request,
|
2023-03-17 14:22:15 -04:00
|
|
|
root_map,
|
|
|
|
remapped_specifiers,
|
|
|
|
)],
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
2023-03-21 18:33:12 -04:00
|
|
|
runtime.execute_script(located_script_name!(), exec_source)?;
|
2020-10-13 19:52:49 -04:00
|
|
|
|
|
|
|
let op_state = runtime.op_state();
|
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
let state = op_state.take::<State>();
|
|
|
|
|
|
|
|
if let Some(response) = state.maybe_response {
|
|
|
|
let diagnostics = response.diagnostics;
|
|
|
|
let maybe_tsbuildinfo = state.maybe_tsbuildinfo;
|
|
|
|
let stats = response.stats;
|
|
|
|
|
|
|
|
Ok(Response {
|
|
|
|
diagnostics,
|
|
|
|
maybe_tsbuildinfo,
|
|
|
|
stats,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Err(anyhow!("The response for the exec request was not set."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-11-25 18:29:48 -05:00
|
|
|
use super::Diagnostic;
|
|
|
|
use super::DiagnosticCategory;
|
2020-10-13 19:52:49 -04:00
|
|
|
use super::*;
|
2022-06-27 16:54:09 -04:00
|
|
|
use crate::args::TsConfig;
|
2021-10-10 17:26:22 -04:00
|
|
|
use deno_core::futures::future;
|
2023-10-08 15:00:41 -04:00
|
|
|
use deno_core::serde_json;
|
2022-03-14 13:44:15 -04:00
|
|
|
use deno_core::OpState;
|
2023-06-06 17:07:46 -04:00
|
|
|
use deno_graph::GraphKind;
|
2023-02-09 22:00:23 -05:00
|
|
|
use deno_graph::ModuleGraph;
|
2023-06-10 11:09:45 -04:00
|
|
|
use test_util::PathRef;
|
2021-10-10 17:26:22 -04:00
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
2022-03-23 09:54:22 -04:00
|
|
|
pub struct MockLoader {
|
2023-06-10 11:09:45 -04:00
|
|
|
pub fixtures: PathRef,
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl deno_graph::source::Loader for MockLoader {
|
|
|
|
fn load(
|
2024-04-18 20:51:16 -04:00
|
|
|
&self,
|
2021-10-10 17:26:22 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-02-15 14:49:35 -05:00
|
|
|
_options: deno_graph::source::LoadOptions,
|
2021-10-10 17:26:22 -04:00
|
|
|
) -> deno_graph::source::LoadFuture {
|
|
|
|
let specifier_text = specifier
|
|
|
|
.to_string()
|
|
|
|
.replace(":///", "_")
|
|
|
|
.replace("://", "_")
|
2022-02-24 20:03:12 -05:00
|
|
|
.replace('/', "-");
|
2021-10-10 17:26:22 -04:00
|
|
|
let source_path = self.fixtures.join(specifier_text);
|
2024-01-31 22:15:22 -05:00
|
|
|
let response = source_path.read_to_bytes_if_exists().map(|c| {
|
2023-06-10 11:09:45 -04:00
|
|
|
Some(deno_graph::source::LoadResponse::Module {
|
|
|
|
specifier: specifier.clone(),
|
|
|
|
maybe_headers: None,
|
|
|
|
content: c.into(),
|
2021-10-10 17:26:22 -04:00
|
|
|
})
|
2023-06-10 11:09:45 -04:00
|
|
|
});
|
2022-01-13 11:58:00 -05:00
|
|
|
Box::pin(future::ready(response))
|
2021-10-10 17:26:22 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
|
|
|
|
async fn setup(
|
|
|
|
maybe_specifier: Option<ModuleSpecifier>,
|
2023-04-01 10:12:40 -04:00
|
|
|
maybe_hash_data: Option<u64>,
|
2020-10-13 19:52:49 -04:00
|
|
|
maybe_tsbuildinfo: Option<String>,
|
2022-03-14 13:44:15 -04:00
|
|
|
) -> OpState {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = maybe_specifier
|
2023-03-13 19:31:03 -04:00
|
|
|
.unwrap_or_else(|| ModuleSpecifier::parse("file:///main.ts").unwrap());
|
2023-04-01 10:12:40 -04:00
|
|
|
let hash_data = maybe_hash_data.unwrap_or(0);
|
2021-08-11 10:20:47 -04:00
|
|
|
let fixtures = test_util::testdata_path().join("tsc2");
|
2024-04-18 20:51:16 -04:00
|
|
|
let loader = MockLoader { fixtures };
|
2023-06-06 17:07:46 -04:00
|
|
|
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
|
2023-02-09 22:00:23 -05:00
|
|
|
graph
|
2024-04-18 20:51:16 -04:00
|
|
|
.build(vec![specifier], &loader, Default::default())
|
2023-02-09 22:00:23 -05:00
|
|
|
.await;
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = State::new(
|
2023-02-09 22:00:23 -05:00
|
|
|
Arc::new(graph),
|
2021-01-05 21:22:38 -05:00
|
|
|
hash_data,
|
2021-06-21 17:18:32 -04:00
|
|
|
None,
|
2021-01-05 21:22:38 -05:00
|
|
|
maybe_tsbuildinfo,
|
|
|
|
HashMap::new(),
|
|
|
|
HashMap::new(),
|
2023-03-14 13:18:01 -04:00
|
|
|
std::env::current_dir()
|
|
|
|
.context("Unable to get CWD")
|
|
|
|
.unwrap(),
|
2022-03-14 13:44:15 -04:00
|
|
|
);
|
2023-11-05 16:27:36 -05:00
|
|
|
let mut op_state = OpState::new(None);
|
2022-03-14 13:44:15 -04:00
|
|
|
op_state.put(state);
|
|
|
|
op_state
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2020-10-27 20:52:20 -04:00
|
|
|
async fn test_exec(
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<Response, AnyError> {
|
2023-04-01 10:12:40 -04:00
|
|
|
let hash_data = 123; // something random
|
2021-08-11 10:20:47 -04:00
|
|
|
let fixtures = test_util::testdata_path().join("tsc2");
|
2024-04-18 20:51:16 -04:00
|
|
|
let loader = MockLoader { fixtures };
|
2023-06-06 17:07:46 -04:00
|
|
|
let mut graph = ModuleGraph::new(GraphKind::TypesOnly);
|
2023-02-09 22:00:23 -05:00
|
|
|
graph
|
2024-04-18 20:51:16 -04:00
|
|
|
.build(vec![specifier.clone()], &loader, Default::default())
|
2023-02-09 22:00:23 -05:00
|
|
|
.await;
|
2020-10-27 20:52:20 -04:00
|
|
|
let config = TsConfig::new(json!({
|
|
|
|
"allowJs": true,
|
|
|
|
"checkJs": false,
|
|
|
|
"esModuleInterop": true,
|
|
|
|
"emitDecoratorMetadata": false,
|
|
|
|
"incremental": true,
|
|
|
|
"jsx": "react",
|
|
|
|
"jsxFactory": "React.createElement",
|
|
|
|
"jsxFragmentFactory": "React.Fragment",
|
|
|
|
"lib": ["deno.window"],
|
|
|
|
"noEmit": true,
|
2023-02-05 11:49:20 -05:00
|
|
|
"outDir": "internal:///",
|
2020-10-27 20:52:20 -04:00
|
|
|
"strict": true,
|
|
|
|
"target": "esnext",
|
2023-02-05 11:49:20 -05:00
|
|
|
"tsBuildInfoFile": "internal:///.tsbuildinfo",
|
2020-10-27 20:52:20 -04:00
|
|
|
}));
|
|
|
|
let request = Request {
|
|
|
|
config,
|
|
|
|
debug: false,
|
2023-02-09 22:00:23 -05:00
|
|
|
graph: Arc::new(graph),
|
2020-10-27 20:52:20 -04:00
|
|
|
hash_data,
|
2023-09-28 16:43:45 -04:00
|
|
|
maybe_npm: None,
|
2020-10-27 20:52:20 -04:00
|
|
|
maybe_tsbuildinfo: None,
|
|
|
|
root_names: vec![(specifier.clone(), MediaType::TypeScript)],
|
2023-03-21 18:19:42 -04:00
|
|
|
check_mode: TypeCheckMode::All,
|
2020-10-27 20:52:20 -04:00
|
|
|
};
|
2021-01-05 20:38:23 -05:00
|
|
|
exec(request)
|
|
|
|
}
|
|
|
|
|
2023-03-22 08:38:56 -04:00
|
|
|
// TODO(bartlomieju): this test is segfaulting in V8, saying that there are too
|
|
|
|
// few external references registered. It seems to be a bug in our snapshotting
|
|
|
|
// logic. Because when we create TSC snapshot we register a few ops that
|
|
|
|
// are called during snapshotting time, V8 expects at least as many references
|
|
|
|
// when it starts up. The thing is that these ops are one-off - ie. they will never
|
|
|
|
// be used again after the snapshot is taken. We should figure out a mechanism
|
|
|
|
// to allow removing some of the ops before taking a snapshot.
|
|
|
|
#[ignore]
|
2024-02-19 08:34:24 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_compiler_snapshot() {
|
2022-07-14 17:52:44 -04:00
|
|
|
let mut js_runtime = JsRuntime::new(RuntimeOptions {
|
2022-07-04 18:12:41 -04:00
|
|
|
startup_snapshot: Some(compiler_snapshot()),
|
2021-01-05 20:38:23 -05:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
js_runtime
|
2024-03-04 20:17:39 -05:00
|
|
|
.execute_script(
|
2021-01-05 20:38:23 -05:00
|
|
|
"<anon>",
|
|
|
|
r#"
|
2023-12-11 10:59:09 -05:00
|
|
|
if (!(globalThis.exec)) {
|
2021-01-05 20:38:23 -05:00
|
|
|
throw Error("bad");
|
|
|
|
}
|
|
|
|
console.log(`ts version: ${ts.version}`);
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_create_hash() {
|
2023-04-01 10:12:40 -04:00
|
|
|
let mut state = setup(None, Some(123), None).await;
|
2024-04-12 15:45:38 -04:00
|
|
|
let actual = op_create_hash_inner(&mut state, "some sort of content");
|
2023-04-01 10:12:40 -04:00
|
|
|
assert_eq!(actual, "11905938177474799758");
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
2024-02-19 08:34:24 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_hash_url() {
|
2021-02-17 13:47:18 -05:00
|
|
|
let specifier = deno_core::resolve_url(
|
2021-01-05 21:22:38 -05:00
|
|
|
"data:application/javascript,console.log(\"Hello%20Deno\");",
|
|
|
|
)
|
|
|
|
.unwrap();
|
2022-10-21 11:20:18 -04:00
|
|
|
assert_eq!(hash_url(&specifier, MediaType::JavaScript), "data:///d300ea0796bd72b08df10348e0b70514c021f2e45bfe59cec24e12e97cd79c58.js");
|
2021-01-05 21:22:38 -05:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_emit_tsbuildinfo() {
|
|
|
|
let mut state = setup(None, None, None).await;
|
2024-04-12 15:45:38 -04:00
|
|
|
let actual = op_emit_inner(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
2022-02-02 14:48:54 -05:00
|
|
|
EmitArgs {
|
|
|
|
data: "some file content".to_string(),
|
2023-02-05 11:49:20 -05:00
|
|
|
file_name: "internal:///.tsbuildinfo".to_string(),
|
2022-02-02 14:48:54 -05:00
|
|
|
},
|
2022-05-13 04:36:31 -04:00
|
|
|
);
|
|
|
|
assert!(actual);
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow::<State>();
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
|
|
|
state.maybe_tsbuildinfo,
|
|
|
|
Some("some file content".to_string())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load() {
|
|
|
|
let mut state = setup(
|
2023-03-13 19:31:03 -04:00
|
|
|
Some(ModuleSpecifier::parse("https://deno.land/x/mod.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
Some("some content".to_string()),
|
|
|
|
)
|
|
|
|
.await;
|
2023-12-02 21:07:04 -05:00
|
|
|
let actual =
|
2024-04-12 15:45:38 -04:00
|
|
|
op_load_inner(&mut state, "https://deno.land/x/mod.ts").unwrap();
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
2023-10-08 15:00:41 -04:00
|
|
|
serde_json::to_value(actual).unwrap(),
|
2020-10-13 19:52:49 -04:00
|
|
|
json!({
|
|
|
|
"data": "console.log(\"hello deno\");\n",
|
2023-04-01 10:12:40 -04:00
|
|
|
"version": "7821807483407828376",
|
2020-10-22 20:50:15 -04:00
|
|
|
"scriptKind": 3,
|
2024-09-13 09:36:26 -04:00
|
|
|
"isCjs": false,
|
2020-10-13 19:52:49 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-10-27 20:52:20 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load_asset() {
|
|
|
|
let mut state = setup(
|
2023-03-13 19:31:03 -04:00
|
|
|
Some(ModuleSpecifier::parse("https://deno.land/x/mod.ts").unwrap()),
|
2020-10-27 20:52:20 -04:00
|
|
|
None,
|
|
|
|
Some("some content".to_string()),
|
|
|
|
)
|
|
|
|
.await;
|
2024-04-12 15:45:38 -04:00
|
|
|
let actual = op_load_inner(&mut state, "asset:///lib.dom.d.ts")
|
2023-12-12 05:26:27 -05:00
|
|
|
.expect("should have invoked op")
|
|
|
|
.expect("load should have succeeded");
|
2023-01-14 09:36:19 -05:00
|
|
|
let expected = get_lazily_loaded_asset("lib.dom.d.ts").unwrap();
|
2023-12-12 05:26:27 -05:00
|
|
|
assert_eq!(actual.data, expected);
|
2022-05-15 14:41:37 -04:00
|
|
|
assert!(actual.version.is_some());
|
2020-10-27 20:52:20 -04:00
|
|
|
assert_eq!(actual.script_kind, 3);
|
|
|
|
}
|
|
|
|
|
2020-10-13 19:52:49 -04:00
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load_tsbuildinfo() {
|
|
|
|
let mut state = setup(
|
2023-03-13 19:31:03 -04:00
|
|
|
Some(ModuleSpecifier::parse("https://deno.land/x/mod.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
Some("some content".to_string()),
|
|
|
|
)
|
|
|
|
.await;
|
2024-04-12 15:45:38 -04:00
|
|
|
let actual = op_load_inner(&mut state, "internal:///.tsbuildinfo")
|
2023-12-12 05:26:27 -05:00
|
|
|
.expect("should have invoked op")
|
|
|
|
.expect("load should have succeeded");
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
2023-10-08 15:00:41 -04:00
|
|
|
serde_json::to_value(actual).unwrap(),
|
2020-10-13 19:52:49 -04:00
|
|
|
json!({
|
|
|
|
"data": "some content",
|
2022-05-15 14:41:37 -04:00
|
|
|
"version": null,
|
2020-10-22 20:50:15 -04:00
|
|
|
"scriptKind": 0,
|
2024-09-13 09:36:26 -04:00
|
|
|
"isCjs": false,
|
2020-10-13 19:52:49 -04:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_load_missing_specifier() {
|
|
|
|
let mut state = setup(None, None, None).await;
|
2024-04-12 15:45:38 -04:00
|
|
|
let actual = op_load_inner(&mut state, "https://deno.land/x/mod.ts")
|
2023-12-02 21:07:04 -05:00
|
|
|
.expect("should have invoked op");
|
2023-12-12 05:26:27 -05:00
|
|
|
assert_eq!(serde_json::to_value(actual).unwrap(), json!(null));
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_resolve() {
|
|
|
|
let mut state = setup(
|
2023-03-13 19:31:03 -04:00
|
|
|
Some(ModuleSpecifier::parse("https://deno.land/x/a.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await;
|
2024-04-12 15:45:38 -04:00
|
|
|
let actual = op_resolve_inner(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
2022-01-31 04:32:49 -05:00
|
|
|
ResolveArgs {
|
|
|
|
base: "https://deno.land/x/a.ts".to_string(),
|
2024-07-10 14:46:25 -04:00
|
|
|
is_base_cjs: false,
|
2022-01-31 04:32:49 -05:00
|
|
|
specifiers: vec!["./b.ts".to_string()],
|
|
|
|
},
|
2020-10-13 19:52:49 -04:00
|
|
|
)
|
|
|
|
.expect("should have invoked op");
|
2024-09-13 09:36:26 -04:00
|
|
|
assert_eq!(actual, vec![("https://deno.land/x/b.ts".into(), ".ts")]);
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-10-22 20:50:15 -04:00
|
|
|
async fn test_resolve_empty() {
|
2020-10-13 19:52:49 -04:00
|
|
|
let mut state = setup(
|
2023-03-13 19:31:03 -04:00
|
|
|
Some(ModuleSpecifier::parse("https://deno.land/x/a.ts").unwrap()),
|
2020-10-13 19:52:49 -04:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
.await;
|
2024-04-12 15:45:38 -04:00
|
|
|
let actual = op_resolve_inner(
|
2020-10-13 19:52:49 -04:00
|
|
|
&mut state,
|
2022-01-31 04:32:49 -05:00
|
|
|
ResolveArgs {
|
|
|
|
base: "https://deno.land/x/a.ts".to_string(),
|
2024-07-10 14:46:25 -04:00
|
|
|
is_base_cjs: false,
|
2022-01-31 04:32:49 -05:00
|
|
|
specifiers: vec!["./bad.ts".to_string()],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.expect("should have not errored");
|
2024-09-13 09:36:26 -04:00
|
|
|
assert_eq!(actual, vec![(MISSING_DEPENDENCY_SPECIFIER.into(), ".d.ts")]);
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_respond() {
|
|
|
|
let mut state = setup(None, None, None).await;
|
2023-09-11 20:55:57 -04:00
|
|
|
let args = serde_json::from_value(json!({
|
|
|
|
"diagnostics": [
|
|
|
|
{
|
|
|
|
"messageText": "Unknown compiler option 'invalid'.",
|
|
|
|
"category": 1,
|
|
|
|
"code": 5023
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"stats": [["a", 12]]
|
|
|
|
}))
|
|
|
|
.unwrap();
|
2024-04-12 15:45:38 -04:00
|
|
|
op_respond_inner(&mut state, args);
|
2022-03-14 13:44:15 -04:00
|
|
|
let state = state.borrow::<State>();
|
2020-10-13 19:52:49 -04:00
|
|
|
assert_eq!(
|
|
|
|
state.maybe_response,
|
|
|
|
Some(RespondArgs {
|
2020-10-27 20:52:20 -04:00
|
|
|
diagnostics: Diagnostics::new(vec![Diagnostic {
|
2020-10-13 19:52:49 -04:00
|
|
|
category: DiagnosticCategory::Error,
|
|
|
|
code: 5023,
|
|
|
|
start: None,
|
|
|
|
end: None,
|
2024-01-10 17:40:30 -05:00
|
|
|
original_source_start: None,
|
2020-10-13 19:52:49 -04:00
|
|
|
message_text: Some(
|
|
|
|
"Unknown compiler option \'invalid\'.".to_string()
|
|
|
|
),
|
|
|
|
message_chain: None,
|
|
|
|
source: None,
|
|
|
|
source_line: None,
|
|
|
|
file_name: None,
|
|
|
|
related_information: None,
|
|
|
|
}]),
|
|
|
|
stats: Stats(vec![("a".to_string(), 12)])
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
2020-10-27 20:52:20 -04:00
|
|
|
async fn test_exec_basic() {
|
2023-03-13 19:31:03 -04:00
|
|
|
let specifier = ModuleSpecifier::parse("https://deno.land/x/a.ts").unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
let actual = test_exec(&specifier)
|
2020-10-13 19:52:49 -04:00
|
|
|
.await
|
2020-10-27 20:52:20 -04:00
|
|
|
.expect("exec should not have errored");
|
|
|
|
assert!(actual.diagnostics.is_empty());
|
2020-10-13 19:52:49 -04:00
|
|
|
assert!(actual.maybe_tsbuildinfo.is_some());
|
|
|
|
assert_eq!(actual.stats.0.len(), 12);
|
|
|
|
}
|
2020-10-24 16:05:33 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn test_exec_reexport_dts() {
|
2023-03-13 19:31:03 -04:00
|
|
|
let specifier = ModuleSpecifier::parse("file:///reexports.ts").unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
let actual = test_exec(&specifier)
|
2020-10-24 16:05:33 -04:00
|
|
|
.await
|
2020-10-27 20:52:20 -04:00
|
|
|
.expect("exec should not have errored");
|
|
|
|
assert!(actual.diagnostics.is_empty());
|
2020-10-24 16:05:33 -04:00
|
|
|
assert!(actual.maybe_tsbuildinfo.is_some());
|
|
|
|
assert_eq!(actual.stats.0.len(), 12);
|
|
|
|
}
|
2020-10-27 20:52:20 -04:00
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
async fn fix_lib_ref() {
|
2023-03-13 19:31:03 -04:00
|
|
|
let specifier = ModuleSpecifier::parse("file:///libref.ts").unwrap();
|
2020-10-27 20:52:20 -04:00
|
|
|
let actual = test_exec(&specifier)
|
|
|
|
.await
|
|
|
|
.expect("exec should not have errored");
|
|
|
|
assert!(actual.diagnostics.is_empty());
|
|
|
|
}
|
2020-10-13 19:52:49 -04:00
|
|
|
}
|