Skip to content

Commit

Permalink
Merge pull request #7141 from omarjatoi/6616-fix-cache-dir
Browse files Browse the repository at this point in the history
Fix `roc_cache_dir()` and introduce `roc_cache_packages_dir()` (#6616)
  • Loading branch information
Anton-4 authored Oct 4, 2024
2 parents 2c62f77 + 7ba54cd commit 6cb2501
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 27 deletions.
2 changes: 1 addition & 1 deletion crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ pub fn test(matches: &ArgMatches, target: Target) -> io::Result<i32> {
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,
);

Expand Down
12 changes: 6 additions & 6 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
);
Expand Down Expand Up @@ -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,
)?)
}
Expand All @@ -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)) => {
Expand Down
2 changes: 1 addition & 1 deletion crates/compiler/load_internal/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions crates/glue/src/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
),
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/language_server/src/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub(crate) fn global_analysis(doc_info: DocInfo) -> Vec<AnalyzedDocument> {
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,
);

Expand Down
2 changes: 1 addition & 1 deletion crates/language_server/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, .. } => {
Expand Down
23 changes: 10 additions & 13 deletions crates/packaging/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -222,14 +222,10 @@ 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),
Some(xdg_cache_home) => Path::new(&xdg_cache_home).join(ROC_CACHE_DIR_NAME),
None => {
#[cfg(windows)]
{
Expand All @@ -239,9 +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)
.join(PACKAGES_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);
Expand All @@ -252,10 +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)
.join(PACKAGES_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);
Expand All @@ -271,3 +262,9 @@ pub fn roc_cache_dir() -> PathBuf {
pub fn roc_cache_dir() -> PathBuf {
PathBuf::from(".cache").join(ROC_CACHE_DIR_NAME)
}

/// Returns a path of the form roc_cache_dir().join("packages")
pub fn roc_cache_packages_dir() -> PathBuf {
const PACKAGES_DIR_NAME: &str = "packages";
roc_cache_dir().join(PACKAGES_DIR_NAME)
}
2 changes: 1 addition & 1 deletion crates/repl_eval/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn compile_to_mono<'a, 'i, I: Iterator<Item = &'i str>>(
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,
Expand Down

0 comments on commit 6cb2501

Please sign in to comment.