gitlab_project with changed default_branch but not initialize_with_readme lead to drift and errors

Bug Report

default_branch on the GitLab Projects API requires initialize_with_readme to be set to true (https://docs.gitlab.com/ee/api/projects.html#create-project). The Terraform providers allows default_branch to be defined without initialize_with_readme. Allowing this combination is useful as this is the "expected" state for an imported project.

However, for new projects the provider detects that the default branch for an empty repository is master during refreshes (even if the instance's default branch for new projects is different). The Web UI displays:

A default branch cannot be chosen for an empty project. 

This creates a drift and Terraform will try to correct the default_branch to the right value. Applying cannot be done since it's not possible to set a default branch when the project is still empty.

Expected behavior: Since creation is successful, there should not be a drift while the repository is empty. Or if drift is detected, applying should not return a 400.

Relevant Terraform Configuration

resource "gitlab_project" "example" {
  name        = "example"
  description = "example"

  default_branch = "main" // Or anything that is not master
}

Relevant Terraform Command

N/A: Can plan/apply as normally

Relevant Log Output

~ default_branch                                   = "master" -> "main"
│ Error: PUT https://gitlab.url/api/v4/projects/1: 400 {message: {base: [Could not change HEAD: branch 'main' does not exist]}}

Additional Details

  • GitLab Terraform Provider Version: 16.0.3
  • GitLab Version: v16.0.4
  • Terraform Version: 1.5.4
Edited by Sayrus