grafana/k6 · error

missing init environment

Error message

missing init environment

What it means

After passing the init-context check, grpc Client.Load requires a non-nil init environment (vu.InitEnv()) to resolve import paths and the file filesystem. If the VU was created without an InitEnv -- typical in unit tests, embedded runners, or extensions that fabricate VUs -- loading fails with this error.

Source

Thrown at internal/js/modules/k6/grpc/client.go:54

type Client struct {
	mds  map[string]protoreflect.MethodDescriptor
	conn *grpcext.Conn
	vu   modules.VU
	addr string

	types    *protoregistry.Types
	typesMtx sync.Mutex
}

// Load will parse the given proto files and make the file descriptors available to request.
func (c *Client) Load(importPaths []string, filenames ...string) ([]MethodInfo, error) {
	if c.vu.State() != nil {
		return nil, errors.New("load must be called in the init context")
	}

	initEnv := c.vu.InitEnv()
	if initEnv == nil {
		return nil, errors.New("missing init environment")
	}

	// If no import paths are specified, use the current working directory
	if len(importPaths) == 0 {
		importPaths = append(importPaths, initEnv.CWD.Path)
	}

	for i, s := range importPaths {
		// Clean file scheme as it is the only supported scheme and the following APIs do not support them
		importPaths[i] = strings.TrimPrefix(s, "file://")
	}

	resolver := protocompile.WithStandardImports(&protocompile.SourceResolver{
		ImportPaths: importPaths,
		Accessor: func(filename string) (io.ReadCloser, error) {
			absFilePath := initEnv.GetAbsFilePath(filename)
			return initEnv.FileSystems["file"].Open(absFilePath)
		},

View on GitHub (pinned to 93accf6570)

Solutions

  1. In Go tests, populate the VU's InitEnvField with a real *lib.InitEnv (including FileSystems["file"])
  2. Ensure Load is called during the init phase where the runtime provides the environment
  3. Report a clearer upstream error if you wrap this API in an extension

Example fix

// Go test harness: before, InitEnvField is nil -> "missing init environment"
// after
runtime.VU.InitEnvField = &lib.InitEnv{
    CWD:        &url.URL{Scheme: "file", Path: "/tmp/project/"},
    FileSystems: map[string]fsext.Fs{"file": fsext.NewOsFs()},
}
Defensive patterns

Strategy: validation

Validate before calling

// Go: guard before calling
if vu.InitEnv() == nil {
    return fmt.Errorf("cannot load protos: VU has no init environment (init phase required)")
}
client.Load(importPaths, files...)

Prevention

When it happens

Trigger: Calling Load on a Client whose VU has no InitEnv: constructing the module/VU in Go tests without lib.InitEnv, or invoking Load in a custom execution context that skipped init.

Common situations: Extension authors writing unit tests for gRPC helpers; runners that only exercise the VU phase; in normal JS scripts this is nearly unreachable because the state check fires first.

Related errors


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