diff --git a/packages/workspace/src/command-line/reset.ts b/packages/workspace/src/command-line/reset.ts index b94624f3b8..9c8c42d902 100644 --- a/packages/workspace/src/command-line/reset.ts +++ b/packages/workspace/src/command-line/reset.ts @@ -1,22 +1,15 @@ -import { appRootPath } from '@nrwl/tao/src/utils/app-root'; -import { remove } from 'fs-extra'; +import { removeSync } from 'fs-extra'; import { stop as stopDaemon } from '../core/project-graph/daemon/client/client'; -import { - cacheDirectory, - readCacheDirectoryProperty, -} from '../utilities/cache-directory'; +import { cacheDir } from '../utilities/cache-directory'; import { output } from '../utilities/output'; -export async function resetHandler() { +export function resetHandler() { output.note({ title: 'Resetting the Nx workspace cache and stopping the Nx Daemon.', bodyLines: [`This might take a few minutes.`], }); - const dir = cacheDirectory( - appRootPath, - readCacheDirectoryProperty(appRootPath) - ); - await Promise.all([stopDaemon(), remove(dir)]); + stopDaemon(); + removeSync(cacheDir); output.success({ title: 'Successful reset the Nx workspace.', }); diff --git a/packages/workspace/src/core/nx-deps/nx-deps-cache.ts b/packages/workspace/src/core/nx-deps/nx-deps-cache.ts index 87ace5bb02..6d0bec8531 100644 --- a/packages/workspace/src/core/nx-deps/nx-deps-cache.ts +++ b/packages/workspace/src/core/nx-deps/nx-deps-cache.ts @@ -10,15 +10,11 @@ import type { } from '@nrwl/devkit'; import { readJsonFile, writeJsonFile } from '@nrwl/devkit'; import { join } from 'path'; -import { appRootPath } from '@nrwl/tao/src/utils/app-root'; import { existsSync } from 'fs'; import { ensureDirSync } from 'fs-extra'; import { directoryExists, fileExists } from '../../utilities/fileutils'; import { performance } from 'perf_hooks'; -import { - cacheDirectory, - readCacheDirectoryProperty, -} from '../../utilities/cache-directory'; +import { cacheDir } from '../../utilities/cache-directory'; export interface ProjectGraphCache { version: string; @@ -33,16 +29,12 @@ export interface ProjectGraphCache { dependencies: Record; } -export const nxDepsDir = cacheDirectory( - appRootPath, - readCacheDirectoryProperty(appRootPath) -); -export const nxDepsPath = join(nxDepsDir, 'nxdeps.json'); +export const nxDepsPath = join(cacheDir, 'nxdeps.json'); export function ensureCacheDirectory(): void { try { - if (!existsSync(nxDepsDir)) { - ensureDirSync(nxDepsDir); + if (!existsSync(cacheDir)) { + ensureDirSync(cacheDir); } } catch (e) { /* @@ -55,8 +47,8 @@ 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(nxDepsDir)) { - throw new Error(`Failed to create directory: ${nxDepsDir}`); + if (!directoryExists(cacheDir)) { + throw new Error(`Failed to create directory: ${cacheDir}`); } } } diff --git a/packages/workspace/src/core/project-graph/daemon/client/client.ts b/packages/workspace/src/core/project-graph/daemon/client/client.ts index 605a696f3d..1b885dda7c 100644 --- a/packages/workspace/src/core/project-graph/daemon/client/client.ts +++ b/packages/workspace/src/core/project-graph/daemon/client/client.ts @@ -1,6 +1,7 @@ import { logger, ProjectGraph } from '@nrwl/devkit'; import { ChildProcess, spawn, spawnSync } from 'child_process'; import { openSync, readFileSync } from 'fs'; +import { ensureDirSync, ensureFileSync } from 'fs-extra'; import { connect } from 'net'; import { performance } from 'perf_hooks'; import { @@ -8,10 +9,15 @@ import { writeDaemonJsonProcessCache, } from '../cache'; import { FULL_OS_SOCKET_PATH, killSocketOrPath } from '../socket-utils'; -import { DAEMON_OUTPUT_LOG_FILE } from '../tmp-dir'; +import { + DAEMON_DIR_FOR_CURRENT_WORKSPACE, + DAEMON_OUTPUT_LOG_FILE, +} from '../tmp-dir'; export async function startInBackground(): Promise { await safelyCleanUpExistingProcess(); + ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE); + ensureFileSync(DAEMON_OUTPUT_LOG_FILE); try { const out = openSync(DAEMON_OUTPUT_LOG_FILE, 'a'); diff --git a/packages/workspace/src/core/project-graph/daemon/tmp-dir.ts b/packages/workspace/src/core/project-graph/daemon/tmp-dir.ts index 6b8d7d55c3..e93d758c1f 100644 --- a/packages/workspace/src/core/project-graph/daemon/tmp-dir.ts +++ b/packages/workspace/src/core/project-graph/daemon/tmp-dir.ts @@ -5,10 +5,9 @@ */ import { normalizePath } from '@nrwl/devkit'; import { appRootPath } from '@nrwl/tao/src/utils/app-root'; -import { ensureDirSync, ensureFileSync } from 'fs-extra'; -import { tmpdir } from 'os'; import { join } from 'path'; import { createHash } from 'crypto'; +import { cacheDir } from '../../../utilities/cache-directory'; // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping function escapeRegExp(string) { @@ -48,22 +47,16 @@ const subDirForCurrentWorkspace = createHash('sha1') .digest('hex') .slice(0, 32); -/** - * E.g. on a mac the value for this will be something like: - * /var/folders/zk/9ff5snsj71j2r07qht44w_nr0000gn/T/nx-daemon/{{subDirForCurrentWorkspace}} - */ export const DAEMON_DIR_FOR_CURRENT_WORKSPACE = join( - tmpdir(), + cacheDir, 'nx-daemon', subDirForCurrentWorkspace ); -ensureDirSync(DAEMON_DIR_FOR_CURRENT_WORKSPACE); export const DAEMON_OUTPUT_LOG_FILE = join( DAEMON_DIR_FOR_CURRENT_WORKSPACE, 'server.log' ); -ensureFileSync(DAEMON_OUTPUT_LOG_FILE); export const DAEMON_SOCKET_PATH = join( DAEMON_DIR_FOR_CURRENT_WORKSPACE, diff --git a/packages/workspace/src/tasks-runner/cache.ts b/packages/workspace/src/tasks-runner/cache.ts index 1ce5dabf86..230a582463 100644 --- a/packages/workspace/src/tasks-runner/cache.ts +++ b/packages/workspace/src/tasks-runner/cache.ts @@ -14,7 +14,7 @@ import { import { dirname, join, resolve, sep } from 'path'; import { DefaultTasksRunnerOptions } from './default-tasks-runner'; import { spawn, exec } from 'child_process'; -import { cacheDirectory } from '../utilities/cache-directory'; +import { cacheDir } from '../utilities/cache-directory'; const util = require('util'); @@ -320,9 +320,8 @@ export class Cache { } private createCacheDir() { - const dir = cacheDirectory(this.root, this.options.cacheDirectory); - ensureDirSync(dir); - return dir; + ensureDirSync(cacheDir); + return cacheDir; } private createTerminalOutputsDir() { diff --git a/packages/workspace/src/utilities/cache-directory.ts b/packages/workspace/src/utilities/cache-directory.ts index da10c38678..5801698cb1 100644 --- a/packages/workspace/src/utilities/cache-directory.ts +++ b/packages/workspace/src/utilities/cache-directory.ts @@ -1,7 +1,8 @@ import { NxJsonConfiguration, readJsonFile } from '@nrwl/devkit'; import { join } from 'path'; +import { appRootPath } from '@nrwl/tao/src/utils/app-root'; -export function readCacheDirectoryProperty(root: string): string | undefined { +function readCacheDirectoryProperty(root: string): string | undefined { try { const nxJson = readJsonFile(join(root, 'nx.json')); return nxJson.tasksRunnerOptions.default.options.cacheDirectory; @@ -10,7 +11,7 @@ export function readCacheDirectoryProperty(root: string): string | undefined { } } -export function cacheDirectory(root: string, cacheDirectory: string) { +function cacheDirectory(root: string, cacheDirectory: string) { const cacheDirFromEnv = process.env.NX_CACHE_DIRECTORY; if (cacheDirFromEnv) { cacheDirectory = cacheDirFromEnv; @@ -25,3 +26,8 @@ export function cacheDirectory(root: string, cacheDirectory: string) { return join(root, 'node_modules', '.cache', 'nx'); } } + +export const cacheDir = cacheDirectory( + appRootPath, + readCacheDirectoryProperty(appRootPath) +);