grafana/k6 · error

invalid stack URL: %w

Error message

invalid stack URL: %w

What it means

v6.Client.ValidateToken (internal/cloudapi/v6/api.go:193) rejects a stack URL that Go's url.Parse cannot parse before sending it as the X-Stack-Url header. url.Parse fails only on grossly malformed input — control characters, invalid bracket/percent syntax — so this error signals a corrupted configuration value, not a merely wrong hostname.

Source

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

		zones = append(zones, LoadZone{
			ID:           zone.Id,
			K6LoadZoneID: zone.K6LoadZoneId,
			Name:         zone.Name,
			Public:       zone.Public,
			Available:    zone.Available,
		})
	}

	return zones, nil
}

// ValidateToken validates the cloud authentication token.
func (c *Client) ValidateToken(ctx context.Context, stackURL string) (_ *k6cloud.AuthenticationResponse, err error) {
	if stackURL == "" {
		return nil, errors.New("stack URL is required to validate token")
	}
	if _, err := url.Parse(stackURL); err != nil {
		return nil, fmt.Errorf("invalid stack URL: %w", err)
	}

	res, hr, err := c.apiClient.AuthorizationAPI.
		Auth(c.authCtx(ctx)).
		XStackUrl(stackURL).
		Execute()
	defer closeResponse(hr, &err)

	if err := CheckResponse(hr, err); err != nil {
		return nil, err
	}
	if res == nil {
		return nil, errUnknown
	}

	return res, nil
}

View on GitHub (pinned to 93accf6570)

Solutions

  1. Print the URL with %q to reveal hidden characters and fix the config source
  2. Set a clean https://<stack-host> value with no trailing whitespace or newlines
  3. Validate with url.ParseRequestURI at config-load time to fail with a clearer message
  4. Prefer explicit flags/env over concatenated values when building the URL

Example fix

# before
export K6_CLOUD_HOST="https://my-stack.grafana.net "  # trailing space/newline

# after
export K6_CLOUD_HOST="https://my-stack.grafana.net"
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.ParseRequestURI(stackURL)
if err != nil || u.Scheme == "" || u.Host == "" {
	return fmt.Errorf("stack URL %q is not a valid absolute URL", stackURL)
}

Type guard

func isParsableURL(s string) bool {
	_, err := url.Parse(s)
	return err == nil
}

Try / catch

if _, err := v6Client.ValidateToken(ctx, stackURL); err != nil {
	if strings.Contains(err.Error(), "invalid stack URL") {
		log.Printf("stack URL %q is malformed; fix the config source", stackURL)
	}
	return err
}

Prevention

When it happens

Trigger: K6_CLOUD_HOST or the passed stackURL containing control characters, a newline from shell quoting, invalid '%' sequences, or malformed IPv6 bracket syntax.

Common situations: Copy-pasting URLs with embedded line breaks; env vars assembled by templating that inject stray characters; config values double-quoted or escaped incorrectly in CI.

Related errors


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