func (r *resolver) findDescriptor(scope protoreflect.FullName, ref partialName) (protoreflect.Descriptor, error) {
	if !ref.IsValid() {
		return nil, errors.New("invalid name reference: %q", ref)
	}
	if ref.IsFull() {
		scope, ref = "", ref[1:]
	}
	var foundButNotImported protoreflect.Descriptor
	for {
		s := protoreflect.FullName(ref)
		if scope != "" {
			s = scope + "." + s
		}
		if d, ok := r.local[s]; ok {
			return d, nil
		}
		d, err := r.remote.FindDescriptorByName(s)
		if err == nil {
			if r.imports[d.ParentFile().Path()] {
				return d, nil
			}
			foundButNotImported = d
		} else if err != protoregistry.NotFound {
			return nil, errors.Wrap(err, "%q", s)
		}
		if scope == "" {
			if d := foundButNotImported; d != nil {
				return nil, errors.New("resolved %q, but %q is not imported", d.FullName(), d.ParentFile().Path())
			}
			return nil, protoregistry.NotFound
		}
		scope = scope.Parent()
	}
}
findDescriptor finds the descriptor by name,
which may be a relative name within some scope.

Suppose the scope was "fizz.buzz" and the reference was "Foo.Bar",
then the following full names are searched:
* fizz.buzz.Foo.Bar
* fizz.Foo.Bar
* Foo.Bar
package protodesc
import (
	"google.golang.org/protobuf/internal/encoding/defval"
	"google.golang.org/protobuf/internal/errors"
	"google.golang.org/protobuf/internal/filedesc"
	"google.golang.org/protobuf/reflect/protoreflect"
	"google.golang.org/protobuf/reflect/protoregistry"
	"google.golang.org/protobuf/types/descriptorpb"
)
type resolver struct {
	local   descsByName
	remote  Resolver
	imports importSet
	allowUnresolvable bool
}
func (r *resolver) resolveMessageDependencies(ms []filedesc.Message, mds []*descriptorpb.DescriptorProto) (err error) {
	for i, md := range mds {
		m := &ms[i]
		for j, fd := range md.GetField() {
			f := &m.L2.Fields.List[j]
			if f.L1.Cardinality == protoreflect.Required {
				m.L2.RequiredNumbers.List = append(m.L2.RequiredNumbers.List, f.L1.Number)
			}
			if fd.OneofIndex != nil {
				k := int(fd.GetOneofIndex())
				if !(0 <= k && k < len(md.GetOneofDecl())) {
					return errors.New("message field %q has an invalid oneof index: %d", f.FullName(), k)
				}
				o := &m.L2.Oneofs.List[k]
				f.L1.ContainingOneof = o
				o.L1.Fields.List = append(o.L1.Fields.List, f)
			}
			if f.L1.Kind, f.L1.Enum, f.L1.Message, err = r.findTarget(f.Kind(), f.Parent().FullName(), partialName(fd.GetTypeName()), f.IsWeak()); err != nil {
				return errors.New("message field %q cannot resolve type: %v", f.FullName(), err)
			}
			if fd.DefaultValue != nil {
				v, ev, err := unmarshalDefault(fd.GetDefaultValue(), f, r.allowUnresolvable)
				if err != nil {
					return errors.New("message field %q has invalid default: %v", f.FullName(), err)
				}
				f.L1.Default = filedesc.DefaultValue(v, ev)
			}
		}
		if err := r.resolveMessageDependencies(m.L1.Messages.List, md.GetNestedType()); err != nil {
			return err
		}
		if err := r.resolveExtensionDependencies(m.L1.Extensions.List, md.GetExtension()); err != nil {
			return err
		}
	}
	return nil
}
func (r *resolver) resolveExtensionDependencies(xs []filedesc.Extension, xds []*descriptorpb.FieldDescriptorProto) (err error) {
	for i, xd := range xds {
		x := &xs[i]
		if x.L1.Extendee, err = r.findMessageDescriptor(x.Parent().FullName(), partialName(xd.GetExtendee()), false); err != nil {
			return errors.New("extension field %q cannot resolve extendee: %v", x.FullName(), err)
		}
		if x.L1.Kind, x.L2.Enum, x.L2.Message, err = r.findTarget(x.Kind(), x.Parent().FullName(), partialName(xd.GetTypeName()), false); err != nil {
			return errors.New("extension field %q cannot resolve type: %v", x.FullName(), err)
		}
		if xd.DefaultValue != nil {
			v, ev, err := unmarshalDefault(xd.GetDefaultValue(), x, r.allowUnresolvable)
			if err != nil {
				return errors.New("extension field %q has invalid default: %v", x.FullName(), err)
			}
			x.L2.Default = filedesc.DefaultValue(v, ev)
		}
	}
	return nil
}
func (r *resolver) resolveServiceDependencies(ss []filedesc.Service, sds []*descriptorpb.ServiceDescriptorProto) (err error) {
	for i, sd := range sds {
		s := &ss[i]
		for j, md := range sd.GetMethod() {
			m := &s.L2.Methods.List[j]
			m.L1.Input, err = r.findMessageDescriptor(m.Parent().FullName(), partialName(md.GetInputType()), false)
			if err != nil {
				return errors.New("service method %q cannot resolve input: %v", m.FullName(), err)
			}
			m.L1.Output, err = r.findMessageDescriptor(s.FullName(), partialName(md.GetOutputType()), false)
			if err != nil {
				return errors.New("service method %q cannot resolve output: %v", m.FullName(), err)
			}
		}
	}
	return nil
}
func (r *resolver) findTarget(k protoreflect.Kind, scope protoreflect.FullName, ref partialName, isWeak bool) (protoreflect.Kind, protoreflect.EnumDescriptor, protoreflect.MessageDescriptor, error) {
	switch k {
	case protoreflect.EnumKind:
		ed, err := r.findEnumDescriptor(scope, ref, isWeak)
		if err != nil {
			return 0, nil, nil, err
		}
		return k, ed, nil, nil
	case protoreflect.MessageKind, protoreflect.GroupKind:
		md, err := r.findMessageDescriptor(scope, ref, isWeak)
		if err != nil {
			return 0, nil, nil, err
		}
		return k, nil, md, nil
	case 0:
		d, err := r.findDescriptor(scope, ref)
		if err == protoregistry.NotFound && (r.allowUnresolvable || isWeak) {
			return k, filedesc.PlaceholderEnum(ref.FullName()), filedesc.PlaceholderMessage(ref.FullName()), nil
		} else if err == protoregistry.NotFound {
			return 0, nil, nil, errors.New("%q not found", ref.FullName())
		} else if err != nil {
			return 0, nil, nil, err
		}
		switch d := d.(type) {
		case protoreflect.EnumDescriptor:
			return protoreflect.EnumKind, d, nil, nil
		case protoreflect.MessageDescriptor:
			return protoreflect.MessageKind, nil, d, nil
		default:
			return 0, nil, nil, errors.New("unknown kind")
		}
	default:
		if ref != "" {
			return 0, nil, nil, errors.New("target name cannot be specified for %v", k)
		}
		if !k.IsValid() {
			return 0, nil, nil, errors.New("invalid kind: %d", k)
		}
		return k, nil, nil, nil
	}
}