{"record":{"id":"02cae8dc7d8d0597","repo":"go-kratos/kratos","slug":"path-invalid","errorCode":null,"errorMessage":"path invalid","messagePattern":"path invalid","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"contrib/config/consul/config.go","lineNumber":52,"sourceCode":"}\n\ntype source struct {\n\tclient  *api.Client\n\toptions *options\n}\n\nfunc New(client *api.Client, opts ...Option) (config.Source, error) {\n\toptions := &options{\n\t\tctx:  context.Background(),\n\t\tpath: \"\",\n\t}\n\n\tfor _, opt := range opts {\n\t\topt(options)\n\t}\n\n\tif options.path == \"\" {\n\t\treturn nil, errors.New(\"path invalid\")\n\t}\n\n\treturn &source{\n\t\tclient:  client,\n\t\toptions: options,\n\t}, nil\n}\n\n// Load return the config values\nfunc (s *source) Load() ([]*config.KeyValue, error) {\n\tkv, _, err := s.client.KV().List(s.options.path, nil)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tpathPrefix := s.options.path\n\tif !strings.HasSuffix(s.options.path, \"/\") {\n\t\tpathPrefix = pathPrefix + \"/\"","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/go-kratos/kratos/blob/668db92c2c001e9552594ba5a8aede8456af6d7e/contrib/config/consul/config.go#L34-L70","documentation":"Returned by consul.New in the consul config source (contrib/config/consul/config.go:52). The constructor initializes path:\"\" and only the WithPath option sets it; if no path was supplied the source has no KV prefix to list, so New rejects construction with this error. It is a constructor-time misconfiguration, not a runtime Consul failure.","triggerScenarios":"Calling config source constructor as consul.New(consulClient) with no options, or passing options that do not include consul.WithPath. Any option combination where options.path stays empty fails; consul.WithPath(\"/kratos/app\") is mandatory.","commonSituations":"Copy-pasted example code that omitted the WithPath line; refactoring that accidentally dropped the option; assuming the path is derived from the Consul client config (it is not - it is a separate option).","solutions":["Pass consul.WithPath with the KV prefix that holds your config, e.g. consul.New(cli, consul.WithPath(\"/kratos/app/config\"))","Confirm the prefix exists in Consul KV (consul kv get -recurse <path>) so Load returns values"],"exampleFix":"// before\nsrc, err := consul.New(cli)\n// err = path invalid\n\n// after\nsrc, err := consul.New(cli, consul.WithPath(\"/kratos/app/config\"))","handlingStrategy":"validation","validationCode":"// verify all required options before constructing\nif path == \"\" {\n    return fmt.Errorf(\"consul config source requires a KV path\")\n}\nsrc, err := consul.New(cli, consul.WithPath(path))","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat WithPath as mandatory boilerplate for the consul source - code-review for its presence","Keep one helper that builds config sources so the option cannot be forgotten","Add a unit test asserting New succeeds with your production option set"],"tags":["config","consul","constructor-validation"],"backgroundTag":null,"analyzedSha":"668db92c2c001e9552594ba5a8aede8456af6d7e","analyzedAt":"2026-08-16T02:07:20.704Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}