angular/angular-cli · error
Cannot get context when workflow is not executing...
Error message
Cannot get context when workflow is not executing...
What it means
BaseWorkflow.context returns the top of the _context stack, which only has entries while the workflow is executing a schematic. Outside an execution (or after it finishes) the stack is empty and it throws. The context represents the running execution's logger/interrupt handling.
Source
Thrown at packages/angular_devkit/schematics/src/workflow/base.ts:96
if (options.registry) {
this._registry = options.registry;
} else {
this._registry = new schema.CoreSchemaRegistry(standardFormats);
this._registry.addPostTransform(schema.transforms.addUndefinedDefaults);
}
this._engine = new SchematicEngine(this._engineHost, this);
this._context = [];
this._force = options.force || false;
this._dryRun = options.dryRun || false;
}
get context(): Readonly<WorkflowExecutionContext> {
const maybeContext = this._context.at(-1);
if (!maybeContext) {
throw new Error('Cannot get context when workflow is not executing...');
}
return maybeContext;
}
get engine(): Engine<{}, {}> {
return this._engine;
}
get engineHost(): EngineHost<{}, {}> {
return this._engineHost;
}
get registry(): schema.SchemaRegistry {
return this._registry;
}
get reporter(): Observable<DryRunEvent> {
return this._reporter.asObservable();
}
get lifeCycle(): Observable<LifeCycleEvent> {
return this._lifeCycle.asObservable();View on GitHub (pinned to bb72145f9a)
Solutions
- Only access workflow.context from inside code invoked during execute() (rules, tasks, event handlers)
- Pass the SchematicContext/execution context as a parameter instead of reaching for workflow.context
- Capture the context in a local variable inside the executing scope and use that
- If you need logging outside execution, create your own logging context rather than workflow.context
Example fix
// before
const ctx = workflow.context; // throws: not executing
workflow.execute({ collection, schematic });
// after
workflow
.execute({ collection, schematic })
.then(() => {})
.catch((err) => console.error(err));
// inside a rule: (tree, context) => { /* context is provided here */ } Defensive patterns
Strategy: validation
Validate before calling
if (!isExecuting) { /* track execution state yourself */ }
// or: only read context inside rule/task callbacks where it is passed in Try / catch
let ctx;
try {
ctx = workflow.context;
} catch (e) {
if (String(e).includes('when workflow is not executing')) {
ctx = createFallbackLoggerContext();
} else throw e;
} Prevention
- Never read workflow.context outside execute() lifetime
- Use the SchematicContext passed to rules and task executors instead of global access
- Capture context in a local variable within the executing scope
- In async callbacks, ensure the workflow is still executing before touching context
When it happens
Trigger: Reading workflow.context before calling workflow.execute(), after the execute() promise resolves, or from a detached async callback that runs after execution ended; storing context and using it later.
Common situations: Custom task code capturing context at import time; CLI tools introspecting the workflow outside run(); logging hooks firing after completion; tests accessing context on a freshly constructed workflow.
Related errors
- Option "project" is required.
- Project is not defined in this workspace.
- Targets are not defined for this project.
- TestProjectHost must be initialized before being used.
- Unknown task dependency [ID: ${id.id}].
AI-assisted analysis of angular/angular-cli@bb72145f9a (2026-08-30).
Data as JSON: /api/errors/9c6fda5e1f84e882.
Report an issue: GitHub.