grafana/k6 · error

unexpected nil response from CreateOrFindLoadTest

Error message

unexpected nil response from CreateOrFindLoadTest

What it means

A defensive guard in CreateOrFindLoadTest: the generated OpenAPI client's getters (res.GetId()) return zero values when called on a nil receiver, so if Execute() returned (nil, nil) the client would silently report a load-test ID of 0 and later calls would target a nonexistent test. This error converts that silent corruption into a loud failure.

Source

Thrown at internal/cloudapi/v6/api.go:271

		Name(name).
		Execute()
	defer closeResponse(hr, &err)

	if err := CheckResponse(hr, err); err != nil {
		var rerr ResponseError
		if errors.As(err, &rerr) && rerr.Response != nil && rerr.Response.StatusCode == http.StatusConflict {
			lt, err := c.findTestByName(ctx, projectID, name)
			if err != nil {
				return 0, err
			}
			return lt.GetId(), nil
		}
		return 0, err
	}
	if res == nil {
		// The SDK getters return zero values on a nil receiver, so guard
		// against silently producing a bogus 0 id and report it clearly.
		return 0, errors.New("unexpected nil response from CreateOrFindLoadTest")
	}

	return res.GetId(), nil
}

// UploadTest creates or updates a cloud load test's script.
func (c *Client) UploadTest(
	ctx context.Context, name string, projectID int64, arc *lib.Archive,
) (*k6cloud.LoadTestApiModel, error) {
	lt, err := c.createTest(ctx, name, projectID, arc)
	if err == nil {
		return lt, nil
	}
	var rerr ResponseError
	if !errors.As(err, &rerr) || rerr.Response == nil || rerr.Response.StatusCode != http.StatusConflict {
		return nil, err
	}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Retry the `k6 cloud run` command; if it persists, capture debug HTTP output
  2. If you maintain a custom transport/mock, make sure 2xx responses include a valid JSON body the SDK can deserialize
  3. Upgrade k6 so the vendored SDK version matches the API contract
  4. Report to k6 maintainers with the exact k6 version and response body
Defensive patterns

Strategy: type-guard

Type guard

// The SDK getters return zero values on a nil receiver, so callers that
// bypass this client must nil-check the model before reading fields.
func validLoadTest(lt *k6cloud.LoadTestLightWithLatest) bool {
    return lt != nil && lt.Id != nil && *lt.Id > 0
}

Try / catch

id, err := client.CreateOrFindLoadTest(ctx, name, projectID, arc)
if err != nil {
    if strings.Contains(err.Error(), "unexpected nil response") {
        // SDK/transport anomaly: safe to retry the create-or-find once
        id, err = client.CreateOrFindLoadTest(ctx, name, projectID, arc)
    }
    if err != nil {
        return 0, err
    }
}

Prevention

When it happens

Trigger: The k6-cloud-openapi-client-go LoadTestsCreate call returns err == nil but a nil *LoadTestLightWithLatest response. Realistically only seen with custom/mock HTTP transports, SDK deserialization edge cases, or a patched/older SDK version.

Common situations: Unit tests with mocked cloud API transports that forget to return a body; running a k6 build against a forked k6-cloud-openapi-client-go; extremely rare SDK/transport anomalies against the real API.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/3fe6636ecfa09a1d. Report an issue: GitHub.