Skip to content

Misleading error message when GITHUB_TOKEN not set #598

Closed

Description

@lukiffer

When configuring the provider with only the organization property and relying on the GITHUB_TOKEN environment variable for authentication, if the environment variable hasn't been set, apply and import operations display the following error message:

Error: This resource can only be used in the context of an organization, "" is a user.

This is misleading as the actual error is a missing credential.

After setting the environment variable, everything works as expected.

Terraform Version

0.13.5

Affected Resource(s)

Have only reproduced this with github_membership and github_team, but it probably applies to more.

Terraform Configuration Files

terraform {
  required_version = "=0.13.5"

  required_providers {
    github = {
      source  = "hashicorp/github"
      version = "=4.0.0"
    }
  }
}

provider "github" {
  version = "=4.0.0"
  organization = "example-org"
}

resource "github_membership" "member" {
  username = "lukiffer"
  role     = "admin"
}

Expected Behavior

A more accurate error message should be displayed, like Error: Missing GITHUB_TOKEN environment variable.

Actual Behavior

I received the error message Error: This resource can only be used in the context of an organization, "" is a user. and subsequently spent an hour debugging the provider config trying to figure out why when I supply organization it's being interpreted as an empty string.

Steps to Reproduce

  • terraform apply; or
  • terraform import github_membership.member "example-org:lukiffer"

