grafana/k6 · error

missing init environment

Error message

missing init environment

What it means

After grpc Client.load() passes the init-context check (State() is nil), it still needs the init environment for the working directory and file systems to resolve proto import paths. If c.vu.InitEnv() is nil there is no CWD and no file system registry to read from, so load fails with this error. In a normal k6 run State()==nil implies a valid InitEnv; hitting both conditions means the module is executing somewhere the init environment was never attached.

Solutions

  1. Call client.load() only from top-level (init) code of the main test script in a standard k6 run.
  2. If embedding k6, ensure the VU implementation returns a non-nil InitEnv() during the init phase.
  3. Upgrade k6 to a current release to pick up init-environment propagation fixes.
Defensive patterns

Strategy: try-catch

Validate before calling

// nothing to validate from JS - guard the call and report
if (!client.load('./proto/', 'svc.proto')) { /* not applicable: load throws */ }

Try / catch

try {
  client.load('./proto/', 'svc.proto');
} catch (e) {
  if (/missing init environment/.test(e.message)) {
    throw new Error('client.load() must run in the init context of a standard k6 run');
  }
  throw e;
}

Prevention

When it happens

Trigger: client.load() invoked from a context that is neither a live VU nor a properly initialized init context: handleSummary(), module code evaluated outside the script's init phase, embedded k6 usage in Go tests/binary extensions that construct the module instance without an init environment, or bugs in older k6 versions where InitEnv was not propagated to the module.

Common situations: Custom k6 forks/binaries that instantiate the grpc module manually; calling client.load() from within handleSummary or other late lifecycle hooks; edge cases in k6 versions with init-env propagation regressions.

Related errors


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

Appendix: source

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

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 8d06114777)