From 933eebdde929140b1db0712064e8201a0c486758 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 25 Nov 2022 19:04:30 -0500 Subject: [PATCH] refactor: `DenoDir` - move to cache folder and make `root_dir` private (#16823) --- cli/args/mod.rs | 2 +- cli/{ => cache}/deno_dir.rs | 46 +++++++++++++++++++++++++++++++++++-- cli/cache/mod.rs | 2 ++ cli/lsp/language_server.rs | 20 +++++++--------- cli/lsp/registries.rs | 6 ++--- cli/main.rs | 3 +-- cli/npm/cache.rs | 9 ++++---- cli/proc_state.rs | 6 ++--- cli/tools/info.rs | 16 ++++--------- cli/tools/repl/mod.rs | 2 +- cli/tools/standalone.rs | 4 ++-- cli/tools/upgrade.rs | 16 ++++++------- cli/worker.rs | 4 +--- 13 files changed, 83 insertions(+), 53 deletions(-) rename cli/{ => cache}/deno_dir.rs (81%) diff --git a/cli/args/mod.rs b/cli/args/mod.rs index ae43bccaf0..50a407ee3d 100644 --- a/cli/args/mod.rs +++ b/cli/args/mod.rs @@ -44,7 +44,7 @@ use std::net::SocketAddr; use std::path::PathBuf; use std::sync::Arc; -use crate::deno_dir::DenoDir; +use crate::cache::DenoDir; use crate::file_fetcher::get_root_cert_store; use crate::file_fetcher::CacheSetting; use crate::fs_util; diff --git a/cli/deno_dir.rs b/cli/cache/deno_dir.rs similarity index 81% rename from cli/deno_dir.rs rename to cli/cache/deno_dir.rs index c0d116a6a1..217658c15e 100644 --- a/cli/deno_dir.rs +++ b/cli/cache/deno_dir.rs @@ -1,6 +1,6 @@ // Copyright 2018-2022 the Deno authors. All rights reserved. MIT license. -use crate::cache::DiskCache; +use super::DiskCache; use std::path::PathBuf; @@ -9,7 +9,8 @@ use std::path::PathBuf; #[derive(Clone)] pub struct DenoDir { /// Example: /Users/rld/.deno/ - pub root: PathBuf, + /// Note: This is not exposed in order to encourage using re-usable methods. + root: PathBuf, /// Used by TsCompiler to cache compiler output. pub gen_cache: DiskCache, } @@ -46,6 +47,11 @@ impl DenoDir { Ok(deno_dir) } + /// The root directory of the DENO_DIR for display purposes only. + pub fn root_path_for_display(&self) -> std::path::Display { + self.root.display() + } + /// Path for the incremental cache used for formatting. pub fn fmt_incremental_cache_db_file_path(&self) -> PathBuf { // bump this version name to invalidate the entire cache @@ -75,6 +81,42 @@ impl DenoDir { // bump this version name to invalidate the entire cache self.root.join("check_cache_v1") } + + /// Path to the registries cache, used for the lps. + pub fn registries_folder_path(&self) -> PathBuf { + self.root.join("registries") + } + + /// Path to the dependencies cache folder. + pub fn deps_folder_path(&self) -> PathBuf { + self.root.join("deps") + } + + /// Path to the origin data cache folder. + pub fn origin_data_folder_path(&self) -> PathBuf { + // TODO(@crowlKats): change to origin_data for 2.0 + self.root.join("location_data") + } + + /// File used for the upgrade checker. + pub fn upgrade_check_file_path(&self) -> PathBuf { + self.root.join("latest.txt") + } + + /// Folder used for the npm cache. + pub fn npm_folder_path(&self) -> PathBuf { + self.root.join("npm") + } + + /// Path used for the REPL history file. + pub fn repl_history_file_path(&self) -> PathBuf { + self.root.join("deno_history.txt") + } + + /// Folder path used for downloading new versions of deno. + pub fn dl_folder_path(&self) -> PathBuf { + self.root.join("dl") + } } /// To avoid the poorly managed dirs crate diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs index 16390013a9..cf9a4c4413 100644 --- a/cli/cache/mod.rs +++ b/cli/cache/mod.rs @@ -16,6 +16,7 @@ use std::sync::Arc; mod check; mod common; +mod deno_dir; mod disk_cache; mod emit; mod incremental; @@ -24,6 +25,7 @@ mod parsed_source; pub use check::TypeCheckCache; pub use common::FastInsecureHasher; +pub use deno_dir::DenoDir; pub use disk_cache::DiskCache; pub use emit::EmitCache; pub use incremental::IncrementalCache; diff --git a/cli/lsp/language_server.rs b/cli/lsp/language_server.rs index 4595d21465..4e7c4b240b 100644 --- a/cli/lsp/language_server.rs +++ b/cli/lsp/language_server.rs @@ -63,8 +63,7 @@ use crate::args::Flags; use crate::args::FmtConfig; use crate::args::LintConfig; use crate::args::TsConfig; -use crate::deno_dir; -use crate::deno_dir::DenoDir; +use crate::cache::DenoDir; use crate::file_fetcher::get_root_cert_store; use crate::file_fetcher::get_source_from_data_url; use crate::file_fetcher::CacheSetting; @@ -80,9 +79,6 @@ use crate::progress_bar::ProgressBar; use crate::tools::fmt::format_file; use crate::tools::fmt::format_parsed_source; -pub const REGISTRIES_PATH: &str = "registries"; -const CACHE_PATH: &str = "deps"; - #[derive(Debug, Clone)] pub struct LanguageServer(Arc>); @@ -267,14 +263,14 @@ fn create_lsp_npm_resolver( impl Inner { fn new(client: Client) -> Self { let maybe_custom_root = env::var("DENO_DIR").map(String::into).ok(); - let dir = deno_dir::DenoDir::new(maybe_custom_root) - .expect("could not access DENO_DIR"); - let module_registries_location = dir.root.join(REGISTRIES_PATH); + let dir = + DenoDir::new(maybe_custom_root).expect("could not access DENO_DIR"); + let module_registries_location = dir.registries_folder_path(); let http_client = HttpClient::new(None, None).unwrap(); let module_registries = ModuleRegistry::new(&module_registries_location, http_client.clone()) .unwrap(); - let location = dir.root.join(CACHE_PATH); + let location = dir.deps_folder_path(); let documents = Documents::new(&location); let cache_metadata = cache::CacheMetadata::new(&location); let performance = Arc::new(Performance::default()); @@ -521,7 +517,7 @@ impl Inner { let maybe_custom_root = new_cache_path .clone() .or_else(|| env::var("DENO_DIR").map(String::into).ok()); - let dir = deno_dir::DenoDir::new(maybe_custom_root)?; + let dir = DenoDir::new(maybe_custom_root)?; let workspace_settings = self.config.get_workspace_settings(); let maybe_root_path = self .config @@ -537,13 +533,13 @@ impl Inner { root_cert_store, workspace_settings.unsafely_ignore_certificate_errors, )?; - let module_registries_location = dir.root.join(REGISTRIES_PATH); + let module_registries_location = dir.registries_folder_path(); self.module_registries = ModuleRegistry::new(&module_registries_location, client.clone())?; self.module_registries_location = module_registries_location; self.npm_resolver = create_lsp_npm_resolver(&dir, client); // update the cache path - let location = dir.root.join(CACHE_PATH); + let location = dir.deps_folder_path(); self.documents.set_location(&location); self.cache_metadata.set_location(&location); self.maybe_cache_path = new_cache_path; diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index 6fd48d8b9d..43500e6970 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -12,7 +12,7 @@ use super::path_to_regex::StringOrNumber; use super::path_to_regex::StringOrVec; use super::path_to_regex::Token; -use crate::deno_dir; +use crate::cache::DenoDir; use crate::file_fetcher::CacheSetting; use crate::file_fetcher::FileFetcher; use crate::http_cache::HttpCache; @@ -422,8 +422,8 @@ impl Default for ModuleRegistry { // This only gets used when creating the tsc runtime and for testing, and so // it shouldn't ever actually access the DenoDir, so it doesn't support a // custom root. - let dir = deno_dir::DenoDir::new(None).unwrap(); - let location = dir.root.join("registries"); + let dir = DenoDir::new(None).unwrap(); + let location = dir.registries_folder_path(); let http_client = HttpClient::new(None, None).unwrap(); Self::new(&location, http_client).unwrap() } diff --git a/cli/main.rs b/cli/main.rs index e42c6325e1..4eaaeb755a 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -4,7 +4,6 @@ mod args; mod auth_tokens; mod cache; mod checksum; -mod deno_dir; mod deno_std; mod diff; mod display; @@ -706,7 +705,7 @@ To grant permissions, set them before the script argument. For example: // Run a background task that checks for available upgrades. If an earlier // run of this background task found a new version of Deno. - tools::upgrade::check_for_upgrades(ps.dir.root.clone()); + tools::upgrade::check_for_upgrades(ps.dir.upgrade_check_file_path()); let main_module = if NpmPackageReference::from_str(&run_flags.script).is_ok() { diff --git a/cli/npm/cache.rs b/cli/npm/cache.rs index ce2208db71..b2c8423092 100644 --- a/cli/npm/cache.rs +++ b/cli/npm/cache.rs @@ -11,7 +11,7 @@ use deno_core::error::custom_error; use deno_core::error::AnyError; use deno_core::url::Url; -use crate::deno_dir::DenoDir; +use crate::cache::DenoDir; use crate::file_fetcher::CacheSetting; use crate::fs_util; use crate::http_util::HttpClient; @@ -146,7 +146,7 @@ impl Default for ReadonlyNpmCache { // This only gets used when creating the tsc runtime and for testing, and so // it shouldn't ever actually access the DenoDir, so it doesn't support a // custom root. - Self::from_deno_dir(&crate::deno_dir::DenoDir::new(None).unwrap()) + Self::from_deno_dir(&DenoDir::new(None).unwrap()) } } @@ -173,7 +173,7 @@ impl ReadonlyNpmCache { } pub fn from_deno_dir(dir: &DenoDir) -> Self { - Self::new(dir.root.join("npm")) + Self::new(dir.npm_folder_path()) } pub fn package_folder_for_id( @@ -510,7 +510,8 @@ mod test { #[test] fn should_get_package_folder() { - let root_dir = crate::deno_dir::DenoDir::new(None).unwrap().root; + let deno_dir = crate::cache::DenoDir::new(None).unwrap(); + let root_dir = deno_dir.npm_folder_path(); let cache = ReadonlyNpmCache::new(root_dir.clone()); let registry_url = Url::parse("https://registry.npmjs.org/").unwrap(); diff --git a/cli/proc_state.rs b/cli/proc_state.rs index 019b6e4473..dc80ca8db7 100644 --- a/cli/proc_state.rs +++ b/cli/proc_state.rs @@ -8,12 +8,12 @@ use crate::args::TsConfigType; use crate::args::TsTypeLib; use crate::args::TypeCheckMode; use crate::cache; +use crate::cache::DenoDir; use crate::cache::EmitCache; use crate::cache::FastInsecureHasher; use crate::cache::NodeAnalysisCache; use crate::cache::ParsedSourceCache; use crate::cache::TypeCheckCache; -use crate::deno_dir; use crate::emit::emit_parsed_source; use crate::file_fetcher::FileFetcher; use crate::graph_util::graph_lock_or_exit; @@ -73,7 +73,7 @@ use std::sync::Arc; pub struct ProcState(Arc); pub struct Inner { - pub dir: deno_dir::DenoDir, + pub dir: DenoDir, pub file_fetcher: FileFetcher, pub options: Arc, pub emit_cache: EmitCache, @@ -152,7 +152,7 @@ impl ProcState { let shared_array_buffer_store = SharedArrayBufferStore::default(); let compiled_wasm_module_store = CompiledWasmModuleStore::default(); let dir = cli_options.resolve_deno_dir()?; - let deps_cache_location = dir.root.join("deps"); + let deps_cache_location = dir.deps_folder_path(); let http_cache = http_cache::HttpCache::new(&deps_cache_location); let root_cert_store = cli_options.resolve_root_cert_store()?; let cache_usage = cli_options.cache_setting(); diff --git a/cli/tools/info.rs b/cli/tools/info.rs index f718d8e52d..38aa40a5e3 100644 --- a/cli/tools/info.rs +++ b/cli/tools/info.rs @@ -22,7 +22,6 @@ use crate::args::Flags; use crate::args::InfoFlags; use crate::checksum; use crate::display; -use crate::lsp; use crate::npm::NpmPackageId; use crate::npm::NpmPackageReference; use crate::npm::NpmPackageReq; @@ -58,13 +57,12 @@ fn print_cache_info( json: bool, location: Option<&deno_core::url::Url>, ) -> Result<(), AnyError> { - let deno_dir = &state.dir.root; + let deno_dir = &state.dir.root_path_for_display(); let modules_cache = &state.file_fetcher.get_http_cache_location(); let npm_cache = &state.npm_cache.as_readonly().get_cache_location(); let typescript_cache = &state.dir.gen_cache.location; - let registry_cache = - &state.dir.root.join(lsp::language_server::REGISTRIES_PATH); - let mut origin_dir = state.dir.root.join("location_data"); + let registry_cache = &state.dir.registries_folder_path(); + let mut origin_dir = state.dir.origin_data_folder_path(); if let Some(location) = &location { origin_dir = @@ -75,7 +73,7 @@ fn print_cache_info( if json { let mut output = json!({ - "denoDir": deno_dir, + "denoDir": deno_dir.to_string(), "modulesCache": modules_cache, "npmCache": npm_cache, "typescriptCache": typescript_cache, @@ -89,11 +87,7 @@ fn print_cache_info( display::write_json_to_stdout(&output) } else { - println!( - "{} {}", - colors::bold("DENO_DIR location:"), - deno_dir.display() - ); + println!("{} {}", colors::bold("DENO_DIR location:"), deno_dir); println!( "{} {}", colors::bold("Remote modules cache:"), diff --git a/cli/tools/repl/mod.rs b/cli/tools/repl/mod.rs index 597b3ff5f0..1cdb17ab1b 100644 --- a/cli/tools/repl/mod.rs +++ b/cli/tools/repl/mod.rs @@ -89,7 +89,7 @@ pub async fn run( sync_sender: rustyline_channel.0, }; - let history_file_path = ps.dir.root.join("deno_history.txt"); + let history_file_path = ps.dir.repl_history_file_path(); let editor = ReplEditor::new(helper, history_file_path)?; if let Some(eval_files) = maybe_eval_files { diff --git a/cli/tools/standalone.rs b/cli/tools/standalone.rs index 494c2f4761..f173103cb2 100644 --- a/cli/tools/standalone.rs +++ b/cli/tools/standalone.rs @@ -5,7 +5,7 @@ use crate::args::DenoSubcommand; use crate::args::Flags; use crate::args::RunFlags; use crate::args::TypeCheckMode; -use crate::deno_dir::DenoDir; +use crate::cache::DenoDir; use crate::fs_util; use crate::standalone::Metadata; use crate::standalone::MAGIC_TRAILER; @@ -50,7 +50,7 @@ pub async fn get_base_binary( format!("release/v{}/{}", env!("CARGO_PKG_VERSION"), binary_name) }; - let download_directory = deno_dir.root.join("dl"); + let download_directory = deno_dir.dl_folder_path(); let binary_path = download_directory.join(&binary_path_suffix); if !binary_path.exists() { diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs index f398236003..c5f4698351 100644 --- a/cli/tools/upgrade.rs +++ b/cli/tools/upgrade.rs @@ -31,7 +31,6 @@ const RELEASE_URL: &str = "https://github.com/denoland/deno/releases"; // How often query server for new version. In hours. const UPGRADE_CHECK_INTERVAL: i64 = 24; -const UPGRADE_CHECK_FILE_NAME: &str = "latest.txt"; const UPGRADE_CHECK_FETCH_DELAY: Duration = Duration::from_millis(500); @@ -47,14 +46,14 @@ trait UpdateCheckerEnvironment: Clone + Send + Sync { #[derive(Clone)] struct RealUpdateCheckerEnvironment { - cache_dir: PathBuf, + cache_file_path: PathBuf, current_time: chrono::DateTime, } impl RealUpdateCheckerEnvironment { - pub fn new(cache_dir: PathBuf) -> Self { + pub fn new(cache_file_path: PathBuf) -> Self { Self { - cache_dir, + cache_file_path, // cache the current time current_time: chrono::Utc::now(), } @@ -79,12 +78,11 @@ impl UpdateCheckerEnvironment for RealUpdateCheckerEnvironment { } fn read_check_file(&self) -> String { - std::fs::read_to_string(self.cache_dir.join(UPGRADE_CHECK_FILE_NAME)) - .unwrap_or_default() + std::fs::read_to_string(&self.cache_file_path).unwrap_or_default() } fn write_check_file(&self, text: &str) { - let _ = std::fs::write(self.cache_dir.join(UPGRADE_CHECK_FILE_NAME), text); + let _ = std::fs::write(&self.cache_file_path, text); } fn current_time(&self) -> chrono::DateTime { @@ -160,12 +158,12 @@ impl UpdateChecker { } } -pub fn check_for_upgrades(cache_dir: PathBuf) { +pub fn check_for_upgrades(cache_file_path: PathBuf) { if env::var("DENO_NO_UPDATE_CHECK").is_ok() { return; } - let env = RealUpdateCheckerEnvironment::new(cache_dir); + let env = RealUpdateCheckerEnvironment::new(cache_file_path); let update_checker = UpdateChecker::new(env); if update_checker.should_check_for_new_version() { diff --git a/cli/worker.rs b/cli/worker.rs index d06864634b..fb021b614c 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -483,9 +483,7 @@ async fn create_main_worker_internal( let maybe_storage_key = ps.options.resolve_storage_key(&main_module); let origin_storage_dir = maybe_storage_key.as_ref().map(|key| { ps.dir - .root - // TODO(@crowlKats): change to origin_data for 2.0 - .join("location_data") + .origin_data_folder_path() .join(checksum::gen(&[key.as_bytes()])) }); let cache_storage_dir = maybe_storage_key.map(|key| {