Make roc_cache_dir() panic if it can't find $HOME

This commit is contained in:
Richard Feldman 2022-11-20 20:48:09 -05:00
parent 4987fe1656
commit 7d2fa63dca
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
7 changed files with 31 additions and 61 deletions

View file

@ -409,9 +409,6 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
let target = &triple;
let opt_level = opt_level;
let target_info = TargetInfo::from(target);
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
// Step 1: compile the app and generate the .o file
let subs_by_module = Default::default();
@ -428,7 +425,7 @@ pub fn test(matches: &ArgMatches, triple: Triple) -> io::Result<i32> {
arena,
path.to_path_buf(),
subs_by_module,
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
load_config,
)
.unwrap();

View file

@ -34,15 +34,11 @@ fn main() -> io::Result<()> {
let exit_code = match matches.subcommand() {
None => {
if matches.is_present(ROC_FILE) {
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
build(
&matches,
BuildConfig::BuildAndRunIfNoErrors,
Triple::host(),
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
LinkType::Executable,
)
} else {
@ -53,15 +49,11 @@ fn main() -> io::Result<()> {
}
Some((CMD_RUN, matches)) => {
if matches.is_present(ROC_FILE) {
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
build(
matches,
BuildConfig::BuildAndRun,
Triple::host(),
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
LinkType::Executable,
)
} else {
@ -81,15 +73,11 @@ fn main() -> io::Result<()> {
}
Some((CMD_DEV, matches)) => {
if matches.is_present(ROC_FILE) {
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
build(
matches,
BuildConfig::BuildAndRunIfNoErrors,
Triple::host(),
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
LinkType::Executable,
)
} else {
@ -113,21 +101,14 @@ fn main() -> io::Result<()> {
Some((CMD_GEN_STUB_LIB, matches)) => {
let input_path = Path::new(matches.value_of_os(ROC_FILE).unwrap());
let target: Target = matches.value_of_t(FLAG_TARGET).unwrap_or_default();
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
roc_linker::generate_stub_lib(
input_path,
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
&target.to_triple(),
)
}
Some((CMD_BUILD, matches)) => {
let target: Target = matches.value_of_t(FLAG_TARGET).unwrap_or_default();
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
let link_type = match (
matches.is_present(FLAG_LIB),
matches.is_present(FLAG_NO_LINK),
@ -142,7 +123,7 @@ fn main() -> io::Result<()> {
matches,
BuildConfig::BuildOnly,
target.to_triple(),
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
link_type,
)?)
}
@ -152,9 +133,6 @@ fn main() -> io::Result<()> {
let emit_timings = matches.is_present(FLAG_TIME);
let filename = matches.value_of_os(ROC_FILE).unwrap();
let roc_file_path = PathBuf::from(filename);
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
let threading = match matches
.value_of(roc_cli::FLAG_MAX_THREADS)
.and_then(|s| s.parse::<usize>().ok())
@ -169,7 +147,7 @@ fn main() -> io::Result<()> {
&arena,
roc_file_path,
emit_timings,
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
threading,
) {
Ok((problems, total_time)) => {

View file

@ -453,9 +453,6 @@ fn render_sidebar<'a, I: Iterator<Item = (&'a ModuleDocumentation, Vec<String>)>
pub fn load_modules_for_files(filenames: Vec<PathBuf>) -> Vec<LoadedModule> {
let arena = Bump::new();
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
let mut modules = Vec::with_capacity(filenames.len());
for filename in filenames {
@ -470,7 +467,7 @@ pub fn load_modules_for_files(filenames: Vec<PathBuf>) -> Vec<LoadedModule> {
&arena,
filename,
Default::default(),
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
load_config,
) {
Ok(loaded) => modules.push(loaded),

View file

@ -128,13 +128,9 @@ fn run_event_loop(project_dir_path_opt: Option<&Path>) -> Result<(), Box<dyn Err
println!("Loading file {:?}...", file_path_str);
let file_path = Path::new(&file_path_str);
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
let loaded_module = load_module(
file_path,
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
Threading::AllAvailable,
);

View file

@ -83,9 +83,6 @@ pub fn load_types(
ignore_errors: IgnoreErrors,
) -> Result<Vec<(Types, TargetInfo)>, io::Error> {
let target_info = (&Triple::host()).into();
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
let arena = &Bump::new();
let subs_by_module = Default::default();
let LoadedModule {
@ -100,7 +97,7 @@ pub fn load_types(
arena,
full_file_path,
subs_by_module,
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
LoadConfig {
target_info,
render: RenderTarget::Generic,

View file

@ -107,30 +107,38 @@ const ROC_VERSION: &str = include_str!("../../../version.txt");
///
/// Returns None if XDG_CACHE_HOME is not set, and also we can't determine the home directory
/// (or if %APPDATA% is missing on Windows) on this system.
pub fn roc_cache_dir() -> Option<PathBuf> {
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) => Some(
Path::new(&xdg_cache_home)
Some(xdg_cache_home) => Path::new(&xdg_cache_home)
.join(ROC_CACHE_DIR_NAME)
.join(ROC_VERSION),
),
None => {
#[cfg(windows)]
{
// e.g. %APPDATA%\\Roc
Some(Path::new(&env::var_os("APPDATA")?).join(ROC_CACHE_DIR_NAME))
if let Some(appdata) =
// CSIDL_APPDATA is the same as APPDATA, according to:
// 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)
} 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!");
std::process::exit(1);
}
}
#[cfg(unix)]
{
// e.g. $HOME/.cache/roc
Some(
Path::new(&env::var_os("HOME")?)
.join(".cache")
.join(ROC_CACHE_DIR_NAME),
)
if let Some(home) = env::var_os("HOME") {
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!");
std::process::exit(1);
}
}
}
}

View file

@ -54,16 +54,13 @@ pub fn compile_to_mono<'a, 'i, I: Iterator<Item = &'i str>>(
let src_dir = PathBuf::from("fake/test/path");
let (bytes_before_expr, module_src) = promote_expr_to_module(arena, defs, expr);
let exposed_types = Default::default();
let roc_cache_dir = cache::roc_cache_dir().unwrap_or_else(|| {
todo!("Gracefully handle not being able to find default Roc cache dir.")
});
let loaded = roc_load::load_and_monomorphize_from_str(
arena,
filename,
module_src,
src_dir,
exposed_types,
RocCacheDir::Persistent(roc_cache_dir.as_path()),
RocCacheDir::Persistent(cache::roc_cache_dir().as_path()),
LoadConfig {
target_info,
render: roc_reporting::report::RenderTarget::ColorTerminal,