{"record":{"id":"60bfc7d5c58f407d","repo":"vitessio/vitess","slug":"cannot-create-local-vtctldclient-without-a-server","errorCode":null,"errorMessage":"cannot create local vtctldclient without a server; call SetServer first","messagePattern":"cannot create local vtctldclient without a server; call SetServer first","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtctl/localvtctldclient/client.go","lineNumber":67,"sourceCode":"}\n\n// SetServer sets the server implementation used when creating local clients\n// via the vtctldclient.Factory.\n//\n// This function must be called before calling vtctldclient.New.\nfunc SetServer(s vtctlservicepb.VtctldServer) {\n\tm.Lock()\n\tdefer m.Unlock()\n\n\tserver = s\n}\n\nfunc localVtctldClientFactory(ctx context.Context, addr string) (vtctldclient.VtctldClient, error) {\n\tm.Lock()\n\tdefer m.Unlock()\n\n\tif server == nil {\n\t\treturn nil, errors.New(\"cannot create local vtctldclient without a server; call SetServer first\")\n\t}\n\n\treturn New(server), nil\n}\n\nfunc init() {\n\tvtctldclient.Register(\"local\", localVtctldClientFactory)\n}\n","sourceCodeStart":49,"sourceCodeEnd":76,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtctl/localvtctldclient/client.go#L49-L76","documentation":"localVtctldClientFactory builds an in-process vtctld client wired directly to a registered VtctldServer. Because there is no gRPC connection to dial (the addr argument is ignored), the factory fails if no server was previously installed via SetServer, preventing a silently useless client.","triggerScenarios":"Creating a vtctldclient with the 'local' protocol scheme before test/setup code has called localvtctldclient.SetServer(server).","commonSituations":"Unit tests constructing a local client before spinning up the fake/mock VtctldServer; initialization-order bugs where the client factory runs before test fixtures register the server.","solutions":["Call localvtctldclient.SetServer(vtctldServer) before any code creates the local client","Fix initialization order so server registration happens in a setup step (or init/TestMain) prior to client creation","If a real vtctld is intended, use the gRPC scheme with a proper vtctld address instead of the local scheme"],"exampleFix":"// before\nclient, err := vtctldclient.New(\"local\")\n// after\nlocalvtctldclient.SetServer(fakeVtctldServer)\nclient, err := vtctldclient.New(\"local\")","handlingStrategy":"validation","validationCode":"if localvtctldclient.Server() == nil {\n    localvtctldclient.SetServer(vtctldSrv)\n}\nclient, err := vtctldclient.New(\"local\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Register the server in TestMain or a shared setup helper before any client creation","Centralize local client construction in one helper that guarantees SetServer was called","Use the gRPC scheme with a real address outside of tests"],"tags":["vtctld","local-client","initialization","testing"],"backgroundTag":"uninitialized-server","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}