From 7c86cf026d85445e0a0866c81e0dedcb02caaafb Mon Sep 17 00:00:00 2001 From: Omar Jatoi Date: Thu, 3 Oct 2024 12:51:26 -0400 Subject: [PATCH 1/4] fix `roc_cache_dir` function - roc_cache_dir() will return "~/.cache/roc" - new function roc_cache_packages_dir() will return "~/.cache/roc/packages" --- crates/packaging/src/cache.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/packaging/src/cache.rs b/crates/packaging/src/cache.rs index 4efa78ba1ba..82234242986 100644 --- a/crates/packaging/src/cache.rs +++ b/crates/packaging/src/cache.rs @@ -207,7 +207,7 @@ const ROC_CACHE_DIR_NAME: &str = "roc"; /// This looks up environment variables, so it should ideally be called once and then cached! /// -/// Returns a path of the form cache_dir_path.join(ROC_CACHE_DIR_NAME).join("packages") +/// Returns a path of the form cache_dir_path.join(ROC_CACHE_DIR_NAME) /// where cache_dir_path is: /// - The XDG_CACHE_HOME environment varaible, if it's set. /// - Otherwise, ~/.cache on UNIX and %APPDATA% on Windows. @@ -222,14 +222,11 @@ const ROC_CACHE_DIR_NAME: &str = "roc"; pub fn roc_cache_dir() -> PathBuf { use std::{env, process}; - const PACKAGES_DIR_NAME: &str = "packages"; - // Respect XDG, if the system appears to be using it. // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html match env::var_os("XDG_CACHE_HOME") { Some(xdg_cache_home) => Path::new(&xdg_cache_home) .join(ROC_CACHE_DIR_NAME) - .join(PACKAGES_DIR_NAME), None => { #[cfg(windows)] { @@ -241,7 +238,6 @@ pub fn roc_cache_dir() -> PathBuf { { Path::new(&appdata) .join(ROC_CACHE_DIR_NAME) - .join(PACKAGES_DIR_NAME) } else { eprintln!("roc needs either the %APPDATA% or else the %XDG_CACHE_HOME% environment variables set. Please set one of these environment variables and re-run roc!"); process::exit(1); @@ -255,7 +251,6 @@ pub fn roc_cache_dir() -> PathBuf { Path::new(&home) .join(".cache") .join(ROC_CACHE_DIR_NAME) - .join(PACKAGES_DIR_NAME) } else { eprintln!("roc needs either the $HOME or else the $XDG_CACHE_HOME environment variables set. Please set one of these environment variables and re-run roc!"); process::exit(1); @@ -265,9 +260,26 @@ pub fn roc_cache_dir() -> PathBuf { } } +/// Returns a path of the form roc_cache_dir().join("packages") +#[cfg(not(target_family = "wasm"))] +pub fn roc_cache_packages_dir() -> PathBuf { + const PACKAGES_DIR_NAME: &str = "packages"; + roc_cache_dir().join(PACKAGES_DIR_NAME) +} + /// WASI doesn't have a home directory, so just make the cache dir in the current directory /// https://github.com/WebAssembly/wasi-filesystem/issues/59 #[cfg(target_family = "wasm")] pub fn roc_cache_dir() -> PathBuf { PathBuf::from(".cache").join(ROC_CACHE_DIR_NAME) } + +/// WASI doesn't have a home directory, so just make the cache dir in the current directory +/// https://github.com/WebAssembly/wasi-filesystem/issues/59 +#[cfg(target_family = "wasm")] +pub fn roc_cache_packages_dir() -> PathBuf { + const PACKAGES_DIR_NAME: &str = "packages"; + PathBuf::from(".cache") + .join(ROC_CACHE_DIR_NAME) + .join(PACKAGES_DIR_NAME) +} From 3f318e2235a5323434838b48c1b1748d6cf329df Mon Sep 17 00:00:00 2001 From: Omar Jatoi Date: Thu, 3 Oct 2024 12:52:52 -0400 Subject: [PATCH 2/4] update existing calls to `roc_cache_dir()` using `roc_cache_packages_dir()` instead, which will return "~/.cache/roc/packages", which was the existing functionality, but `roc_cache_dir()` will now return "~/.cache/roc" --- crates/cli/src/lib.rs | 2 +- crates/cli/src/main.rs | 12 ++++++------ crates/compiler/load_internal/src/file.rs | 2 +- crates/docs/src/lib.rs | 2 +- crates/glue/src/load.rs | 4 ++-- crates/language_server/src/analysis.rs | 2 +- crates/language_server/src/convert.rs | 2 +- crates/repl_eval/src/gen.rs | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index b4703e935d7..83c7270350f 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -564,7 +564,7 @@ pub fn test(matches: &ArgMatches, target: Target) -> io::Result { arena, path.to_path_buf(), opt_main_path.cloned(), - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), load_config, ); diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 97adb89a0a4..b20962af5e3 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -49,7 +49,7 @@ fn main() -> io::Result<()> { BuildConfig::BuildAndRunIfNoErrors, Triple::host().into(), None, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), LinkType::Executable, ) } else { @@ -64,7 +64,7 @@ fn main() -> io::Result<()> { BuildConfig::BuildAndRun, Triple::host().into(), None, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), LinkType::Executable, ) } else { @@ -90,7 +90,7 @@ fn main() -> io::Result<()> { BuildConfig::BuildAndRunIfNoErrors, Triple::host().into(), None, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), LinkType::Executable, ) } else { @@ -127,7 +127,7 @@ fn main() -> io::Result<()> { let function_kind = FunctionKind::from_env(); roc_linker::generate_stub_lib( input_path, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), target, function_kind, ); @@ -199,7 +199,7 @@ fn main() -> io::Result<()> { BuildConfig::BuildOnly, target, out_path, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), link_type, )?) } @@ -222,7 +222,7 @@ fn main() -> io::Result<()> { roc_file_path.to_owned(), opt_main_path.cloned(), emit_timings, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), threading, ) { Ok((problems, total_time)) => { diff --git a/crates/compiler/load_internal/src/file.rs b/crates/compiler/load_internal/src/file.rs index 0b6881e42af..2d3fd6e307c 100644 --- a/crates/compiler/load_internal/src/file.rs +++ b/crates/compiler/load_internal/src/file.rs @@ -784,7 +784,7 @@ impl<'a> State<'a> { number_of_workers: usize, exec_mode: ExecutionMode, ) -> Self { - let cache_dir = roc_packaging::cache::roc_cache_dir(); + let cache_dir = roc_packaging::cache::roc_cache_packages_dir(); let dependencies = Dependencies::new(exec_mode.goal_phase()); Self { diff --git a/crates/docs/src/lib.rs b/crates/docs/src/lib.rs index 2ff8341d562..603651e8af7 100644 --- a/crates/docs/src/lib.rs +++ b/crates/docs/src/lib.rs @@ -483,7 +483,7 @@ pub fn load_module_for_docs(filename: PathBuf) -> LoadedModule { &arena, filename, None, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), load_config, ) { Ok(loaded) => loaded, diff --git a/crates/glue/src/load.rs b/crates/glue/src/load.rs index 00b580c0dcc..0ae5f9c5e1c 100644 --- a/crates/glue/src/load.rs +++ b/crates/glue/src/load.rs @@ -88,7 +88,7 @@ pub fn generate( linking_strategy, true, None, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), load_config, Some(dylib_dir.path()), ), @@ -414,7 +414,7 @@ pub fn load_types( arena, full_file_path, None, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), LoadConfig { target, function_kind, diff --git a/crates/language_server/src/analysis.rs b/crates/language_server/src/analysis.rs index 96523075ec2..6c8ae3e2eeb 100644 --- a/crates/language_server/src/analysis.rs +++ b/crates/language_server/src/analysis.rs @@ -114,7 +114,7 @@ pub(crate) fn global_analysis(doc_info: DocInfo) -> Vec { roc_target::Target::LinuxX64, roc_load::FunctionKind::LambdaSet, roc_reporting::report::RenderTarget::LanguageServer, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), roc_reporting::report::DEFAULT_PALETTE, ); diff --git a/crates/language_server/src/convert.rs b/crates/language_server/src/convert.rs index 8833791c51f..c546847299c 100644 --- a/crates/language_server/src/convert.rs +++ b/crates/language_server/src/convert.rs @@ -160,7 +160,7 @@ pub(crate) mod diag { LoadingProblem::CouldNotFindCacheDir => { format!( "Could not find Roc cache directory {}", - roc_packaging::cache::roc_cache_dir().display() + roc_packaging::cache::roc_cache_packages_dir().display() ) } LoadingProblem::UnrecognizedPackageShorthand { shorthand, .. } => { diff --git a/crates/repl_eval/src/gen.rs b/crates/repl_eval/src/gen.rs index 6731d167f00..f4523832557 100644 --- a/crates/repl_eval/src/gen.rs +++ b/crates/repl_eval/src/gen.rs @@ -61,7 +61,7 @@ pub fn compile_to_mono<'a, 'i, I: Iterator>( module_src, src_dir, None, - RocCacheDir::Persistent(cache::roc_cache_dir().as_path()), + RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()), LoadConfig { target, function_kind: FunctionKind::LambdaSet, From 820dccab99d042c10476b97c923e1b587e72ae20 Mon Sep 17 00:00:00 2001 From: Omar Jatoi Date: Thu, 3 Oct 2024 13:06:01 -0400 Subject: [PATCH 3/4] no separate `roc_cache_packages_dir()` for `wasm` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit since `roc_cache_packages_dir()` is essentially just calling `roc_cache_dir()` and joining with `PACKAGES_DIR_NAME`, we don’t need a separate implementation for the WebAssembly target since `roc_cache_dir()` already has a separate implementation --- crates/packaging/src/cache.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/crates/packaging/src/cache.rs b/crates/packaging/src/cache.rs index 82234242986..ee0f620d5a4 100644 --- a/crates/packaging/src/cache.rs +++ b/crates/packaging/src/cache.rs @@ -260,13 +260,6 @@ pub fn roc_cache_dir() -> PathBuf { } } -/// Returns a path of the form roc_cache_dir().join("packages") -#[cfg(not(target_family = "wasm"))] -pub fn roc_cache_packages_dir() -> PathBuf { - const PACKAGES_DIR_NAME: &str = "packages"; - roc_cache_dir().join(PACKAGES_DIR_NAME) -} - /// WASI doesn't have a home directory, so just make the cache dir in the current directory /// https://github.com/WebAssembly/wasi-filesystem/issues/59 #[cfg(target_family = "wasm")] @@ -274,12 +267,8 @@ pub fn roc_cache_dir() -> PathBuf { PathBuf::from(".cache").join(ROC_CACHE_DIR_NAME) } -/// WASI doesn't have a home directory, so just make the cache dir in the current directory -/// https://github.com/WebAssembly/wasi-filesystem/issues/59 -#[cfg(target_family = "wasm")] +/// Returns a path of the form roc_cache_dir().join("packages") pub fn roc_cache_packages_dir() -> PathBuf { const PACKAGES_DIR_NAME: &str = "packages"; - PathBuf::from(".cache") - .join(ROC_CACHE_DIR_NAME) - .join(PACKAGES_DIR_NAME) + roc_cache_dir().join(PACKAGES_DIR_NAME) } From 7ba54cd91bec8c4e034bd45fa3124791a8fd9d84 Mon Sep 17 00:00:00 2001 From: Omar Jatoi Date: Thu, 3 Oct 2024 13:32:41 -0400 Subject: [PATCH 4/4] add missing comma --- crates/packaging/src/cache.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/crates/packaging/src/cache.rs b/crates/packaging/src/cache.rs index ee0f620d5a4..6040328d6aa 100644 --- a/crates/packaging/src/cache.rs +++ b/crates/packaging/src/cache.rs @@ -225,8 +225,7 @@ pub fn roc_cache_dir() -> PathBuf { // Respect XDG, if the system appears to be using it. // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html match env::var_os("XDG_CACHE_HOME") { - Some(xdg_cache_home) => Path::new(&xdg_cache_home) - .join(ROC_CACHE_DIR_NAME) + Some(xdg_cache_home) => Path::new(&xdg_cache_home).join(ROC_CACHE_DIR_NAME), None => { #[cfg(windows)] { @@ -236,8 +235,7 @@ pub fn roc_cache_dir() -> PathBuf { // https://learn.microsoft.com/en-us/windows/deployment/usmt/usmt-recognized-environment-variables env::var_os("APPDATA").or_else(|| env::var_os("CSIDL_APPDATA")) { - Path::new(&appdata) - .join(ROC_CACHE_DIR_NAME) + Path::new(&appdata).join(ROC_CACHE_DIR_NAME) } else { eprintln!("roc needs either the %APPDATA% or else the %XDG_CACHE_HOME% environment variables set. Please set one of these environment variables and re-run roc!"); process::exit(1); @@ -248,9 +246,7 @@ pub fn roc_cache_dir() -> PathBuf { { // e.g. $HOME/.cache/roc if let Some(home) = env::var_os("HOME") { - Path::new(&home) - .join(".cache") - .join(ROC_CACHE_DIR_NAME) + Path::new(&home).join(".cache").join(ROC_CACHE_DIR_NAME) } else { eprintln!("roc needs either the $HOME or else the $XDG_CACHE_HOME environment variables set. Please set one of these environment variables and re-run roc!"); process::exit(1);