go-kratos/kratos · error

path invalid

Error message

path invalid

What it means

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.

Source

Thrown at contrib/config/consul/config.go:52

}

type source struct {
	client  *api.Client
	options *options
}

func New(client *api.Client, opts ...Option) (config.Source, error) {
	options := &options{
		ctx:  context.Background(),
		path: "",
	}

	for _, opt := range opts {
		opt(options)
	}

	if options.path == "" {
		return nil, errors.New("path invalid")
	}

	return &source{
		client:  client,
		options: options,
	}, nil
}

// Load return the config values
func (s *source) Load() ([]*config.KeyValue, error) {
	kv, _, err := s.client.KV().List(s.options.path, nil)
	if err != nil {
		return nil, err
	}

	pathPrefix := s.options.path
	if !strings.HasSuffix(s.options.path, "/") {
		pathPrefix = pathPrefix + "/"

View on GitHub (pinned to 668db92c2c)

Solutions

  1. Pass consul.WithPath with the KV prefix that holds your config, e.g. consul.New(cli, consul.WithPath("/kratos/app/config"))
  2. Confirm the prefix exists in Consul KV (consul kv get -recurse <path>) so Load returns values

Example fix

// before
src, err := consul.New(cli)
// err = path invalid

// after
src, err := consul.New(cli, consul.WithPath("/kratos/app/config"))
Defensive patterns

Strategy: validation

Validate before calling

// verify all required options before constructing
if path == "" {
    return fmt.Errorf("consul config source requires a KV path")
}
src, err := consul.New(cli, consul.WithPath(path))

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


AI-assisted analysis of go-kratos/kratos@668db92c2c (2026-08-16). Data as JSON: /api/errors/02cae8dc7d8d0597. Report an issue: GitHub.