instructure/canvas-lms · error · ImportError

Invalid role '# ' for admin

Error message

Invalid role '#{role}' for admin

What it means

Raised by SIS::AdminImporter#process_admin when a `role` name was supplied but get_role could not resolve it to an existing role in the account. The role string must match a real role (e.g. AccountAdmin or a custom role name) in the target account.

Solutions

  1. Check the exact role name in the target account (account.roles.pluck(:name)) and use it verbatim.
  2. Create the custom role in the account before importing admins that reference it.
  3. Use role_id: with the role's id instead of the name to avoid naming mismatches.

Example fix

// before
process_admin(user_id: 'u1', account_id: 'a1', role: 'Admin', status: 'active')
// after
process_admin(user_id: 'u1', account_id: 'a1', role: 'AccountAdmin', status: 'active')
Defensive patterns

Strategy: validation

Validate before calling

role = account.roles.find_by(name: role_name)
raise ArgumentError, "role #{role_name.inspect} not found in account" unless role

Try / catch

begin
  importer.process_admin(user_id:, account_id:, role:, status:)
rescue SIS::ImportError => e
  errors << { row: row_no, message: e.message }
end

Prevention

When it happens

Trigger: role: 'Admin' or 'account_admin' when the account's role is named differently; referencing a custom role that does not exist in that account or was renamed.

Common situations: Custom role names differ between environments (dev vs prod); renamed/deleted custom roles; case or spacing mismatches in the role column; using built-in display names not the role's machine name.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of instructure/canvas-lms@1c9f0bb801 (2026-09-15). Data as JSON: /api/errors/a4c85c1cb9c5e268. Report an issue: GitHub.

Appendix: source

Thrown at lib/sis/admin_importer.rb:75

        @account_users_to_update_associations = Set.new
        @account_users_to_set_batch_id = Set.new
        @account_roles_by_account_id = {}
      end

      def process_admin(user_id: nil, account_id: nil, role_id: nil, role: nil, status: nil, root_account: nil)
        raise ImportError, "No user_id given for admin" if user_id.blank?
        raise ImportError, "No status given for admin" if status.blank?
        raise ImportError, "No role_id or role given for admin" if role.blank? && role_id.blank?

        state = status.downcase.strip
        raise ImportError, "Invalid status #{status} for admin" unless %w[active deleted].include? state
        return if @batch.skip_deletes? && state == "deleted"

        get_account(account_id)
        raise ImportError, "Invalid account_id given for admin" unless @account

        get_role(role_id, role)
        raise ImportError, "Invalid role '#{role}' for admin" if role.present? && !@role
        raise ImportError, "Invalid role_id '#{role_id}' for admin" if role_id.present? && !@role

        the_root_account = root_account_from_id(root_account) if root_account
        raise ImportError, "Invalid or unknown user_id '#{user_id}' for admin" if root_account && !the_root_account

        the_root_account ||= @root_account

        user = get_user(user_id, the_root_account)
        raise ImportError, "Invalid or unknown user_id '#{user_id}' for admin" unless user

        if state == "deleted" && user.id == @batch&.user_id && @account == @root_account
          raise ImportError, "Can't remove yourself user_id '#{user_id}'"
        end

        create_or_find_admin(user, state)
        user.clear_adminable_accounts_cache!
        @success_count += 1
      end

View on GitHub (pinned to 1c9f0bb801)