{"record":{"id":"f268ca0d2d1a1d00","repo":"hcengineering/platform","slug":"document-space-spacepath-not-found","errorCode":null,"errorMessage":"Document space ${spacePath} not found","messagePattern":"Document space (.+?) not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/importer/src/importer/builder.ts","lineNumber":163,"sourceCode":"\n  addOrgSpace (path: string, space: ImportOrgSpace): this {\n    this.validateAndAdd('documentSpace', path, space, (s) => this.validateOrgSpace(s), this.qmsSpaces, path)\n    return this\n  }\n\n  addControlledDocument (\n    spacePath: string,\n    docPath: string,\n    doc: ImportControlledDocument,\n    parentDocPath?: string\n  ): this {\n    if (!this.qmsDocsBySpace.has(spacePath)) {\n      this.qmsDocsBySpace.set(spacePath, new Map())\n    }\n\n    const docs = this.qmsDocsBySpace.get(spacePath)\n    if (docs === undefined) {\n      throw new Error(`Document space ${spacePath} not found`)\n    }\n\n    this.validateAndAdd(\n      'controlledDocument',\n      docPath,\n      doc,\n      (d) => this.validateControlledDocument(d as ImportControlledDocument),\n      docs,\n      docPath\n    )\n\n    if (parentDocPath !== undefined) {\n      this.qmsDocsParents.set(docPath, parentDocPath)\n    }\n\n    return this\n  }\n","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/packages/importer/src/importer/builder.ts#L145-L181","documentation":"WorkspaceBuilder.addControlledDocument stores controlled documents per QMS space in this.qmsDocsBySpace, creating the inner Map if absent, then throws 'Document space <path> not found' if get() still returns undefined. This is a defensive invariant; in sequential use the entry is created on the same call, so the throw indicates corrupted or concurrently mutated builder state.","triggerScenarios":"addControlledDocument invoked while the qmsDocsBySpace entry for spacePath is externally removed, or builder state is mutated concurrently between the set and get calls.","commonSituations":"Parallel import pipelines sharing one builder; code that clears qmsDocsBySpace to re-run import; subclasses overriding map lifecycle.","solutions":["Register the space first with addOrgSpace(spacePath, space) so document processing has a well-defined order.","Keep builder usage single-threaded / sequential per import run.","Remove any mid-run clearing or replacement of the builder's internal maps.","Report as a bug if it fires in plain sequential usage."],"exampleFix":"// before\nbuilder.addControlledDocument('space/qms', docPath, doc)\n// after\nbuilder.addOrgSpace('space/qms', orgSpace)\nbuilder.addControlledDocument('space/qms', docPath, doc)","handlingStrategy":"try-catch","validationCode":"if (!builder.getOrgSpaces().has(spacePath)) {\n  builder.addOrgSpace(spacePath, orgSpace)\n}","typeGuard":null,"tryCatchPattern":"try {\n  builder.addControlledDocument(spacePath, docPath, doc)\n} catch (e) {\n  if ((e as Error).message === `Document space ${spacePath} not found`) {\n    console.error(`Builder state lost for space ${spacePath}; re-add the org space first`)\n  } else throw e\n}","preventionTips":["Call addOrgSpace for each QMS space before adding controlled documents to it.","Process documents and templates sequentially on one builder.","Avoid clearing qmsDocsBySpace between import phases.","On occurrence, rebuild the workspace from scratch."],"tags":["importer","builder","invariant","qms"],"backgroundTag":"internal-state-invariant","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}