Skip to content

[BUG]: Pre-existing fork repositories are recreated when fork set to true #2954

Closed

Description

@julianr-am

Expected Behavior

The fork parameter can be set to true to track the forked status of pre-existing github_repository resources that manage fork repositories, or left blank to prevent terraform from recreating the repository.

Actual Behavior

Terraform tries to recreate pre-existing github_repository resources that manage fork repositories on newer versions of the provider when the fork parameter is set to true.

Terraform Version

Terraform v1.5.7
on linux_amd64

  • provider registry.terraform.io/integrations/github v6.8.3

Affected Resource(s)

github_repository

Terraform Configuration Files

terraform {
  required_version = ">= 1.5.7"
  required_providers {
    github = {
      source  = "integrations/github"
      version = "6.8.3"
    }
  }
}

provider "github" {
  owner = "owner-here"
}

resource "github_repository" "repository" {
  name                        = "fork-bug-terraform-provider-github"
  allow_update_branch         = false
  auto_init                   = false
  description                 = "Repository forked to troubleshoot provider issue when reading repository properties"
  has_discussions             = false
  has_downloads               = true
  has_issues                  = false
  has_projects                = true
  has_wiki                    = false
  homepage_url                = "https://www.terraform.io/docs/providers/github/"

  
  # Added after upgrading to v6.8.3 from 6.7.5
  fork                   = true
  source_repo            = "terraform-provider-github"
  source_owner           = "integrations"
}

Steps to Reproduce

  • Manually fork a repository using the Github Web UI
  • Import the repository to a github_repository in a terraform-provider-github version previous to feat(core): add fork functionality #2678 (6.7.5 was used in this case)
  • Run a successful apply that imports the resource, then upgrade to terraform-provider-github version 6.8.3
  • Set the resource fork, source_owner and source_repo parameters accordingly, then run terraform plan

Debug Output

Terraform will perform the following actions:                                                                                  

  # github_repository.repository must be replaced
-/+ resource "github_repository" "repository" {
      ~ default_branch              = "main" -> (known after apply)
      ~ etag                        = "W/\"...\"" -> (known after apply)
      + fork                        = true # forces replacement
      ~ full_name                   = "owner-here/fork-bug-terraform-provider-github" -> (known after apply)
      ~ git_clone_url               = "git://github.com/owner-here/fork-bug-terraform-provider-github.git" -> (known after apply)
      ~ html_url                    = "https://github.com/owner-here/fork-bug-terraform-provider-github" -> (known after apply) 
      ~ http_clone_url              = "https://github.com/owner-here/fork-bug-terraform-provider-github.git" -> (known after apply)
      ~ id                          = "fork-bug-terraform-provider-github" -> (known after apply)
      - is_template                 = false -> null
        name                        = "fork-bug-terraform-provider-github"
      ~ node_id                     = "..." -> (known after apply)
      ~ primary_language            = "Go" -> (known after apply)
      ~ private                     = false -> (known after apply)
      ~ repo_id                     = ... -> (known after apply)
      + source_owner                = "integrations" # forces replacement
      + source_repo                 = "terraform-provider-github" # forces replacement
      ~ ssh_clone_url               = "[email protected]:owner-here/fork-bug-terraform-provider-github.git" -> (known after apply) 
      ~ svn_url                     = "https://github.com/owner-here/fork-bug-terraform-provider-github" -> (known after apply) 
      ~ topics                      = [] -> (known after apply)
      ~ visibility                  = "public" -> (known after apply)
      ~ vulnerability_alerts        = false -> (known after apply)
        # (20 unchanged attributes hidden)

      - security_and_analysis {
          - secret_scanning {
              - status = "enabled" -> null
            }
          - secret_scanning_push_protection {
              - status = "enabled" -> null
            }
        }
    }

Plan: 1 to add, 0 to change, 1 to destroy.

