From d5c452194f6741e0022c78b1284eabd63053ade3 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Thu, 3 Oct 2024 21:53:57 -0400 Subject: [PATCH] feat(core): enable db cache by default (#28048) ## Current Behavior DB cache is disabled by default ## Expected Behavior DB cache is enabled by default ## Related Issue(s) Fixes # --- docs/generated/devkit/NxJsonConfiguration.md | 18 +++++++------- docs/generated/devkit/Workspace.md | 26 ++++++++++---------- packages/nx/src/adapter/compat.ts | 2 +- packages/nx/src/config/nx-json.ts | 4 +-- packages/nx/src/native/cache/cache.rs | 11 +++++++-- packages/nx/src/native/cache/file_ops.rs | 19 +++++++++++++- packages/nx/src/tasks-runner/cache.ts | 3 ++- 7 files changed, 54 insertions(+), 29 deletions(-) diff --git a/docs/generated/devkit/NxJsonConfiguration.md b/docs/generated/devkit/NxJsonConfiguration.md index c7efdc6cdbd91..d3878933c6577 100644 --- a/docs/generated/devkit/NxJsonConfiguration.md +++ b/docs/generated/devkit/NxJsonConfiguration.md @@ -23,7 +23,6 @@ Nx.json configuration - [cli](../../devkit/documents/NxJsonConfiguration#cli): Object - [defaultBase](../../devkit/documents/NxJsonConfiguration#defaultbase): string - [defaultProject](../../devkit/documents/NxJsonConfiguration#defaultproject): string -- [enableDbCache](../../devkit/documents/NxJsonConfiguration#enabledbcache): boolean - [extends](../../devkit/documents/NxJsonConfiguration#extends): string - [generators](../../devkit/documents/NxJsonConfiguration#generators): Object - [implicitDependencies](../../devkit/documents/NxJsonConfiguration#implicitdependencies): ImplicitDependencyEntry @@ -43,6 +42,7 @@ Nx.json configuration - [tasksRunnerOptions](../../devkit/documents/NxJsonConfiguration#tasksrunneroptions): Object - [useDaemonProcess](../../devkit/documents/NxJsonConfiguration#usedaemonprocess): boolean - [useInferencePlugins](../../devkit/documents/NxJsonConfiguration#useinferenceplugins): boolean +- [useLegacyCache](../../devkit/documents/NxJsonConfiguration#uselegacycache): boolean - [workspaceLayout](../../devkit/documents/NxJsonConfiguration#workspacelayout): Object ## Properties @@ -99,14 +99,6 @@ will be used. Convenient for small workspaces with one main application. --- -### enableDbCache - -• `Optional` **enableDbCache**: `boolean` - -Enable the new experimental db based cache - ---- - ### extends • `Optional` **extends**: `string` @@ -297,6 +289,14 @@ Set this to false to disable adding inference plugins when generating new projec --- +### useLegacyCache + +• `Optional` **useLegacyCache**: `boolean` + +Use the legacy file system cache instead of the db cache + +--- + ### workspaceLayout • `Optional` **workspaceLayout**: `Object` diff --git a/docs/generated/devkit/Workspace.md b/docs/generated/devkit/Workspace.md index 78fbb58467305..6b69552ee8516 100644 --- a/docs/generated/devkit/Workspace.md +++ b/docs/generated/devkit/Workspace.md @@ -21,7 +21,6 @@ use ProjectsConfigurations or NxJsonConfiguration - [cli](../../devkit/documents/Workspace#cli): Object - [defaultBase](../../devkit/documents/Workspace#defaultbase): string - [defaultProject](../../devkit/documents/Workspace#defaultproject): string -- [enableDbCache](../../devkit/documents/Workspace#enabledbcache): boolean - [extends](../../devkit/documents/Workspace#extends): string - [generators](../../devkit/documents/Workspace#generators): Object - [implicitDependencies](../../devkit/documents/Workspace#implicitdependencies): ImplicitDependencyEntry @@ -42,6 +41,7 @@ use ProjectsConfigurations or NxJsonConfiguration - [tasksRunnerOptions](../../devkit/documents/Workspace#tasksrunneroptions): Object - [useDaemonProcess](../../devkit/documents/Workspace#usedaemonprocess): boolean - [useInferencePlugins](../../devkit/documents/Workspace#useinferenceplugins): boolean +- [useLegacyCache](../../devkit/documents/Workspace#uselegacycache): boolean - [version](../../devkit/documents/Workspace#version): number - [workspaceLayout](../../devkit/documents/Workspace#workspacelayout): Object @@ -119,18 +119,6 @@ will be used. Convenient for small workspaces with one main application. --- -### enableDbCache - -• `Optional` **enableDbCache**: `boolean` - -Enable the new experimental db based cache - -#### Inherited from - -[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[enableDbCache](../../devkit/documents/NxJsonConfiguration#enabledbcache) - ---- - ### extends • `Optional` **extends**: `string` @@ -409,6 +397,18 @@ Set this to false to disable adding inference plugins when generating new projec --- +### useLegacyCache + +• `Optional` **useLegacyCache**: `boolean` + +Use the legacy file system cache instead of the db cache + +#### Inherited from + +[NxJsonConfiguration](../../devkit/documents/NxJsonConfiguration).[useLegacyCache](../../devkit/documents/NxJsonConfiguration#uselegacycache) + +--- + ### version • **version**: `number` diff --git a/packages/nx/src/adapter/compat.ts b/packages/nx/src/adapter/compat.ts index f24befda7e90a..d5daa3c2d70ee 100644 --- a/packages/nx/src/adapter/compat.ts +++ b/packages/nx/src/adapter/compat.ts @@ -80,7 +80,7 @@ export const allowedWorkspaceExtensions = [ 'useInferencePlugins', 'neverConnectToCloud', 'sync', - 'enableDbCache', + 'useLegacyCache', ] as const; if (!patched) { diff --git a/packages/nx/src/config/nx-json.ts b/packages/nx/src/config/nx-json.ts index 65f789572e74c..9fd1b42b39754 100644 --- a/packages/nx/src/config/nx-json.ts +++ b/packages/nx/src/config/nx-json.ts @@ -517,9 +517,9 @@ export interface NxJsonConfiguration { sync?: NxSyncConfiguration; /** - * Enable the new experimental db based cache + * Use the legacy file system cache instead of the db cache */ - enableDbCache?: boolean; + useLegacyCache?: boolean; } export type PluginConfiguration = string | ExpandedPluginConfiguration; diff --git a/packages/nx/src/native/cache/cache.rs b/packages/nx/src/native/cache/cache.rs index e1236b741e255..866a9527faec2 100644 --- a/packages/nx/src/native/cache/cache.rs +++ b/packages/nx/src/native/cache/cache.rs @@ -136,12 +136,17 @@ impl NxCache { let task_dir = self.cache_path.join(&hash); // Remove the task directory + // + trace!("Removing task directory: {:?}", &task_dir); remove_items(&[&task_dir])?; // Create the task directory again + trace!("Creating task directory: {:?}", &task_dir); create_dir_all(&task_dir)?; // Write the terminal outputs into a file - write(self.get_task_outputs_path_internal(&hash), terminal_output)?; + let task_outputs_path: _ = self.get_task_outputs_path_internal(&hash); + trace!("Writing terminal outputs to: {:?}", &task_outputs_path); + write(task_outputs_path, terminal_output)?; // Expand the outputs let expanded_outputs = _expand_outputs(&self.workspace_root, outputs)?; @@ -151,10 +156,12 @@ impl NxCache { let p = self.workspace_root.join(expanded_output); if p.exists() { let cached_outputs_dir = task_dir.join(expanded_output); + trace!("Copying {:?} -> {:?}", &p, &cached_outputs_dir); _copy(p, cached_outputs_dir)?; } } + trace!("Recording to cache: {:?}", &hash); self.record_to_cache(hash, code)?; Ok(()) } @@ -269,7 +276,7 @@ impl NxCache { let entry = entry?; let is_dir = entry.file_type()?.is_dir(); - if (is_dir) { + if is_dir { if let Some(file_name) = entry.file_name().to_str() { if hash_regex.is_match(file_name) { return Ok(false); diff --git a/packages/nx/src/native/cache/file_ops.rs b/packages/nx/src/native/cache/file_ops.rs index 47e78a4dea458..189879f3aab87 100644 --- a/packages/nx/src/native/cache/file_ops.rs +++ b/packages/nx/src/native/cache/file_ops.rs @@ -2,6 +2,7 @@ use std::path::{Path, PathBuf}; use std::{fs, io}; use fs_extra::error::ErrorKind; +use tracing::trace; #[napi] pub fn remove(src: String) -> anyhow::Result<()> { @@ -17,7 +18,8 @@ pub fn copy(src: String, dest: String) -> anyhow::Result<()> { } pub fn _copy

(src: P, dest: P) -> anyhow::Result<()> where P: AsRef { - let dest: PathBuf = dest.as_ref().into(); + let dest: PathBuf = remove_trailing_single_dot(dest); + let dest_parent = dest.parent().unwrap_or(&dest); let src: PathBuf = src.as_ref().into(); @@ -37,6 +39,18 @@ pub fn _copy

(src: P, dest: P) -> anyhow::Result<()> where P: AsRef { Ok(()) } +fn remove_trailing_single_dot(path: impl AsRef) -> PathBuf { + let mut components = path.as_ref().components().collect::>(); + + if let Some(last_component) = components.last() { + if last_component.as_os_str() == "." { + components.pop(); + } + } + + components.iter().collect() +} + #[cfg(windows)] fn symlink, Q: AsRef>(original: P, link: Q) -> io::Result<()> { std::os::windows::fs::symlink_file(original, link) @@ -53,7 +67,10 @@ fn symlink, Q: AsRef>(original: P, link: Q) -> io::Result<( } fn copy_dir_all(src: impl AsRef, dst: impl AsRef) -> io::Result<()> { + + trace!("creating directory: {:?}", dst.as_ref()); fs::create_dir_all(&dst)?; + trace!("reading source directory: {:?}", src.as_ref()); for entry in fs::read_dir(src)? { let entry = entry?; let ty = entry.file_type()?; diff --git a/packages/nx/src/tasks-runner/cache.ts b/packages/nx/src/tasks-runner/cache.ts index 55447db4bbea3..e9306dc18cd9c 100644 --- a/packages/nx/src/tasks-runner/cache.ts +++ b/packages/nx/src/tasks-runner/cache.ts @@ -32,7 +32,8 @@ export type TaskWithCachedResult = { task: Task; cachedResult: CachedResult }; export function dbCacheEnabled(nxJson: NxJsonConfiguration = readNxJson()) { return ( process.env.NX_DISABLE_DB !== 'true' && - (nxJson.enableDbCache === true || process.env.NX_DB_CACHE === 'true') + nxJson.useLegacyCache !== true && + process.env.NX_DB_CACHE !== 'false' ); }