angular/angular-cli · error · Error

Cannot retrieve cache configuration as workspace is not defi

Error message

Cannot retrieve cache configuration as workspace is not defined.

What it means

getCacheConfig() in the Angular CLI's cache command utilities returns the effective cache settings (path, environment, enabled) for a workspace. Because the entire function is meaningless without a workspace, it explicitly throws a plain Error when the passed AngularWorkspace is undefined instead of silently returning defaults.

Source

Thrown at packages/angular/cli/src/commands/cache/utilities.ts:69

              return resolve(dirname(commonGitDir), cachePathSetting);
            }
          }
        }
      }
      const parentDir = dirname(currentDir);
      if (parentDir === currentDir) {
        break;
      }
      currentDir = parentDir;
    }
  } catch {}

  return resolve(workspaceRoot, cachePathSetting);
}

export function getCacheConfig(workspace: AngularWorkspace | undefined): Required<Cache> {
  if (!workspace) {
    throw new Error(`Cannot retrieve cache configuration as workspace is not defined.`);
  }

  const defaultSettings: Required<Cache> = {
    path: getCacheBasePath(workspace.basePath, '.angular/cache'),
    environment: Environment.Local,
    enabled: true,
  };

  const cliSetting = workspace.extensions['cli'];
  if (!cliSetting || !isJsonObject(cliSetting)) {
    return defaultSettings;
  }

  const cacheSettings = cliSetting['cache'];
  if (!isJsonObject(cacheSettings)) {
    return defaultSettings;
  }

View on GitHub (pinned to bb72145f9a)

Solutions

  1. Run the command from inside an Angular project directory (angular.json present in cwd or an ancestor)
  2. Restore/rename angular.json if it was deleted or renamed
  3. If calling the API programmatically, resolve the workspace first with getWorkspace/getWorkspaceRaw and pass the result to getCacheConfig
  4. Check that cli.workspaceContext / workspace root detection is correctly configured if embedding the CLI

Example fix

// before
const config = getCacheConfig(undefined);
// after
const workspace = await getWorkspace('local');
if (!workspace) {
  throw new Error('Run this command inside an Angular workspace (angular.json not found).');
}
const config = getCacheConfig(workspace);
Defensive patterns

Strategy: validation

Validate before calling

import { getWorkspace } from '@angular/cli/src/utilities/workspace';
const workspace = await getWorkspace('local');
if (!workspace) {
  throw new Error('No Angular workspace found; `ng cache` requires angular.json.');
}
const cacheConfig = getCacheConfig(workspace);

Type guard

function hasWorkspace(w: AngularWorkspace | undefined | null): w is AngularWorkspace {
  return !!w && typeof w.basePath === 'string';
}

Try / catch

try {
  const config = getCacheConfig(maybeWorkspace);
} catch (e) {
  if (e instanceof Error && e.message.includes('workspace is not defined')) {
    logger.error('Run this command inside an Angular workspace (angular.json missing).');
  } else { throw e; }
}

Prevention

When it happens

Trigger: Calling getCacheConfig(undefined) — typically from command handlers (e.g. `ng cache clean`, `ng cache enable`) executed in a context where the workspace file (angular.json) could not be located, so the resolved workspace is undefined before being passed in.

Common situations: Running `ng cache` commands outside an Angular workspace directory (no angular.json in or above cwd); a corrupted or renamed angular.json; invoking the cache utilities programmatically without first resolving a workspace.

Related errors


AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30). Data as JSON: /api/errors/03c84a8a11e1ec21. Report an issue: GitHub.