A Recovered .proto Is One File in a Graph (and the Import Is the Edge to the Rest)
I wanted to see what it actually takes to get a usable .proto file out of a running gRPC server when there is no .proto file on hand. The reflection service is how you ask, and the last time I pulled one of those replies apart the schema came back as opaque bytes you have to decode a second time. The decode tab on the tool now does that second decode for you: hand it the descriptor bytes and it renders the .proto back out. So the question that was left open was the one right after the .proto appears on the screen. Can you use it? I tested it, and the answer depends on whether the file has an import in it.
I wrote a small service that imports another file. The main file, api2.proto, declares a message with a field whose type lives in a different file, wkt.proto. I asked the reflection server for the file containing that type, and it sent back the descriptor as bytes, the way it always does. I turned those bytes into a .proto and I was left with one file on disk. That is where the test begins, because a .proto is not really a single unit. It is a node in a graph of files, and the import line at the top is the edge to the next node.
I ran the recovered file through the same parser that powers the tool, by itself, with nothing else around it. It failed. The parser could read every field in the file, it just could not finish, and the error named exactly what was missing: no such type demo.wkt.Tag. The field in my message points at a type that lives in wkt.proto, and wkt.proto was not there, so the type could not resolve. I ran the same file again, this time with its dependency present, and it resolved clean, the field pointing at the fully qualified type and every number intact. Same file, same bytes, one result flips on the presence of the file it imports.
That is the catch, and it is not a tool problem, it is how the format works. A file descriptor does not embed the files it imports. It lists them by name. The descriptor for my api2.proto carries a dependency field, and in that field is the plain string wkt.proto. So when reflection hands you the schema for one file, it also hands you a list of the other files you are going to need, as names, not as content. The .proto you can read on screen is one node of the graph. The import line is the pointer to the rest, and a file that imports anything is not self contained. The parser that reads the recovered file enforces exactly that: it reports the imports for the file, and it will not finish resolving until the things the imports point at are actually present.
There is a reason this shows up at exactly this step and not earlier. The descriptor, as a thing you can decode, is a message with its own schema, and the last post in this series walked through the bytes and the detection. The reflection reply is a stream, and the post before that one showed the stream and the opaque bytes. This post is the part after: you have the .proto, and now it has to resolve. The tool at proto.datajutsu.com turns the descriptor bytes into the .proto, and the decode tab is where you would feed it the file. When the file it renders has an import in it, that import is not a comment you can ignore and it is not a file that came back in the same reply. It is a second request to the reflection service, by the name the descriptor already told you. Ask for the file, read the imports it lists, ask for each of those, and the graph is whole.
That is where it lands. Recovering a .proto from a live server is a graph walk, not a file download. The reflection service is happy to hand you the whole graph, one node per request, and the import lines are the edges that tell you how many requests to make. A file with no imports is a single request and you are done. A file with imports is the first request of several, and the ones that follow are named for you in the descriptor you already hold. The parser that reads the recovered file enforces exactly this: it will not call the schema resolved until every file the imports point at is in front of it, and the moment they are, the same bytes that failed resolve clean.