{"record":{"id":"30d154439729dd0a","repo":"grafana/k6","slug":"missing-init-environment","errorCode":null,"errorMessage":"missing init environment","messagePattern":"missing init environment","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/grpc/client.go","lineNumber":57,"sourceCode":"type Client struct {\n\tmds  map[string]protoreflect.MethodDescriptor\n\tconn *grpcext.Conn\n\tvu   modules.VU\n\taddr string\n\n\ttypes    *protoregistry.Types\n\ttypesMtx sync.Mutex\n}\n\n// Load will parse the given proto files and make the file descriptors available to request.\nfunc (c *Client) Load(importPaths []string, filenames ...string) ([]MethodInfo, error) {\n\tif c.vu.State() != nil {\n\t\treturn nil, errors.New(\"load must be called in the init context\")\n\t}\n\n\tinitEnv := c.vu.InitEnv()\n\tif initEnv == nil {\n\t\treturn nil, errors.New(\"missing init environment\")\n\t}\n\n\t// If no import paths are specified, use the current working directory\n\tif len(importPaths) == 0 {\n\t\timportPaths = append(importPaths, initEnv.CWD.Path)\n\t}\n\n\tfor i, s := range importPaths {\n\t\t// Clean file scheme as it is the only supported scheme and the following APIs do not support them\n\t\timportPaths[i] = strings.TrimPrefix(s, \"file://\")\n\t}\n\n\tresolver := protocompile.WithStandardImports(&protocompile.SourceResolver{\n\t\tImportPaths: importPaths,\n\t\tAccessor: func(filename string) (io.ReadCloser, error) {\n\t\t\tabsFilePath := initEnv.GetAbsFilePath(filename)\n\t\t\treturn initEnv.FileSystems[\"file\"].Open(absFilePath)\n\t\t},","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/grafana/k6/blob/8d06114777191d21ef885d05c5f8aa9e1ed838ae/internal/js/modules/k6/grpc/client.go#L39-L75","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Call client.load() only from top-level (init) code of the main test script in a standard k6 run.","If embedding k6, ensure the VU implementation returns a non-nil InitEnv() during the init phase.","Upgrade k6 to a current release to pick up init-environment propagation fixes."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// nothing to validate from JS - guard the call and report\nif (!client.load('./proto/', 'svc.proto')) { /* not applicable: load throws */ }","typeGuard":null,"tryCatchPattern":"try {\n  client.load('./proto/', 'svc.proto');\n} catch (e) {\n  if (/missing init environment/.test(e.message)) {\n    throw new Error('client.load() must run in the init context of a standard k6 run');\n  }\n  throw e;\n}","preventionTips":["Call client.load() from top-level script code only.","On custom/embedded k6 builds, verify InitEnv is provided during init."],"tags":["k6","grpc","init-environment","internal","load"],"backgroundTag":"k6-init-context-violation","analyzedSha":"8d06114777191d21ef885d05c5f8aa9e1ed838ae","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}