{"record":{"id":"c2ecb740d4b3b848","repo":"semaphoreui/semaphore","slug":"environment-does-not-exist-in-environments-name","errorCode":null,"errorMessage":"environment does not exist in environments[].name","messagePattern":"environment does not exist in environments\\[\\]\\.name","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"services/project/restore.go","lineNumber":297,"sourceCode":"}\n\nfunc (e BackupTemplate) Restore(b *BackupDB) error {\n\tvar InventoryID *int\n\tif e.Inventory != nil {\n\t\tif k := findEntityByName[db.Inventory](e.Inventory, b.inventories); k == nil {\n\t\t\treturn fmt.Errorf(\"inventory does not exist in inventories[].name\")\n\t\t} else {\n\t\t\tid := k.GetID()\n\t\t\tInventoryID = &id\n\t\t}\n\t}\n\n\tvar EnvironmentIDs []int\n\tfor i := range e.Environments {\n\t\tenvName := &e.Environments[i]\n\t\tk := findEntityByName[db.Environment](envName, b.environments)\n\t\tif k == nil {\n\t\t\treturn fmt.Errorf(\"environment does not exist in environments[].name\")\n\t\t}\n\t\tEnvironmentIDs = append(EnvironmentIDs, k.GetID())\n\t}\n\n\tvar RepositoryID int\n\tif k := findEntityByName[db.Repository](&e.Repository, b.repositories); k == nil {\n\t\treturn fmt.Errorf(\"repository does not exist in repositories[].name\")\n\t} else {\n\t\tRepositoryID = k.GetID()\n\t}\n\n\tvar BuildTemplateID *int\n\tif string(e.Type) != \"deploy\" {\n\t\tBuildTemplateID = nil\n\t} else if k := findEntityByName[db.Template](e.BuildTemplate, b.templates); k == nil {\n\t\tBuildTemplateID = nil\n\t} else {\n\t\tBuildTemplateID = &(k.ID)","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/semaphoreui/semaphore/blob/1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa/services/project/restore.go#L279-L315","documentation":"Thrown by BackupTemplate.Restore while resolving the template's Environments list: one of the referenced environment names has no match in b.environments, so the environment ID list cannot be built. Restore aborts for this template to avoid linking it to a nonexistent environment.","triggerScenarios":"Restore where any e.Environments[i] has findEntityByName[db.Environment](envName, b.environments) == nil — environments restored after templates, an environment removed post-Verify, or a name mismatch (case/whitespace) between the backup and the restored environments.","commonSituations":"Out-of-order partial restores; renaming environments between backup and restore; templates referencing environments from a different project's backup; retries of single-template restores without their environment dependencies.","solutions":["Restore environments before templates so b.environments contains every referenced name.","Fix the environments[] names in the template entry to match the restored environments exactly.","Remove the missing environment from the template's environments list.","Re-run the full restore from the complete backup."],"exampleFix":"// before\n\"environments\": [\"stg\", \"prod\"], // backup only has \"staging\", \"prod\"\n// after\n\"environments\": [\"staging\", \"prod\"]","handlingStrategy":"validation","validationCode":"for _, env := range tmpl.Environments {\n  if findEntityByName[db.Environment](&env, b.environments) == nil {\n    return fmt.Errorf(\"environment %q not restored\", env)\n  }\n}","typeGuard":null,"tryCatchPattern":"if err := tmpl.Restore(b); err != nil {\n  if strings.Contains(err.Error(), \"environment does not exist\") {\n    // restore environments phase first, then retry the template\n  }\n}","preventionTips":["Restore environments before templates in every pipeline.","Keep environment names identical between source and target projects.","Cross-check template environments[] against restored environments before restore."],"tags":["restore","backup","referential-integrity","environment","go"],"backgroundTag":"entity-not-found","analyzedSha":"1774ccb71a0a8b82eb74ea24c23ac9ab713de2fa","analyzedAt":"2026-09-07T11:00:33.293Z","contentChangedAt":"2026-09-07T11:00:33.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}