Skip to content

Commit

Permalink
feat(core): introduce the NX_PROJECT_GRAPH_CACHE_DIRECTORY to allow s…
Browse files Browse the repository at this point in the history
…eparating it from the computation cache (#10693)
  • Loading branch information
FrozenPandaz committed Jun 10, 2022
1 parent 9de9cb3 commit 1937deb
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
8 changes: 6 additions & 2 deletions docs/shared/using-nx/caching.md
Expand Up @@ -179,7 +179,7 @@ nx affected --target=build --skip-nx-cache

## Customizing the Cache Location

The cache is stored in `node_modules/.cache/nx` by default. To change the cache location, update the `cacheDirectory` option for the task runner:
The cache is stored in `node_modules/.cache/nx` by default. To change the cache location, set a `NX_CACHE_DIRECTORY` environment variable or update the `cacheDirectory` option for the task runner:

```json
{
Expand Down Expand Up @@ -209,7 +209,11 @@ You can connect your workspace to Nx Cloud by running:
nx connect-to-nx-cloud
```

You can also distribute the cache manually using your own storage mechanisms.
You can also distribute the cache manually using your own storage mechanisms. Be warned, it is not as simple as it might sound initially.

- _If possible_, pull a copy of the cache which doesn't mutate the cache currently in use by other consumers. Push an updated copy afterwards.
- By default, Nx also caches some computations necessary for calculating the project graph in the cache directory as well.
- If you are using a shared volume where mutations to the cache are reflected immediately in other consumers, you should set `NX_PROJECT_GRAPH_CACHE_DIRECTORY` to a separate directory to separate it from the computation cache.

## Example

Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/command-line/reset.ts
@@ -1,6 +1,6 @@
import { removeSync } from 'fs-extra';
import { stop as stopDaemon } from '../daemon/client/client';
import { cacheDir } from '../utils/cache-directory';
import { cacheDir, projectGraphCacheDirectory } from '../utils/cache-directory';
import { output } from '../utils/output';

export function resetHandler() {
Expand All @@ -10,6 +10,9 @@ export function resetHandler() {
});
stopDaemon();
removeSync(cacheDir);
if (projectGraphCacheDirectory !== cacheDir) {
removeSync(projectGraphCacheDirectory);
}
output.success({
title: 'Successfully reset the Nx workspace.',
});
Expand Down
7 changes: 5 additions & 2 deletions packages/nx/src/daemon/tmp-dir.ts
Expand Up @@ -5,9 +5,12 @@
*/
import { statSync, writeFileSync } from 'fs';
import { join } from 'path';
import { cacheDir } from '../utils/cache-directory';
import { projectGraphCacheDirectory } from '../utils/cache-directory';

export const DAEMON_DIR_FOR_CURRENT_WORKSPACE = join(cacheDir, 'd');
export const DAEMON_DIR_FOR_CURRENT_WORKSPACE = join(
projectGraphCacheDirectory,
'd'
);

export const DAEMON_OUTPUT_LOG_FILE = join(
DAEMON_DIR_FOR_CURRENT_WORKSPACE,
Expand Down
14 changes: 8 additions & 6 deletions packages/nx/src/project-graph/nx-deps-cache.ts
Expand Up @@ -2,7 +2,7 @@ import { existsSync } from 'fs';
import { ensureDirSync } from 'fs-extra';
import { join } from 'path';
import { performance } from 'perf_hooks';
import { cacheDir } from '../utils/cache-directory';
import { projectGraphCacheDirectory } from '../utils/cache-directory';
import { directoryExists, fileExists } from '../utils/fileutils';
import {
FileData,
Expand All @@ -29,12 +29,12 @@ export interface ProjectGraphCache {
dependencies: Record<string, ProjectGraphDependency[]>;
}

export const nxDepsPath = join(cacheDir, 'nxdeps.json');
export const nxDepsPath = join(projectGraphCacheDirectory, 'nxdeps.json');

export function ensureCacheDirectory(): void {
try {
if (!existsSync(cacheDir)) {
ensureDirSync(cacheDir);
if (!existsSync(projectGraphCacheDirectory)) {
ensureDirSync(projectGraphCacheDirectory);
}
} catch (e) {
/*
Expand All @@ -47,8 +47,10 @@ export function ensureCacheDirectory(): void {
* In this case, we're creating the directory. If the operation failed, we ensure that the directory
* exists before continuing (or raise an exception).
*/
if (!directoryExists(cacheDir)) {
throw new Error(`Failed to create directory: ${cacheDir}`);
if (!directoryExists(projectGraphCacheDirectory)) {
throw new Error(
`Failed to create directory: ${projectGraphCacheDirectory}`
);
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions packages/nx/src/utils/cache-directory.ts
Expand Up @@ -35,3 +35,6 @@ export const cacheDir = cacheDirectory(
workspaceRoot,
readCacheDirectoryProperty(workspaceRoot)
);

export const projectGraphCacheDirectory =
process.env.NX_PROJECT_GRAPH_CACHE_DIRECTORY ?? cacheDir;

1 comment on commit 1937deb

@vercel
Copy link

@vercel vercel bot commented on 1937deb Jun 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx.dev
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app

Please sign in to comment.