Closed
Description
Goal
per-file import uniquification
Why?
- It makes output deterministic. Currently, changing the order of input files, or even adding a new import to one file can result in changes to other files.
- It's a big step towards being able to generate files one at a time, or multiple packages at a time, as protoc does for other languages. Numerous people have requested this, the ability to generate file-at-a-time was only recently broken, and it's otherwise easy to fix.
- It cleans up the generated code when multiple plugin-plugins use the same packages.
I am happy to write this code if there's general support, and I don't believe the changes would be intrusive.
Current operation
Currently, plugins call RegisterUniquePackageName
once per imported name. This happens globally (ie. once per run).
Proposed addition
Add a method WriteImport(f *FileDescriptor, desiredName string, importPath string) (prefix string)
This would generate an import
statement in the output, as well as returning a prefix string to use when referring to objects from that package.
- If
importPath
is the same package as the FileDescriptor, no import is generated, and an empty prefix is returned. - If
importPath
has already been imported in the file, no import is generated, and the previous prefix is returned. - If
desiredName
has already been used by a different import in this file, or reserved byRegisterUniquePackageName
, a unique import alias is generated for the import. The prefix returned is the alias followed by.
The alias is added to the blacklist used byRegisterUniquePackageName
.
This change would allow incremental movement from RegisterUniquePackageName
towards WriteImport
, and both could coexist.