Activity

  1. added
    Type: Bug
    on Nov 24, 2020
  2. JeroenRijks commented on Jan 26, 2021

    @JeroenRijks

    Has anyone got a workaround for this issue?

  3. jcudit commented on Jan 26, 2021

    @jcudit
    Contributor

    Does this relate to the fix that was shipped in v4.3.1? Can anyone try out this release and confirm?

  4. dgteixeira commented on Sep 23, 2021

    @dgteixeira

    Hello everyone, I hope you are all well :)
    @jcudit, I have tried and I have a similar problem when my GITHUB_TOKEN is expired or already inactive.

    Terraform version: 1.0.7;
    Github Provider version: v4.14.0

    With the following terraform code, I get a Error: Plugin did not respond error

    terraform {
      required_version = ">= 1.0.0"
    
      required_providers {
        github = {
          source  = "integrations/github"
          version = "~> 4.14.0"
        }
      }
    }
    
    provider "github" {
      owner = var.org_name
    }
    
    data "github_organization" "org_name" {
      name = var.org_name
    }
    
    resource "github_membership" "members" {
      username = foobar
      role          = "member"
    }
    

    Full error:

    │ Error: Plugin did not respond
    │
    │   with data.github_organization.org_name,
    │   on main.tf line 23, in data "github_organization" "org_name":
    │   23: data "github_organization" "org_name" {
    │
    │ The plugin encountered an error, and failed to respond to the plugin.(*GRPCProvider).ReadDataSource call. The plugin logs may contain more details.
    ╵
    ╷
    │ Error: This resource can only be used in the context of an organization, "" is a user.
    │
    │   with github_membership.members["foobar"],
    │    in resource "github_membership" "members":
    │   XX: resource "github_membership" "members" {
    │
    ╵
    

    The plugin error, checking DEBUG logs is as follows:

    Stack trace from the terraform-provider-github_v4.14.0 plugin:
    
    panic: runtime error: invalid memory address or nil pointer dereference
    [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xd46d11]
    
    goroutine 57 [running]:
    github.com/terraform-providers/terraform-provider-github/github.dataSourceGithubOrganizationRead(0xc00074d7a0, 0xe1f4c0, 0xc000045d80, 0xc00074d7a0, 0x0)
            github.com/terraform-providers/terraform-provider-github/github/data_source_github_organization.go:120 +0x8b1
    github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Resource).ReadDataApply(0xc00057a280, 0xc0004c82e0, 0xe1f4c0, 0xc000045d80, 0xc000585030, 0x1, 0x0)
            github.com/hashicorp/[email protected]/helper/schema/resource.go:398 +0x91
    github.com/hashicorp/terraform-plugin-sdk/helper/schema.(*Provider).ReadDataApply(0xc00057aa80, 0xc0006a19a8, 0xc0004c82e0, 0xc0004c82e0, 0x0, 0x0)
            github.com/hashicorp/[email protected]/helper/schema/provider.go:451 +0x8f
    github.com/hashicorp/terraform-plugin-sdk/internal/helper/plugin.(*GRPCProviderServer).ReadDataSource(0xc000214598, 0x1169bb0, 0xc00030ede0, 0xc0007ae480, 0xc000214598, 0xc00030ede0, 0xc00008ea50)
            github.com/hashicorp/[email protected]/internal/helper/plugin/grpc_provider.go:1036 +0x42d
    github.com/hashicorp/terraform-plugin-sdk/internal/tfplugin5._Provider_ReadDataSource_Handler(0xf97c40, 0xc000214598, 0x1169bb0, 0xc00030ede0, 0xc0002fc240, 0x0, 0x1169bb0, 0xc00030ede0, 0xc0007241c0, 0x69)
            github.com/hashicorp/[email protected]/internal/tfplugin5/tfplugin5.pb.go:3225 +0x214
    google.golang.org/grpc.(*Server).processUnaryRPC(0xc000480000, 0x11713d8, 0xc00043e780, 0xc000190300, 0xc0000f6b10, 0x16d8b10, 0x0, 0x0, 0x0)
            google.golang.org/[email protected]/server.go:995 +0x482
    google.golang.org/grpc.(*Server).handleStream(0xc000480000, 0x11713d8, 0xc00043e780, 0xc000190300, 0x0)
            google.golang.org/[email protected]/server.go:1275 +0xd2c
    google.golang.org/grpc.(*Server).serveStreams.func1.1(0xc0000344d0, 0xc000480000, 0x11713d8, 0xc00043e780, 0xc000190300)
            google.golang.org/[email protected]/server.go:710 +0xab
    created by google.golang.org/grpc.(*Server).serveStreams.func1
            google.golang.org/[email protected]/server.go:708 +0xa5
    
    Error: The terraform-provider-github_v4.14.0 plugin crashed!
    
    This is always indicative of a bug within the plugin. It would be immensely
    helpful if you could report the crash with the plugin's maintainers so that it
    can be fixed. The output above should help diagnose the issue.
    

    When I setup my env var GITHUB_TOKEN correctly with a permission-sufficient PAT , it works without problem.
    This shouldn't crash the plugin, only show an error like @lukiffer has mentioned:

    Error: Missing GITHUB_TOKEN environment variable.

    Thanks!

  5. github-actions commented on Dec 7, 2022

    @github-actions

    👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

  6. added
    Status: Stale
    on Dec 7, 2022
  7. ulfjack commented on Dec 7, 2022

    @ulfjack

    We're seeing this (or a similar issue) with 5.7.0. In our case, we're seeing hangs when someone doesn't have the GITHUB_TOKEN environment variable set, and we're seeing a large number of added resources if they do but missing scopes necessary to create those resources. It's a very bad user experience.

    Also, dear bot, we can't add the Status: Pinned label.

  8. added
    Status: Pinned
    and removed
    Status: Stale
    on Dec 8, 2022
  9. kfcampbell commented on Dec 8, 2022

    @kfcampbell
    Contributor

    Thanks for the notice, I've pinned the issue and I'll look into why label permissions aren't working.

    PRs for adding appropriate error messages are appreciated!

  10. ulfjack commented on Dec 9, 2022

    @ulfjack

    Thanks! We're generally happy to send PRs, but given that we don't currently see an error message in the first place, we're not entirely sure where to start.

  11. kfcampbell commented on Dec 9, 2022

    @kfcampbell
    Contributor

    The hanging thing is very weird. The error message in the original issue is found here, which might help.

  12. deiga commented on Jan 2, 2026

    @deiga
    Collaborator

    @ulfjack Are you still encountering this hanging issue?

  13. ulfjack commented on Jan 24, 2026

    @ulfjack

    To be fair, we're still on 5.29.0.

    Expired credentials:

    ╷
    │ Error: GET https://api.github.com/<redacted>: 401 Bad credentials []
    │ 
    │   with module.<redacted>,
    │   on ../modules/repository/main.tf line 15, in resource "github_repository" "repo":
    │   15: resource "github_repository" "repo" {
    │ 
    ╵
    

    Incorrect credentials:

    ╷
    │ Error: Your token has not been granted the required scopes to execute this query. The 'email' field requires one of the following scopes: <redacted>, but your token has only been granted the: <redacted> scopes. Please modify your token's scopes at: https://github.com/settings/tokens.
    │ 
    │   with data.github_organization.main,
    │   on main.tf line 1, in data "github_organization" "main":
    │    1: data "github_organization" "main" {
    │ 
    ╵
    

    No credentials:

    data.github_organization.main: Still reading... [10s elapsed]
    data.github_organization.main: Still reading... [20s elapsed]
    data.github_organization.main: Still reading... [30s elapsed]
    data.github_organization.main: Still reading... [40s elapsed]
    data.github_organization.main: Still reading... [50s elapsed]
    data.github_organization.main: Still reading... [1m0s elapsed]
    data.github_organization.main: Still reading... [1m10s elapsed]
    data.github_organization.main: Still reading... [1m20s elapsed]
    data.github_organization.main: Still reading... [1m30s elapsed]
    data.github_organization.main: Still reading... [1m40s elapsed]
    data.github_organization.main: Still reading... [1m50s elapsed]
    data.github_organization.main: Still reading... [2m0s elapsed]
    data.github_organization.main: Still reading... [2m10s elapsed]
    data.github_organization.main: Still reading... [2m20s elapsed]
    data.github_organization.main: Still reading... [2m30s elapsed]
    data.github_organization.main: Still reading... [2m40s elapsed]
    data.github_organization.main: Still reading... [2m50s elapsed]
    data.github_organization.main: Still reading... [3m0s elapsed]
    data.github_organization.main: Still reading... [3m10s elapsed]
    data.github_organization.main: Still reading... [3m20s elapsed]
    data.github_organization.main: Still reading... [3m30s elapsed]
    data.github_organization.main: Still reading... [3m40s elapsed]
    data.github_organization.main: Still reading... [3m50s elapsed]
    data.github_organization.main: Still reading... [4m0s elapsed]
    data.github_organization.main: Still reading... [4m10s elapsed]
    data.github_organization.main: Still reading... [4m20s elapsed]
    data.github_organization.main: Still reading... [4m30s elapsed]
    ^C
    Interrupt received.
    Please wait for Terraform to exit or data loss may occur.
    Gracefully shutting down...
    
    Stopping operation...
    data.github_organization.main: Still reading... [4m40s elapsed]
    data.github_organization.main: Still reading... [4m50s elapsed]
    ^C
    Two interrupts received. Exiting immediately. Note that data loss may have occurred.
    
    ╷
    │ Error: operation canceled
    │ 
    │ 
    ╵
    
  14. deiga commented on Jan 24, 2026

    @deiga
    Collaborator

    @ulfjack that's quite far from latest version.

    Are you able to reproduce this with latest version? And could you provide DEBUG logs the next time?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    AuthenticationProviderStatus: PinnedType: Bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions