Protobuf Imports: What the Parser Actually Does With Them
An import line in a .proto file is bookkeeping, and at least one popular parser drops the books. I pointed the vendored copy of protobufjs at a file with import "address.proto" at the top and read back the parsed root: the line was gone. It is not in the JSON serialization, not in any list on the root object, nowhere. The parser read it, used nothing from it, and kept no record. That is not a bug so much as a design statement: for this library, an import is not a relationship between files, it is a comment the developer wrote for humans. Every real tool that parses .proto in JavaScript, including the one on this site, therefore does the actual work some other way: it loads each file separately and merges the namespaces into one tree, and the import lines are decoration on top.
The pitfall that bites first is the alias form. The proto3 grammar has import addr = "address.proto"; and import with the allow_alias option, and neither one parses. The vendored parser throws illegal token 'addr' on the line, and it throws the same way in proto2 files, because it is the same parser. A plain import works, several plain imports work, but the moment a file uses an alias the whole file fails, and the error message points at the alias word as if it were an unknown type. If your file compiles with protoc and dies in a browser tool with illegal token on the import line, the alias is the usual suspect.
What actually resolves a type reference is the merged tree, and the search across it is wider than the spec promises. A field declared as a plain unqualified name in package me resolved, with no error, to a type that lives in package other, and even to one nested three levels deep in a.b.c. The lookup walks the whole tree and takes the first name it finds. That means the import line never told the parser anything: delete it and the reference still resolves, as long as the type is somewhere in the tree you built. For a library that does not load files itself, that is a reasonable trade, but it quietly changes the contract: in protoc, your file's imports define what names are in scope, and here, whatever your tool merged is in scope, and the files say nothing about it.
The foot-gun is what that trade does when two packages define the same type name, which is more common in a real codebase than you would think, because Inner and Message and Config are all popular names. I merged package p1 and package p2, both with an Inner, into one tree, and the unqualified reference in my file resolved to whichever package's copy landed in the tree first, and it would resolve to the other one if I had merged in the other order. No warning, no ambiguity error, just a silently different type on the other side of a name. If the reference resolves to the wrong Inner, the encode path fails loudly on the field types, which is the good case; the bad case is when the wrong Inner happens to accept your values and the message ships to a server that expected the other one.
The one failure that does announce itself is worth knowing by name. When no type with the referenced name exists anywhere in the tree, the resolve step throws no such Type or Enum 'Address' in Type .demo.Order: the missing name and the place it was used, in one line, which is exactly the error the tool shows a user who pasted one file from a repo that has eleven. And the collision question people ask after seeing that: field numbers do not work the same way across files, because they are scoped per message. Two imported messages can both own field number one without incident; the cross-file collision is the type name, not the number. That one is covered in the unknown-field-numbers post.
The practical shape, for anyone merging .proto files in a browser: parse each file on its own, merge the namespaces, resolve once, and when a reference dies, trust the error's two names, the missing type and the file that used it, to tell you which file never got loaded. All of this runs in the browser at proto.datajutsu.com, where a file that imports WKTs and user files both resolves on every tab, because the same merge that handles the imports handles the well-known types the same way.