Panic Output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    Type: Bug
    Status: Triage
    on Nov 27, 2025
  2. deiga commented on Nov 30, 2025

    @deiga
    Collaborator

    Hey @julianr-am 👋

    Thanks for raising this.

    Based on the PR description I understand that using the fork attribute is only for creating a new fork. Existing forks should be recognised without doing anything.

    Are you able to confirm how this acts following the same steps without adding the fork attributes after upgrade?

  3. brianmichel commented on Dec 2, 2025

    @brianmichel

    Yesterday we had updated to the 6.8.x provider version and had a forked repo get destroyed and recreated for some reason. In the workflow run that produced this diff, there was no terraform changes, and this repo has been in terraform state for years at this point so I found this extremely surprising (this caused an internal incident obviously).

    Interestingly, we had never been setting the fork parameter at all (our provider was older then 6.8.0 which introduced the fork concept).

    Terraform Version: v1.10.2
    Provider Version: v6.8.3

    For repos that we have under terraform management, the resource definition looks like this, and has for a long time:

    resource "github_repository" "github_repository" {
      for_each = local.repositories
    
      name = lookup(local.name_overrides, each.key, each.key)
      #tfsec:ignore:github-repositories-private
      visibility             = contains(local.is_public, each.key) ? "public" : "private"
      description            = each.value
      delete_branch_on_merge = !contains(local.keep_branch_on_merge, each.key)
      has_wiki               = contains(local.has_wiki, each.key)
      has_projects           = contains(local.has_projects, each.key)
      has_downloads          = contains(local.has_downloads, each.key)
      has_issues             = contains(local.has_issues, each.key)
      archived               = contains(local.is_archived, each.key)
      topics                 = []
      allow_merge_commit     = true
      allow_rebase_merge     = true
      allow_squash_merge     = true
      allow_auto_merge       = contains(local.allow_auto_merge, each.key)
      auto_init              = null
      gitignore_template     = null
      license_template       = ""
      homepage_url           = lookup(local.homepage_urls, each.key, "")
      vulnerability_alerts   = contains(local.is_archived, each.key) ? null : true
      allow_update_branch    = contains(local.block_update_branch, each.key) ? false : true
    
      dynamic "pages" {
        for_each = flatten([(lookup(local.pages, each.key, []))])
        content {
          cname = pages.value["cname"]
    
          source {
            branch = pages.value["source_branch"]
            path   = pages.value["source_path"]
          }
        }
      }
    }

    Terraform Output:

    Terraform used the selected providers to generate the following execution
    plan. Resource actions are indicated with the following symbols:
    -/+ destroy and then create replacement
    
    Terraform will perform the following actions:
    
      # github_repository.github_repository["swift-composable-architecture"] must be replaced
    -/+ resource "github_repository" "github_repository" {
          ~ allow_merge_commit          = false -> true
          ~ allow_rebase_merge          = false -> true
          - auto_init                   = false -> null
          ~ default_branch              = "main" -> (known after apply)
          ~ etag                        = "W/\"612ef42106ae1bee9eaa74a5d3a4afebd0858f16dc0bc8f52583818b4201e106\"" -> (known after apply)
          - fork                        = true -> null # forces replacement
          ~ full_name                   = "thebrowsercompany/swift-composable-architecture" -> (known after apply)
          ~ git_clone_url               = "git://github.com/thebrowsercompany/swift-composable-architecture.git" -> (known after apply)
          - has_discussions             = false -> null
          ~ has_downloads               = true -> false
          ~ html_url                    = "https://github.com/thebrowsercompany/swift-composable-architecture" -> (known after apply)
          ~ http_clone_url              = "https://github.com/thebrowsercompany/swift-composable-architecture.git" -> (known after apply)
          ~ id                          = "swift-composable-architecture" -> (known after apply)
          - is_template                 = false -> null
            name                        = "swift-composable-architecture"
          ~ node_id                     = "MDEwOlJlcG9zaXRvcnkzNTE3OTM0MjM=" -> (known after apply)
          ~ primary_language            = "Swift" -> (known after apply)
          ~ private                     = false -> (known after apply)
          ~ repo_id                     = 351793423 -> (known after apply)
          - source_owner                = "pointfreeco" -> null # forces replacement
          - source_repo                 = "swift-composable-architecture" -> null # forces replacement
          ~ ssh_clone_url               = "[email protected]:thebrowsercompany/swift-composable-architecture.git" -> (known after apply)
          ~ svn_url                     = "https://github.com/thebrowsercompany/swift-composable-architecture" -> (known after apply)
          ~ topics                      = [] -> (known after apply)
            # (18 unchanged attributes hidden)
    
          ~ security_and_analysis (known after apply)
          - security_and_analysis {
              - secret_scanning {
                  - status = "disabled" -> null
                }
              - secret_scanning_push_protection {
                  - status = "disabled" -> null
                }
            }
        }
    
    Plan: 1 to add, 0 to change, 1 to destroy.
    

    Is it possible there is still some issue with the forking logic?

  4. julianr-am commented on Dec 2, 2025

    @julianr-am
    Author

    Hey @julianr-am 👋

    Thanks for raising this.

    Based on the PR description I understand that using the fork attribute is only for creating a new fork. Existing forks should be recognised without doing anything.

    Are you able to confirm how this acts following the same steps without adding the fork attributes after upgrade?

    Hi @deiga ! Not adding the fork parameter results in a plan with no changes when using the example I listed to reproduce the issue.

    However we came across this issue because of a situation exactly like the one stated by the persone above. Some of our repositories reported that they would be recreated after we upgraded the provider due to this parameter not being set (I haven't been able to reproduce this yet). We tried setting it but it resulted in the issue I reported, where other repositories were planned to be recreated even though they were indeed a fork.

  5. deiga commented on Dec 2, 2025

    @deiga
    Collaborator

    Thank you for providing these details! That definitely looks like a nasty bug and shouldn't be happening.

  6. nickfloyd commented on Dec 2, 2025

    @nickfloyd
    Contributor

    This is no good. So sorry for the trouble here @julianr-am . I think I found the issue. ForceNew: true was added to fork, source_owner, and source_repo.

    If, like in your case, terraform detects a state change then it will force new the fields above. ForceNew should only apply when creating a fork and no other time.

    We need to remove ForceNew from these fields and probably add a CustomizeDiff to compare the schema resource diff.

    @deiga & @diofeher can y'all check me on this line of thinking?

  7. moved this from Backlog to In Progress in Terraform Provideron Dec 2, 2025
  8. added this to the v6.x version wrap up milestone on Dec 2, 2025
  9. removed
    Status: Triage
    on Dec 2, 2025
  10. self-assigned this
    on Dec 2, 2025
  11. mhegazy commented on Dec 3, 2025

    @mhegazy

    @nickfloyd - related to the comment above (#2954 (comment)). We are seeing another repo now considered by the provider to be deleted, same symptoms as the OP.

    The question is why the provider updates these at seemingly random times? I would assume that in one plan all repos that are marked as forks would be destroyed. but we are seeing it happen one repo every other day. any idea what is going on?

    here is the plan from today:

    Terraform planned the following actions, but then encountered a problem:
    
      # github_repository.github_repository["cancel-workflow-action"] must be replaced
    -/+ resource "github_repository" "github_repository" {
          - auto_init                   = false -> null
          ~ default_branch              = "main" -> (known after apply)
          ~ etag                        = "W/\"cbcef5b7bf01a773ebc13bdf0d0037fd0000fa71b95dc5e9a60f58942a43d15b\"" -> (known after apply)
          - fork                        = true -> null # forces replacement
          ~ full_name                   = "thebrowsercompany/cancel-workflow-action" -> (known after apply)
          ~ git_clone_url               = "git://github.com/thebrowsercompany/cancel-workflow-action.git" -> (known after apply)
          - has_discussions             = false -> null
          ~ html_url                    = "https://github.com/thebrowsercompany/cancel-workflow-action" -> (known after apply)
          ~ http_clone_url              = "https://github.com/thebrowsercompany/cancel-workflow-action.git" -> (known after apply)
          ~ id                          = "cancel-workflow-action" -> (known after apply)
          - is_template                 = false -> null
            name                        = "cancel-workflow-action"
          ~ node_id                     = "MDEwOlJlcG9zaXRvcnkzNDQwOTE1MjU=" -> (known after apply)
          + primary_language            = (known after apply)
          ~ private                     = false -> (known after apply)
          ~ repo_id                     = 344091525 -> (known after apply)
          - source_owner                = "pgalisz" -> null # forces replacement
          - source_repo                 = "cancel-workflow-action" -> null # forces replacement
          ~ ssh_clone_url               = "[email protected]:thebrowsercompany/cancel-workflow-action.git" -> (known after apply)
          ~ svn_url                     = "https://github.com/thebrowsercompany/cancel-workflow-action" -> (known after apply)
          ~ topics                      = [] -> (known after apply)
            # (21 unchanged attributes hidden)
    
          ~ security_and_analysis (known after apply)
          - security_and_analysis {
              - secret_scanning {
                  - status = "disabled" -> null
                }
              - secret_scanning_push_protection {
                  - status = "disabled" -> null
                }
            }
        }
    
    Plan: 1 to add, 0 to change, 1 to destroy.
    
    Error: Instance cannot be destroyed
    
      on github-repos.tf line 252:
     252: resource "github_repository" "github_repository" {
    
  12. diofeher commented on Dec 3, 2025

    @diofeher
    Collaborator

    @mhegazy That's strange, but your issue is the same as this one. We have an open PR at #2959 to fix this.

  13. moved this from In Progress to Done in Terraform Provideron Dec 3, 2025
  14. diofeher commented on Dec 3, 2025

    @diofeher
    Collaborator

    @mhegazy @julianr-am Can you upgrade your terraform-provider-github version and see if this issue persists?

  15. julianr-am commented on Dec 5, 2025

    @julianr-am
    Author

    @diofeher After using a locally built version of the provider (from the master branch) that includes this fix I can confirm that Terraform is no longer trying to re-create the resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type: Bug

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions