Skip to content

Commit

Permalink
feat(core): enable db cache by default (#28048)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
DB cache is disabled by default

## Expected Behavior
DB cache is enabled by default 

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
Cammisuli authored Oct 4, 2024
1 parent d477ea7 commit d5c4521
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 29 deletions.
18 changes: 9 additions & 9 deletions docs/generated/devkit/NxJsonConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
Expand All @@ -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
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
26 changes: 13 additions & 13 deletions docs/generated/devkit/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[] | "\*">
Expand All @@ -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

Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/adapter/compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const allowedWorkspaceExtensions = [
'useInferencePlugins',
'neverConnectToCloud',
'sync',
'enableDbCache',
'useLegacyCache',
] as const;

if (!patched) {
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/config/nx-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,9 @@ export interface NxJsonConfiguration<T = '*' | string[]> {
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;
Expand Down
11 changes: 9 additions & 2 deletions packages/nx/src/native/cache/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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(())
}
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 18 additions & 1 deletion packages/nx/src/native/cache/file_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand All @@ -17,7 +18,8 @@ pub fn copy(src: String, dest: String) -> anyhow::Result<()> {
}

pub fn _copy<P>(src: P, dest: P) -> anyhow::Result<()> where P: AsRef<Path> {
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();
Expand All @@ -37,6 +39,18 @@ pub fn _copy<P>(src: P, dest: P) -> anyhow::Result<()> where P: AsRef<Path> {
Ok(())
}

fn remove_trailing_single_dot(path: impl AsRef<Path>) -> PathBuf {
let mut components = path.as_ref().components().collect::<Vec<_>>();

if let Some(last_component) = components.last() {
if last_component.as_os_str() == "." {
components.pop();
}
}

components.iter().collect()
}

#[cfg(windows)]
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<()> {
std::os::windows::fs::symlink_file(original, link)
Expand All @@ -53,7 +67,10 @@ fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> io::Result<(
}

fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> 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()?;
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/tasks-runner/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

Expand Down

0 comments on commit d5c4521

Please sign in to comment.