{"record":{"id":"9c6fda5e1f84e882","repo":"angular/angular-cli","slug":"cannot-get-context-when-workflow-is-not-executing","errorCode":null,"errorMessage":"Cannot get context when workflow is not executing...","messagePattern":"Cannot get context when workflow is not executing\\.\\.\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/angular_devkit/schematics/src/workflow/base.ts","lineNumber":96,"sourceCode":"    if (options.registry) {\n      this._registry = options.registry;\n    } else {\n      this._registry = new schema.CoreSchemaRegistry(standardFormats);\n      this._registry.addPostTransform(schema.transforms.addUndefinedDefaults);\n    }\n\n    this._engine = new SchematicEngine(this._engineHost, this);\n\n    this._context = [];\n\n    this._force = options.force || false;\n    this._dryRun = options.dryRun || false;\n  }\n\n  get context(): Readonly<WorkflowExecutionContext> {\n    const maybeContext = this._context.at(-1);\n    if (!maybeContext) {\n      throw new Error('Cannot get context when workflow is not executing...');\n    }\n\n    return maybeContext;\n  }\n  get engine(): Engine<{}, {}> {\n    return this._engine;\n  }\n  get engineHost(): EngineHost<{}, {}> {\n    return this._engineHost;\n  }\n  get registry(): schema.SchemaRegistry {\n    return this._registry;\n  }\n  get reporter(): Observable<DryRunEvent> {\n    return this._reporter.asObservable();\n  }\n  get lifeCycle(): Observable<LifeCycleEvent> {\n    return this._lifeCycle.asObservable();","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/angular/angular-cli/blob/bb72145f9ab45aee29f523236b3a25cd0813a841/packages/angular_devkit/schematics/src/workflow/base.ts#L78-L114","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst ctx = workflow.context; // throws: not executing\nworkflow.execute({ collection, schematic });\n// after\nworkflow\n  .execute({ collection, schematic })\n  .then(() => {})\n  .catch((err) => console.error(err));\n// inside a rule: (tree, context) => { /* context is provided here */ }","handlingStrategy":"validation","validationCode":"if (!isExecuting) { /* track execution state yourself */ }\n// or: only read context inside rule/task callbacks where it is passed in","typeGuard":null,"tryCatchPattern":"let ctx;\ntry {\n  ctx = workflow.context;\n} catch (e) {\n  if (String(e).includes('when workflow is not executing')) {\n    ctx = createFallbackLoggerContext();\n  } else throw e;\n}","preventionTips":["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"],"tags":["schematics","workflow","lifecycle","angular"],"backgroundTag":"workflow-not-executing","analyzedSha":"bb72145f9ab45aee29f523236b3a25cd0813a841","analyzedAt":"2026-08-30T02:47:34.745Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}