From 6d685628e924571337be05a8602dbd0713429df8 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Tue, 6 Dec 2022 23:44:57 -0800 Subject: [PATCH] new: Add `--updateCache` option. (#490) * Add new enum. * Add display for enums. * Update tests. * Add cli args. * Add blog post. * Polish. * Fix windows tests. --- .github/workflows/moon.yml | 17 +---- crates/cli/src/app.rs | 26 +++++-- crates/cli/src/commands/check.rs | 4 +- crates/cli/src/commands/run.rs | 11 ++- crates/cli/src/enums.rs | 67 ++++++++++++++-- crates/cli/src/lib.rs | 16 +++- crates/cli/tests/check_test.rs | 76 +++++++------------ crates/cli/tests/run_node_test.rs | 11 +++ crates/cli/tests/run_system_test.rs | 30 +++++++- crates/cli/tests/run_test.rs | 22 ++++++ ...run_node_test__inherits_moon_env_vars.snap | 6 +- ...em_test__unix__inherits_moon_env_vars.snap | 2 +- ...ws__caching__creates_run_state_cache.snap} | 4 +- ...ing__uses_cache_on_subsequent_runs-2.snap} | 2 +- ...ching__uses_cache_on_subsequent_runs.snap} | 2 +- ...indows__handles_process_exit_nonzero.snap} | 2 +- ...__windows__handles_process_exit_zero.snap} | 4 +- ...est__windows__inherits_moon_env_vars.snap} | 3 +- ...m_test__windows__passes_args_through.snap} | 2 +- ...ndows__retries_on_failure_till_count.snap} | 2 +- ...ystem_test__windows__runs_bat_script.snap} | 4 +- ...est__windows__runs_from_project_root.snap} | 2 +- ...t__windows__runs_from_workspace_root.snap} | 2 +- ..._system_test__windows__sets_env_vars.snap} | 2 +- crates/core/cache/src/engine.rs | 23 +++--- crates/core/cache/src/helpers.rs | 69 ++++++++++++----- crates/core/cache/src/item.rs | 4 +- .../cache/src/items/dependencies_state.rs | 2 +- crates/core/cache/src/items/projects_state.rs | 2 +- .../core/cache/src/items/run_target_state.rs | 6 +- crates/core/cache/src/items/tool_state.rs | 2 +- crates/core/cache/tests/engine_test.rs | 68 +++++++++++++++++ .../runner/src/subscribers/local_cache.rs | 14 ++-- .../runner/src/subscribers/moonbase_cache.rs | 8 +- .../node/platform/src/actions/install_deps.rs | 16 +++- packages/cli/CHANGELOG.md | 4 + tests/fixtures/cases/outputs/moon.yml | 7 ++ tests/fixtures/system/windows/envVarsMoon.bat | 1 + website/blog/2022-12-16_v0.21.mdx | 32 ++++++++ website/docs/commands/check.mdx | 5 ++ website/docs/commands/overview.mdx | 3 +- website/docs/commands/run.mdx | 1 + 42 files changed, 431 insertions(+), 155 deletions(-) rename crates/cli/tests/snapshots/{run_system_test__system_windows__caching__creates_run_state_cache.snap => run_system_test__windows__caching__creates_run_state_cache.snap} (88%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__caching__uses_cache_on_subsequent_runs-2.snap => run_system_test__windows__caching__uses_cache_on_subsequent_runs-2.snap} (79%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__caching__uses_cache_on_subsequent_runs.snap => run_system_test__windows__caching__uses_cache_on_subsequent_runs.snap} (79%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__handles_process_exit_nonzero.snap => run_system_test__windows__handles_process_exit_nonzero.snap} (83%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__handles_process_exit_zero.snap => run_system_test__windows__handles_process_exit_zero.snap} (77%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__inherits_moon_env_vars.snap => run_system_test__windows__inherits_moon_env_vars.snap} (78%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__passes_args_through.snap => run_system_test__windows__passes_args_through.snap} (85%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__retries_on_failure_till_count.snap => run_system_test__windows__retries_on_failure_till_count.snap} (92%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__runs_bat_script.snap => run_system_test__windows__runs_bat_script.snap} (69%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__runs_from_project_root.snap => run_system_test__windows__runs_from_project_root.snap} (75%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__runs_from_workspace_root.snap => run_system_test__windows__runs_from_workspace_root.snap} (75%) rename crates/cli/tests/snapshots/{run_system_test__system_windows__sets_env_vars.snap => run_system_test__windows__sets_env_vars.snap} (83%) create mode 100644 website/blog/2022-12-16_v0.21.mdx diff --git a/.github/workflows/moon.yml b/.github/workflows/moon.yml index 69715c6d0d8..08c9426cc7a 100644 --- a/.github/workflows/moon.yml +++ b/.github/workflows/moon.yml @@ -26,22 +26,9 @@ jobs: - uses: actions/checkout@v3 with: fetch-depth: 0 - - uses: actions/cache@v3 - name: Cache node modules + - uses: actions/setup-node@v3 with: - path: | - ~/.yarn - .yarn/install-state.gz - key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }} - restore-keys: ${{ runner.os }}-yarn- - - uses: actions/cache@v3 - name: Cache moon toolchain - with: - path: ~/.moon/tools - key: - ${{ runner.os }}-moon-node@${{ matrix.node-version }}-${{ - hashFiles('.moon/toolchain.yml') }} - restore-keys: ${{ runner.os }}-moon-node@${{ matrix.node-version }}- + cache: yarn - uses: actions-rs/toolchain@v1 with: toolchain: 1.65.0 diff --git a/crates/cli/src/app.rs b/crates/cli/src/app.rs index e52b504cd70..ff721c43ce9 100644 --- a/crates/cli/src/app.rs +++ b/crates/cli/src/app.rs @@ -278,19 +278,27 @@ pub enum Commands { #[command( name = "check", about = "Run all build and test related tasks for the current project.", - alias = "c" + alias = "c", + rename_all = "camelCase" )] Check { #[arg(help = "List of project IDs to explicitly check")] #[clap(group = "projects")] ids: Vec, - #[arg(long, help = "Generate a run report for the current actions")] - report: bool, - #[arg(long, help = "Run check for all projects in the workspace")] #[clap(group = "projects")] all: bool, + + #[arg(long, help = "Generate a run report for the current actions")] + report: bool, + + #[arg( + long, + short = 'u', + help = "Bypass cache and force update any existing items" + )] + update_cache: bool, }, // moon ci @@ -317,7 +325,8 @@ pub enum Commands { #[command( name = "run", about = "Run one or many project tasks and their dependent tasks.", - alias = "r" + alias = "r", + rename_all = "camelCase" )] Run { #[arg(required = true, help = "List of targets (project:task) to run")] @@ -329,6 +338,13 @@ pub enum Commands { )] dependents: bool, + #[arg( + long, + short = 'u', + help = "Bypass cache and force update any existing items" + )] + update_cache: bool, + // Debugging #[arg( value_enum, diff --git a/crates/cli/src/commands/check.rs b/crates/cli/src/commands/check.rs index d778f0642ec..9ccd2164fd8 100644 --- a/crates/cli/src/commands/check.rs +++ b/crates/cli/src/commands/check.rs @@ -6,8 +6,9 @@ use moon_project::Project; use std::env; pub struct CheckOptions { - pub report: bool, pub all: bool, + pub report: bool, + pub update_cache: bool, } const LOG_TARGET: &str = "moon:check"; @@ -54,6 +55,7 @@ pub async fn check(project_ids: &Vec, options: CheckOptions) -> Result<( &targets, RunOptions { report: options.report, + update_cache: options.update_cache, ..RunOptions::default() }, workspace, diff --git a/crates/cli/src/commands/run.rs b/crates/cli/src/commands/run.rs index cb2d1e3dc2c..91ec5678821 100644 --- a/crates/cli/src/commands/run.rs +++ b/crates/cli/src/commands/run.rs @@ -1,4 +1,4 @@ -use crate::enums::TouchedStatus; +use crate::enums::{CacheMode, TouchedStatus}; use crate::helpers::AnyError; use crate::queries::touched_files::{query_touched_files, QueryTouchedFilesOptions}; use moon::{build_dep_graph, generate_project_graph, load_workspace}; @@ -9,6 +9,7 @@ use moon_runner_context::{ProfileType, RunnerContext}; use moon_utils::is_ci; use moon_workspace::Workspace; use rustc_hash::{FxHashMap, FxHashSet}; +use std::env; use std::string::ToString; #[derive(Default)] @@ -19,6 +20,7 @@ pub struct RunOptions { pub passthrough: Vec, pub profile: Option, pub report: bool, + pub update_cache: bool, pub upstream: bool, } @@ -36,6 +38,11 @@ pub async fn run_target( workspace: Workspace, project_graph: ProjectGraph, ) -> Result<(), AnyError> { + // Force cache to update using write-only mode + if options.update_cache { + env::set_var("MOON_CACHE", CacheMode::Write.to_string()); + } + // Always query for a touched files list as it'll be used by many actions let touched_files = if options.affected || workspace.vcs.is_enabled() { query_touched_files( @@ -74,7 +81,7 @@ pub async fn run_target( println!( "Target(s) {} not affected by touched files (using status {})", targets_list, - color::symbol(&options.status.to_string().to_lowercase()) + color::symbol(&options.status.to_string()) ); } } else { diff --git a/crates/cli/src/enums.rs b/crates/cli/src/enums.rs index a0e0fc97b3a..adb387b2b16 100644 --- a/crates/cli/src/enums.rs +++ b/crates/cli/src/enums.rs @@ -1,16 +1,34 @@ use clap::ValueEnum; use serde::{Deserialize, Serialize}; -use strum::Display; +use std::fmt::{Display, Error, Formatter}; -#[derive(ValueEnum, Clone, Debug, Default, Display)] +#[derive(ValueEnum, Clone, Debug, Default)] pub enum CacheMode { Off, Read, #[default] + ReadWrite, Write, } -#[derive(ValueEnum, Clone, Debug, Default, Display)] +impl Display for CacheMode { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + write!( + f, + "{}", + match self { + CacheMode::Off => "off", + CacheMode::Read => "read", + CacheMode::ReadWrite => "read-write", + CacheMode::Write => "write", + } + )?; + + Ok(()) + } +} + +#[derive(ValueEnum, Clone, Debug, Default)] pub enum LogLevel { Off, Error, @@ -21,9 +39,26 @@ pub enum LogLevel { Trace, } -#[derive( - ValueEnum, Clone, Copy, Debug, Deserialize, Display, Default, Eq, PartialEq, Serialize, -)] +impl Display for LogLevel { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + write!( + f, + "{}", + match self { + LogLevel::Off => "off", + LogLevel::Error => "error", + LogLevel::Warn => "warn", + LogLevel::Info => "info", + LogLevel::Debug => "debug", + LogLevel::Trace => "trace", + } + )?; + + Ok(()) + } +} + +#[derive(ValueEnum, Clone, Copy, Debug, Deserialize, Default, Eq, PartialEq, Serialize)] #[serde(rename_all = "lowercase")] pub enum TouchedStatus { Added, @@ -35,3 +70,23 @@ pub enum TouchedStatus { Unstaged, Untracked, } + +impl Display for TouchedStatus { + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { + write!( + f, + "{}", + match self { + TouchedStatus::Added => "added", + TouchedStatus::All => "all", + TouchedStatus::Deleted => "deleted", + TouchedStatus::Modified => "modified", + TouchedStatus::Staged => "staged", + TouchedStatus::Unstaged => "unstaged", + TouchedStatus::Untracked => "untracked", + } + )?; + + Ok(()) + } +} diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index 4ee6379fe82..4f875fc8d66 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -52,14 +52,14 @@ pub async fn run_cli() { // Setup logging if env::var("MOON_LOG").is_err() { - env::set_var("MOON_LOG", args.log.to_string().to_lowercase()); + env::set_var("MOON_LOG", args.log.to_string()); } Logger::init(map_log_level(args.log), args.log_file); // Setup caching if env::var("MOON_CACHE").is_err() { - env::set_var("MOON_CACHE", args.cache.to_string().to_lowercase()); + env::set_var("MOON_CACHE", args.cache.to_string()); } // Match and run subcommand @@ -79,12 +79,18 @@ pub async fn run_cli() { }) .await } - Commands::Check { ids, report, all } => { + Commands::Check { + ids, + all, + report, + update_cache, + } => { check( ids, CheckOptions { - report: *report, all: *all, + report: *report, + update_cache: *update_cache, }, ) .await @@ -195,6 +201,7 @@ pub async fn run_cli() { targets, affected, dependents, + update_cache, status, passthrough, profile, @@ -210,6 +217,7 @@ pub async fn run_cli() { passthrough: passthrough.clone(), profile: profile.clone(), report: *report, + update_cache: *update_cache, upstream: *upstream, }, ) diff --git a/crates/cli/tests/check_test.rs b/crates/cli/tests/check_test.rs index c93d818aa41..a9a15579cce 100644 --- a/crates/cli/tests/check_test.rs +++ b/crates/cli/tests/check_test.rs @@ -1,16 +1,34 @@ use moon_test_utils::{ - create_sandbox_with_config, get_cases_fixture_configs, predicates::prelude::*, + create_sandbox_with_config, get_cases_fixture_configs, predicates::prelude::*, Sandbox, }; -#[test] -fn runs_tasks_in_project() { +fn cases_sandbox() -> Sandbox { let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let sandbox = create_sandbox_with_config( + + create_sandbox_with_config( "cases", Some(&workspace_config), Some(&toolchain_config), Some(&projects_config), - ); + ) +} + +#[test] +fn forces_cache_to_write_only() { + let sandbox = cases_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("check").arg("base").arg("--updateCache"); + }); + + let output = assert.output(); + + assert!(!predicate::str::contains("cached").eval(&output)); +} + +#[test] +fn runs_tasks_in_project() { + let sandbox = cases_sandbox(); let assert = sandbox.run_moon(|cmd| { cmd.arg("check").arg("base"); @@ -26,13 +44,7 @@ fn runs_tasks_in_project() { #[test] fn runs_tasks_in_project_using_cwd() { - let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let sandbox = create_sandbox_with_config( - "cases", - Some(&workspace_config), - Some(&toolchain_config), - Some(&projects_config), - ); + let sandbox = cases_sandbox(); let cwd = sandbox.path().join("base"); let assert = sandbox.run_moon(|cmd| { @@ -49,13 +61,7 @@ fn runs_tasks_in_project_using_cwd() { #[test] fn runs_tasks_from_multiple_project() { - let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let sandbox = create_sandbox_with_config( - "cases", - Some(&workspace_config), - Some(&toolchain_config), - Some(&projects_config), - ); + let sandbox = cases_sandbox(); let assert = sandbox.run_moon(|cmd| { cmd.arg("check").arg("base").arg("noop"); @@ -75,13 +81,7 @@ fn runs_tasks_from_multiple_project() { #[test] fn runs_for_all_projects_even_when_not_in_root_dir() { - let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let sandbox = create_sandbox_with_config( - "cases", - Some(&workspace_config), - Some(&toolchain_config), - Some(&projects_config), - ); + let sandbox = cases_sandbox(); let cwd = sandbox.path().join("base"); @@ -96,13 +96,7 @@ fn runs_for_all_projects_even_when_not_in_root_dir() { #[test] fn runs_on_all_projects_from_root_directory() { - let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let sandbox = create_sandbox_with_config( - "cases", - Some(&workspace_config), - Some(&toolchain_config), - Some(&projects_config), - ); + let sandbox = cases_sandbox(); let assert = sandbox.run_moon(|cmd| { cmd.arg("check").arg("--all"); @@ -118,13 +112,7 @@ mod reports { #[test] fn does_not_create_a_report_by_default() { - let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let sandbox = create_sandbox_with_config( - "cases", - Some(&workspace_config), - Some(&toolchain_config), - Some(&projects_config), - ); + let sandbox = cases_sandbox(); sandbox.run_moon(|cmd| { cmd.arg("check").arg("base"); @@ -135,13 +123,7 @@ mod reports { #[test] fn creates_report_when_option_passed() { - let (workspace_config, toolchain_config, projects_config) = get_cases_fixture_configs(); - let sandbox = create_sandbox_with_config( - "cases", - Some(&workspace_config), - Some(&toolchain_config), - Some(&projects_config), - ); + let sandbox = cases_sandbox(); sandbox.run_moon(|cmd| { cmd.arg("check").arg("base").arg("--report"); diff --git a/crates/cli/tests/run_node_test.rs b/crates/cli/tests/run_node_test.rs index 195ed318f70..f43280d3aef 100644 --- a/crates/cli/tests/run_node_test.rs +++ b/crates/cli/tests/run_node_test.rs @@ -273,6 +273,17 @@ fn inherits_moon_env_vars() { assert_snapshot!(assert.output()); } +#[test] +fn forces_cache_to_write_only() { + let sandbox = node_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("node:envVarsMoon").arg("--updateCache"); + }); + + assert!(predicate::str::contains("MOON_CACHE=write").eval(&assert.output())); +} + #[test] fn runs_from_project_root() { let sandbox = node_sandbox(); diff --git a/crates/cli/tests/run_system_test.rs b/crates/cli/tests/run_system_test.rs index a52ebedafc1..f03761a0216 100644 --- a/crates/cli/tests/run_system_test.rs +++ b/crates/cli/tests/run_system_test.rs @@ -127,6 +127,19 @@ mod unix { assert_snapshot!(assert.output()); } + #[test] + fn forces_cache_to_write_only() { + let sandbox = system_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("unix:envVarsMoon").arg("--updateCache"); + }); + + let output = assert.output(); + + assert!(predicate::str::contains("MOON_CACHE=write").eval(&output)); + } + #[test] fn runs_from_project_root() { let sandbox = system_sandbox(); @@ -324,7 +337,7 @@ mod unix { } #[cfg(windows)] -mod system_windows { +mod windows { use super::*; #[test] @@ -403,6 +416,21 @@ mod system_windows { assert_snapshot!(assert.output()); } + #[test] + fn forces_cache_to_write_only() { + let sandbox = system_sandbox(); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run") + .arg("windows:envVarsMoon") + .arg("--updateCache"); + }); + + let output = assert.output(); + + assert!(predicate::str::contains("MOON_CACHE=write").eval(&output)); + } + #[test] fn runs_from_project_root() { let sandbox = system_sandbox(); diff --git a/crates/cli/tests/run_test.rs b/crates/cli/tests/run_test.rs index ada1acca997..4e079d7d55c 100644 --- a/crates/cli/tests/run_test.rs +++ b/crates/cli/tests/run_test.rs @@ -598,6 +598,28 @@ mod outputs { assert!(dir.join("stderr.log").exists()); } + #[test] + fn can_bypass_cache() { + let sandbox = cases_sandbox(); + sandbox.enable_git(); + + sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFixed"); + }); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFixed"); + }); + + assert!(predicate::str::contains("cached from previous run").eval(&assert.output())); + + let assert = sandbox.run_moon(|cmd| { + cmd.arg("run").arg("outputs:generateFixed").arg("-u"); + }); + + assert!(!predicate::str::contains("cached from previous run").eval(&assert.output())); + } + mod hydration { use super::*; use moon_test_utils::pretty_assertions::assert_eq; diff --git a/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap b/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap index 8dd895d1609..8340716e9c6 100644 --- a/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap +++ b/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap @@ -1,11 +1,11 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 216 -expression: "get_path_safe_output(&assert, fixture.path())" +assertion_line: 273 +expression: assert.output() --- ▪▪▪▪ npm install ▪▪▪▪ node:envVarsMoon -MOON_CACHE=write +MOON_CACHE=read-write MOON_CACHE_DIR=/.moon/cache MOON_LOG=trace MOON_PROJECT_ID=node diff --git a/crates/cli/tests/snapshots/run_system_test__unix__inherits_moon_env_vars.snap b/crates/cli/tests/snapshots/run_system_test__unix__inherits_moon_env_vars.snap index 4c7bcf68335..f0c6c02f8cb 100644 --- a/crates/cli/tests/snapshots/run_system_test__unix__inherits_moon_env_vars.snap +++ b/crates/cli/tests/snapshots/run_system_test__unix__inherits_moon_env_vars.snap @@ -4,7 +4,7 @@ assertion_line: 114 expression: "get_path_safe_output(&assert, fixture.path())" --- ▪▪▪▪ unix:envVarsMoon -MOON_CACHE=write +MOON_CACHE=read-write MOON_CACHE_DIR=/.moon/cache MOON_LOG=trace MOON_PROJECT_ID=unix diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__creates_run_state_cache.snap b/crates/cli/tests/snapshots/run_system_test__windows__caching__creates_run_state_cache.snap similarity index 88% rename from crates/cli/tests/snapshots/run_system_test__system_windows__caching__creates_run_state_cache.snap rename to crates/cli/tests/snapshots/run_system_test__windows__caching__creates_run_state_cache.snap index d5751b8d726..f0a48657f71 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__creates_run_state_cache.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__caching__creates_run_state_cache.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: "fs::read_to_string(fixture.path().join(format!(\".moon/cache/hashes/{}.json\",\n state.hash))).unwrap()" +expression: "fs::read_to_string(sandbox.path().join(format!(\".moon/cache/hashes/{}.json\",\n state.hash))).unwrap()" --- [ { @@ -16,7 +16,7 @@ expression: "fs::read_to_string(fixture.path().join(format!(\".moon/cache/hashes "windows/cwd.bat": "f97d5bfb654f63a59a4158e4969707c8e1c29ece", "windows/echo.bat": "cf9137992257ec89c223a5d866458a94a361e0fd", "windows/envVars.bat": "af068672f8e5f2ca1aa1b5fec93df8fb253bdd0c", - "windows/envVarsMoon.bat": "f378867c19344c38e6587248c9a6b1643b49bbca", + "windows/envVarsMoon.bat": "87021bcea5907ae2c0e616fa963f0a35e074785d", "windows/exitNonZero.bat": "8204ab8df26ed8af772def3d3701a0df93d81ebe", "windows/exitZero.bat": "0cc40242e00650f6cf05f8c523f5e9afb268fb1f", "windows/moon.yml": "4ed9b777bf245c05de077d64f4e82812d86c6dcc", diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__uses_cache_on_subsequent_runs-2.snap b/crates/cli/tests/snapshots/run_system_test__windows__caching__uses_cache_on_subsequent_runs-2.snap similarity index 79% rename from crates/cli/tests/snapshots/run_system_test__system_windows__caching__uses_cache_on_subsequent_runs-2.snap rename to crates/cli/tests/snapshots/run_system_test__windows__caching__uses_cache_on_subsequent_runs-2.snap index a4a0ff0924f..05e1b7f49de 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__uses_cache_on_subsequent_runs-2.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__caching__uses_cache_on_subsequent_runs-2.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: get_assert_output(&assert) +expression: assert.output() --- ▪▪▪▪ windows:outputs ▪▪▪▪ windows:outputs (100ms) diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__uses_cache_on_subsequent_runs.snap b/crates/cli/tests/snapshots/run_system_test__windows__caching__uses_cache_on_subsequent_runs.snap similarity index 79% rename from crates/cli/tests/snapshots/run_system_test__system_windows__caching__uses_cache_on_subsequent_runs.snap rename to crates/cli/tests/snapshots/run_system_test__windows__caching__uses_cache_on_subsequent_runs.snap index a4a0ff0924f..05e1b7f49de 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__caching__uses_cache_on_subsequent_runs.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__caching__uses_cache_on_subsequent_runs.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: get_assert_output(&assert) +expression: assert.output() --- ▪▪▪▪ windows:outputs ▪▪▪▪ windows:outputs (100ms) diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__handles_process_exit_nonzero.snap b/crates/cli/tests/snapshots/run_system_test__windows__handles_process_exit_nonzero.snap similarity index 83% rename from crates/cli/tests/snapshots/run_system_test__system_windows__handles_process_exit_nonzero.snap rename to crates/cli/tests/snapshots/run_system_test__windows__handles_process_exit_nonzero.snap index b4cbd8d0fd9..747ebb9c2bd 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__handles_process_exit_nonzero.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__handles_process_exit_nonzero.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: get_assert_output(&assert) +expression: assert.output() --- ▪▪▪▪ windows:exitNonZero stdout diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__handles_process_exit_zero.snap b/crates/cli/tests/snapshots/run_system_test__windows__handles_process_exit_zero.snap similarity index 77% rename from crates/cli/tests/snapshots/run_system_test__system_windows__handles_process_exit_zero.snap rename to crates/cli/tests/snapshots/run_system_test__windows__handles_process_exit_zero.snap index 94b88b9ca96..3dac7384512 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__handles_process_exit_zero.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__handles_process_exit_zero.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: get_assert_output(&assert) +expression: assert.output() --- ▪▪▪▪ windows:exitZero stdout @@ -9,6 +9,6 @@ stdout Tasks: 1 completed Time: 100ms -stderr +stderr diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__inherits_moon_env_vars.snap b/crates/cli/tests/snapshots/run_system_test__windows__inherits_moon_env_vars.snap similarity index 78% rename from crates/cli/tests/snapshots/run_system_test__system_windows__inherits_moon_env_vars.snap rename to crates/cli/tests/snapshots/run_system_test__windows__inherits_moon_env_vars.snap index 81aa5457bfa..47042592240 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__inherits_moon_env_vars.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__inherits_moon_env_vars.snap @@ -1,8 +1,9 @@ --- source: crates/cli/tests/run_system_test.rs -expression: "get_path_safe_output(&assert, fixture.path())" +expression: assert.output() --- ▪▪▪▪ windows:envVarsMoon +MOON_CACHE=read-write MOON_TARGET=windows:envVarsMoon MOON_PROJECT_ID=windows ▪▪▪▪ windows:envVarsMoon (100ms) diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__passes_args_through.snap b/crates/cli/tests/snapshots/run_system_test__windows__passes_args_through.snap similarity index 85% rename from crates/cli/tests/snapshots/run_system_test__system_windows__passes_args_through.snap rename to crates/cli/tests/snapshots/run_system_test__windows__passes_args_through.snap index ba9e2761bf6..cfa9bdba83b 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__passes_args_through.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__passes_args_through.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: get_assert_output(&assert) +expression: assert.output() --- ▪▪▪▪ windows:passthroughArgs -aBc --opt value --optCamel=value foo "'bar baz'" --opt-kebab 123 diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__retries_on_failure_till_count.snap b/crates/cli/tests/snapshots/run_system_test__windows__retries_on_failure_till_count.snap similarity index 92% rename from crates/cli/tests/snapshots/run_system_test__system_windows__retries_on_failure_till_count.snap rename to crates/cli/tests/snapshots/run_system_test__windows__retries_on_failure_till_count.snap index ade944724eb..1bce7c8c43d 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__retries_on_failure_till_count.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__retries_on_failure_till_count.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: get_assert_output(&assert) +expression: assert.output() --- ▪▪▪▪ windows:retryCount stdout diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__runs_bat_script.snap b/crates/cli/tests/snapshots/run_system_test__windows__runs_bat_script.snap similarity index 69% rename from crates/cli/tests/snapshots/run_system_test__system_windows__runs_bat_script.snap rename to crates/cli/tests/snapshots/run_system_test__windows__runs_bat_script.snap index 3289823250b..e22231d4b7d 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__runs_bat_script.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__runs_bat_script.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: "get_path_safe_output(&assert, fixture.path())" +expression: assert.output() --- ▪▪▪▪ windows:bat stdout @@ -9,6 +9,6 @@ stdout Tasks: 1 completed Time: 100ms -stderr +stderr diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__runs_from_project_root.snap b/crates/cli/tests/snapshots/run_system_test__windows__runs_from_project_root.snap similarity index 75% rename from crates/cli/tests/snapshots/run_system_test__system_windows__runs_from_project_root.snap rename to crates/cli/tests/snapshots/run_system_test__windows__runs_from_project_root.snap index fe7879a259b..14b17809b55 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__runs_from_project_root.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__runs_from_project_root.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: "get_path_safe_output(&assert, fixture.path())" +expression: assert.output() --- ▪▪▪▪ windows:runFromProject \windows diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__runs_from_workspace_root.snap b/crates/cli/tests/snapshots/run_system_test__windows__runs_from_workspace_root.snap similarity index 75% rename from crates/cli/tests/snapshots/run_system_test__system_windows__runs_from_workspace_root.snap rename to crates/cli/tests/snapshots/run_system_test__windows__runs_from_workspace_root.snap index da22a84035a..ffb54d06b24 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__runs_from_workspace_root.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__runs_from_workspace_root.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: "get_path_safe_output(&assert, fixture.path())" +expression: assert.output() --- ▪▪▪▪ windows:runFromWorkspace diff --git a/crates/cli/tests/snapshots/run_system_test__system_windows__sets_env_vars.snap b/crates/cli/tests/snapshots/run_system_test__windows__sets_env_vars.snap similarity index 83% rename from crates/cli/tests/snapshots/run_system_test__system_windows__sets_env_vars.snap rename to crates/cli/tests/snapshots/run_system_test__windows__sets_env_vars.snap index bcbf9d92490..406caa976fe 100644 --- a/crates/cli/tests/snapshots/run_system_test__system_windows__sets_env_vars.snap +++ b/crates/cli/tests/snapshots/run_system_test__windows__sets_env_vars.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_system_test.rs -expression: get_assert_output(&assert) +expression: assert.output() --- ▪▪▪▪ windows:envVars MOON_FOO=abc diff --git a/crates/core/cache/src/engine.rs b/crates/core/cache/src/engine.rs index b4b2af751f9..8cd1f65574e 100644 --- a/crates/core/cache/src/engine.rs +++ b/crates/core/cache/src/engine.rs @@ -1,6 +1,7 @@ -use crate::helpers::{is_writable, LOG_TARGET}; +use crate::helpers::LOG_TARGET; use crate::items::{DependenciesState, ProjectsState, RunTargetState, ToolState}; use crate::runfiles::Runfile; +use crate::{get_cache_mode, CacheMode}; use moon_constants::CONFIG_DIRNAME; use moon_error::MoonError; use moon_logger::{color, debug, trace}; @@ -142,17 +143,15 @@ impl CacheEngine { where T: ?Sized + Serialize, { - if is_writable() { - let path = self.get_hash_manifest_path(hash); + let path = self.get_hash_manifest_path(hash); - trace!( - target: LOG_TARGET, - "Writing hash manifest {}", - color::path(&path) - ); + trace!( + target: LOG_TARGET, + "Writing hash manifest {}", + color::path(&path) + ); - json::write(&path, &hasher, true)?; - } + json::write(&path, &hasher, true)?; Ok(()) } @@ -183,6 +182,10 @@ impl CacheEngine { self.hashes_dir.join(format!("{}.json", hash)) } + pub fn get_mode(&self) -> CacheMode { + get_cache_mode() + } + pub fn get_target_dir>(&self, target_id: T) -> PathBuf { self.states_dir.join(target_id.as_ref().replace(':', "/")) } diff --git a/crates/core/cache/src/helpers.rs b/crates/core/cache/src/helpers.rs index ca959d73fdf..62e8fe80d14 100644 --- a/crates/core/cache/src/helpers.rs +++ b/crates/core/cache/src/helpers.rs @@ -5,33 +5,62 @@ pub const LOG_TARGET: &str = "moon:cache"; static mut LOGGED_WARNING: bool = false; -pub fn get_cache_env_var() -> String { - if let Ok(var) = env::var("MOON_CACHE") { - if var == "off" || var == "read" || var == "write" { - return var; - } +pub enum CacheMode { + Off, + Read, + ReadWrite, + Write, +} + +impl From for CacheMode { + fn from(value: String) -> Self { + match value.to_lowercase().as_str() { + "off" => CacheMode::Off, + "read" => CacheMode::Read, + "read-write" => CacheMode::ReadWrite, + "write" => CacheMode::Write, + val => { + // We only want to show this once, not everytime the function is called + unsafe { + if !LOGGED_WARNING { + LOGGED_WARNING = true; - // We only want to show this once, not everytime the function is called - unsafe { - if !LOGGED_WARNING { - LOGGED_WARNING = true; + warn!( + target: LOG_TARGET, + "Unknown MOON_CACHE environment variable value \"{}\", falling back to read-write mode", + val + ); + } + } - warn!( - target: LOG_TARGET, - "Unknown MOON_CACHE environment variable value \"{}\", falling back to write mode", - var - ); + CacheMode::ReadWrite } } } - - String::from("write") } -pub fn is_readable() -> bool { - get_cache_env_var() != "off" +impl CacheMode { + pub fn is_readable(&self) -> bool { + matches!(&self, CacheMode::Read | CacheMode::ReadWrite) + } + + pub fn is_read_only(&self) -> bool { + matches!(&self, CacheMode::Read) + } + + pub fn is_writable(&self) -> bool { + matches!(&self, CacheMode::Write | CacheMode::ReadWrite) + } + + pub fn is_write_only(&self) -> bool { + matches!(&self, CacheMode::Write) + } } -pub fn is_writable() -> bool { - get_cache_env_var() == "write" +pub fn get_cache_mode() -> CacheMode { + if let Ok(var) = env::var("MOON_CACHE") { + return CacheMode::from(var); + } + + CacheMode::ReadWrite } diff --git a/crates/core/cache/src/item.rs b/crates/core/cache/src/item.rs index c15813e8b47..8ebebff6336 100644 --- a/crates/core/cache/src/item.rs +++ b/crates/core/cache/src/item.rs @@ -10,7 +10,7 @@ macro_rules! cache_item { fs::create_dir_all(parent)?; } - if is_readable() { + if get_cache_mode().is_readable() { if path.exists() { // If stale, treat as a cache miss if stale_ms > 0 @@ -49,7 +49,7 @@ macro_rules! cache_item { pub fn save(&self) -> Result<(), MoonError> { let log_target = "moon:cache:item"; - if is_writable() { + if get_cache_mode().is_writable() { trace!( target: log_target, "Writing cache {}", diff --git a/crates/core/cache/src/items/dependencies_state.rs b/crates/core/cache/src/items/dependencies_state.rs index 892389b6bfe..87bff12990c 100644 --- a/crates/core/cache/src/items/dependencies_state.rs +++ b/crates/core/cache/src/items/dependencies_state.rs @@ -1,5 +1,5 @@ use crate::cache_item; -use crate::helpers::{is_readable, is_writable}; +use crate::helpers::get_cache_mode; use moon_error::MoonError; use moon_logger::{color, trace}; use moon_utils::{fs, json, time}; diff --git a/crates/core/cache/src/items/projects_state.rs b/crates/core/cache/src/items/projects_state.rs index 75252beaff4..937642b080d 100644 --- a/crates/core/cache/src/items/projects_state.rs +++ b/crates/core/cache/src/items/projects_state.rs @@ -1,5 +1,5 @@ use crate::cache_item; -use crate::helpers::{is_readable, is_writable}; +use crate::helpers::get_cache_mode; use moon_error::MoonError; use moon_logger::{color, trace}; use moon_utils::{fs, json, time}; diff --git a/crates/core/cache/src/items/run_target_state.rs b/crates/core/cache/src/items/run_target_state.rs index 5f6cfff1f97..04c85feac3f 100644 --- a/crates/core/cache/src/items/run_target_state.rs +++ b/crates/core/cache/src/items/run_target_state.rs @@ -1,5 +1,5 @@ use crate::cache_item; -use crate::helpers::{is_readable, is_writable}; +use crate::helpers::get_cache_mode; use moon_archive::{untar_with_diff, TarArchiver, TreeDiffer}; use moon_error::MoonError; use moon_logger::{color, trace}; @@ -31,7 +31,7 @@ impl RunTargetState { input_root: &Path, outputs: &[String], ) -> Result { - if is_writable() && !archive_file.exists() { + if get_cache_mode().is_writable() && !archive_file.exists() { let mut tar = TarArchiver::new(input_root, archive_file); // Outputs are relative from project root (the input) @@ -66,7 +66,7 @@ impl RunTargetState { project_root: &Path, outputs: &[String], ) -> Result { - if is_readable() && archive_file.exists() { + if get_cache_mode().is_readable() && archive_file.exists() { let mut differ = TreeDiffer::load(project_root, outputs)?; untar_with_diff(&mut differ, archive_file, project_root, None) diff --git a/crates/core/cache/src/items/tool_state.rs b/crates/core/cache/src/items/tool_state.rs index 1837a19e147..32fbb9e858e 100644 --- a/crates/core/cache/src/items/tool_state.rs +++ b/crates/core/cache/src/items/tool_state.rs @@ -1,5 +1,5 @@ use crate::cache_item; -use crate::helpers::{is_readable, is_writable}; +use crate::helpers::get_cache_mode; use moon_error::MoonError; use moon_logger::{color, trace}; use moon_utils::{fs, json, time}; diff --git a/crates/core/cache/tests/engine_test.rs b/crates/core/cache/tests/engine_test.rs index 4da752266d3..b8997b41fcb 100644 --- a/crates/core/cache/tests/engine_test.rs +++ b/crates/core/cache/tests/engine_test.rs @@ -129,6 +129,31 @@ mod cache_run_target_state { dir.close().unwrap(); } + #[test] + #[serial] + fn loads_cache_if_it_exists_and_cache_is_readwrite() { + let dir = create_temp_dir(); + + dir.child(".moon/cache/states/foo/bar/lastRun.json") + .write_str(r#"{"exitCode":123,"hash":"","lastRunTime":0,"stderr":"","stdout":"","target":"foo:bar"}"#) + .unwrap(); + + let cache = CacheEngine::load(dir.path()).unwrap(); + let item = run_with_env("read", || cache.cache_run_target_state("foo:bar")).unwrap(); + + assert_eq!( + item, + RunTargetState { + exit_code: 123, + target: String::from("foo:bar"), + path: dir.path().join(".moon/cache/states/foo/bar/lastRun.json"), + ..RunTargetState::default() + } + ); + + dir.close().unwrap(); + } + #[test] #[serial] fn doesnt_load_if_it_exists_but_cache_is_off() { @@ -153,6 +178,30 @@ mod cache_run_target_state { dir.close().unwrap(); } + #[test] + #[serial] + fn doesnt_load_if_it_exists_but_cache_is_writeonly() { + let dir = create_temp_dir(); + + dir.child(".moon/cache/states/foo/bar/lastRun.json") + .write_str(r#"{"exitCode":123,"hash":"","lastRunTime":0,"stderr":"","stdout":"","target":"foo:bar"}"#) + .unwrap(); + + let cache = CacheEngine::load(dir.path()).unwrap(); + let item = run_with_env("write", || cache.cache_run_target_state("foo:bar")).unwrap(); + + assert_eq!( + item, + RunTargetState { + target: String::from("foo:bar"), + path: dir.path().join(".moon/cache/states/foo/bar/lastRun.json"), + ..RunTargetState::default() + } + ); + + dir.close().unwrap(); + } + #[test] #[serial] fn saves_to_cache() { @@ -172,6 +221,25 @@ mod cache_run_target_state { dir.close().unwrap(); } + #[test] + #[serial] + fn saves_to_cache_if_writeonly() { + let dir = create_temp_dir(); + let cache = CacheEngine::load(dir.path()).unwrap(); + let mut item = cache.cache_run_target_state("foo:bar").unwrap(); + + item.exit_code = 123; + + run_with_env("write", || item.save()).unwrap(); + + assert_eq!( + fs::read_to_string(item.path).unwrap(), + r#"{"exitCode":123,"hash":"","lastRunTime":0,"target":"foo:bar"}"# + ); + + dir.close().unwrap(); + } + #[test] #[serial] fn doesnt_save_if_cache_off() { diff --git a/crates/core/runner/src/subscribers/local_cache.rs b/crates/core/runner/src/subscribers/local_cache.rs index e63f1e0fc12..d5ca8473829 100644 --- a/crates/core/runner/src/subscribers/local_cache.rs +++ b/crates/core/runner/src/subscribers/local_cache.rs @@ -1,4 +1,4 @@ -use moon_cache::{is_readable, is_writable}; +use moon_cache::get_cache_mode; use moon_emitter::{Event, EventFlow, Subscriber}; use moon_error::MoonError; use moon_utils::path; @@ -30,7 +30,9 @@ impl Subscriber for LocalCacheSubscriber { // Check to see if a build with the provided hash has been cached locally. // We only check for the archive, as the manifest is purely for local debugging! Event::TargetOutputCacheCheck { hash, .. } => { - if is_readable() && workspace.cache.get_hash_archive_path(hash).exists() { + if get_cache_mode().is_readable() + && workspace.cache.get_hash_archive_path(hash).exists() + { return Ok(EventFlow::Return("local-cache".into())); } } @@ -45,9 +47,7 @@ impl Subscriber for LocalCacheSubscriber { } => { let archive_path = workspace.cache.get_hash_archive_path(hash); - if is_writable() - && cache.archive_outputs(&archive_path, &project.root, &task.outputs)? - { + if cache.archive_outputs(&archive_path, &project.root, &task.outputs)? { return Ok(EventFlow::Return(path::to_string(archive_path)?)); } } @@ -62,9 +62,7 @@ impl Subscriber for LocalCacheSubscriber { } => { let archive_path = workspace.cache.get_hash_archive_path(hash); - if is_readable() - && cache.hydrate_outputs(&archive_path, &project.root, &task.outputs)? - { + if cache.hydrate_outputs(&archive_path, &project.root, &task.outputs)? { return Ok(EventFlow::Return(path::to_string(archive_path)?)); } } diff --git a/crates/core/runner/src/subscribers/moonbase_cache.rs b/crates/core/runner/src/subscribers/moonbase_cache.rs index 92e33f68f4b..819ffd4a9fa 100644 --- a/crates/core/runner/src/subscribers/moonbase_cache.rs +++ b/crates/core/runner/src/subscribers/moonbase_cache.rs @@ -1,4 +1,4 @@ -use moon_cache::{is_readable, is_writable}; +use moon_cache::get_cache_mode; use moon_emitter::{Event, EventFlow, Subscriber}; use moon_error::MoonError; use moon_logger::warn; @@ -43,7 +43,7 @@ impl Subscriber for MoonbaseCacheSubscriber { match event { // Check if archive exists in moonbase (the remote) by querying the artifacts endpoint. Event::TargetOutputCacheCheck { hash, .. } => { - if is_readable() { + if get_cache_mode().is_readable() { match moonbase.get_artifact(hash).await { Ok(Some(artifact)) => { self.hash_exists.insert(artifact.hash); @@ -70,7 +70,7 @@ impl Subscriber for MoonbaseCacheSubscriber { target, .. } => { - if is_writable() && archive_path.exists() { + if get_cache_mode().is_writable() && archive_path.exists() { let auth_token = moonbase.auth_token.to_owned(); let hash = (*hash).to_owned(); let target = target.id.to_owned(); @@ -92,7 +92,7 @@ impl Subscriber for MoonbaseCacheSubscriber { // This runs *before* the local cache. So if the download is successful, abort // the event flow, otherwise continue and let local cache attempt to hydrate. Event::TargetOutputHydrating { hash, .. } => { - if is_readable() && self.hash_exists.contains(*hash) { + if get_cache_mode().is_readable() && self.hash_exists.contains(*hash) { let archive_file = workspace.cache.get_hash_archive_path(hash); if let Err(error) = moonbase.download_artifact(hash, &archive_file).await { diff --git a/crates/node/platform/src/actions/install_deps.rs b/crates/node/platform/src/actions/install_deps.rs index 2cc1cbdc40f..03feae316de 100644 --- a/crates/node/platform/src/actions/install_deps.rs +++ b/crates/node/platform/src/actions/install_deps.rs @@ -104,10 +104,13 @@ pub async fn install_deps( ) -> Result { let workspace = workspace.read().await; let context = context.read().await; - let node = workspace.toolchain.node.get_for_runtime(runtime)?; - let pm = node.get_package_manager(); - let lock_filename = pm.get_lock_filename(); - let manifest_filename = pm.get_manifest_filename(); + + // When cache is write only, avoid install as user is typically force updating cache + if workspace.cache.get_mode().is_write_only() { + debug!(target: LOG_TARGET, "Force updating cache, skipping install",); + + return Ok(ActionStatus::Skipped); + } // When running against affected files, avoid install as it interrupts the workflow if context.affected_only { @@ -119,6 +122,11 @@ pub async fn install_deps( return Ok(ActionStatus::Skipped); } + let node = workspace.toolchain.node.get_for_runtime(runtime)?; + let pm = node.get_package_manager(); + let lock_filename = pm.get_lock_filename(); + let manifest_filename = pm.get_manifest_filename(); + // Determine the working directory and whether lockfiles and manifests have been modified let working_dir; let has_modified_files; diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 7191760ba0a..31d0b598653 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -14,6 +14,10 @@ ##### Runner +- Added `--updateCache` (`-u`) to `moon check` and `moon run` that force updates the cache and + bypasses any existing cache. +- Added a new cache level, `read-write`, that can be passed to `--cache` or `MOON_CACHE`. This is + now the default level, while `write` is a write-only level. - Added `args` and `env` as valid values for the `options.affectedFiles` task option. ## 0.20.3 diff --git a/tests/fixtures/cases/outputs/moon.yml b/tests/fixtures/cases/outputs/moon.yml index 946fa358b65..e223ec3c93b 100644 --- a/tests/fixtures/cases/outputs/moon.yml +++ b/tests/fixtures/cases/outputs/moon.yml @@ -39,6 +39,13 @@ tasks: outputs: - 'lib/one.js' - 'esm' + generateFixed: + command: node + args: generate.js custom test.mjs + inputs: + - '*.js' + outputs: + - 'test.mjs' # Dependency hashing asDep: diff --git a/tests/fixtures/system/windows/envVarsMoon.bat b/tests/fixtures/system/windows/envVarsMoon.bat index f378867c193..87021bcea59 100644 --- a/tests/fixtures/system/windows/envVarsMoon.bat +++ b/tests/fixtures/system/windows/envVarsMoon.bat @@ -1,3 +1,4 @@ :: Looping over env vars isn't easy, so just check a few of them +echo MOON_CACHE=%MOON_CACHE% echo MOON_TARGET=%MOON_TARGET% echo MOON_PROJECT_ID=%MOON_PROJECT_ID% \ No newline at end of file diff --git a/website/blog/2022-12-16_v0.21.mdx b/website/blog/2022-12-16_v0.21.mdx new file mode 100644 index 00000000000..7b81c4b1b83 --- /dev/null +++ b/website/blog/2022-12-16_v0.21.mdx @@ -0,0 +1,32 @@ +--- +slug: v0.21 +title: moon v0.21 +authors: [milesj] +tags: [runner] +# image: ./img/v0.20.png +--- + +TODO + + + +## New `--updateCache` command line option + +Our caching layer is pretty powerful, as it helps to avoid tasks from running unnecessarily. +However, there are situations where you need to refresh or force update the cache outside of the +[`inputs`](../docs/config/project#inputs) list. This is currently achieved with the +[`--cache`](../docs/commands/overview#caching) option, like `moon --cache off run ...`, but this is +non-ideal for a few reasons: + +1. It disables _all caching_, which means dependency installs/dedupes will continually be ran. This + adds a lot of unwanted overhead. +2. It requires you to backspace in the terminal to add the option _before_ `run` or `check`, as it's + a global option. A little tedious but important for experience. + +So to work around these limitations, we've added a new `--updateCache` (or `-u`) to both +[`moon run`](../docs/commands/run) and [`moon check`](../docs/commands/run), which will bypass +reading any existing cache items, but will force update the cache base on the latest run. + +```shell +$ moon run app:build --updateCache +``` diff --git a/website/docs/commands/check.mdx b/website/docs/commands/check.mdx index c35696e4b5b..8d476d6a350 100644 --- a/website/docs/commands/check.mdx +++ b/website/docs/commands/check.mdx @@ -20,6 +20,9 @@ $ moon check app # Check multiple projects by name $ moon check client server + +# Check ALL projects (may be costly) +$ moon check --all ``` ### Arguments @@ -29,5 +32,7 @@ $ moon check client server ### Options +- `--all` - Run check for all projects in the workspace. - `--report` - Generate a report of the ran actions. Report is written to `.moon/cache/runReport.json`. +- `-u`, `--updateCache` - Bypass cache and force update any existing items. diff --git a/website/docs/commands/overview.mdx b/website/docs/commands/overview.mdx index 1933e507382..832cb15bf24 100644 --- a/website/docs/commands/overview.mdx +++ b/website/docs/commands/overview.mdx @@ -25,7 +25,8 @@ option, or the `MOON_CACHE` environment variable, both of which accept one of th - `off` - Turn off caching entirely. Every task will run fresh, including dependency installs. - `read` - Read existing items from the cache, but do not write to them. -- `write` (default) - Read and write items to the cache. +- `read-write` (default) - Read and write items to the cache. +- `write` - Do not read existing cache items, but write new items to the cache. ```shell $ moon --cache off run app:build diff --git a/website/docs/commands/run.mdx b/website/docs/commands/run.mdx index 95210c54bda..c6bca0ee4c7 100644 --- a/website/docs/commands/run.mdx +++ b/website/docs/commands/run.mdx @@ -32,6 +32,7 @@ $ moon run :lint - Types: `cpu`, `heap` - `--report` - Generate a report of the ran actions. Report is written to `.moon/cache/runReport.json`. +- `-u`, `--updateCache` - Bypass cache and force update any existing items. #### Affected