Submódulos y Vendoring
Submódulos de Git · Subtrees · Código vendorizado
Escrito por Darjus Kazakevic · revisado el
Pin someone else's code inside your repository, and own the pin.
Sometimes a dependency has no package, or you need an upstream fix that is not released, or the build must work offline. Vendoring puts the source in your tree. A git submodule stores a pointer to one commit of another repository. A git subtree merges that repository's files into a folder of yours. A plain copy just takes the files.
All three give a build that reproduces, and all three decay the same way. The pin is a commit someone chose once. When nobody moves it, you maintain a fork without having decided to. A short note next to the code, saying why it is pinned and what ends the pin, keeps it healthy.
En esencia — Bring another repository's code into yours, as a submodule, a subtree or a copy, for a build that reproduces against code you do not control.
Cuándo recurrir a él
Recurre a él cuando
- You need an upstream fix that is merged but not yet released
- The build must work offline, or the dependency has no package
- You must build against an exact commit of a repository you don't own
Reconsidéralo cuando
- A released package at the right version is one lockfile line away
- Code you edit as often as your own; it belongs in your repository
- Repositories that must change together; a monorepo fits better
Conceptos clave
- Git submodule
- A pointer from your repository to one commit of another repository. Your tree stores that commit hash, and .gitmodules stores the URL and path. You keep the pointer, not the files.
- Git subtree
- Merges another repository's files, and optionally its history, into a folder of yours. They are real files in your tree, so a plain clone just works.
- Vendored copy
- The files are copied into a vendor/ folder and committed. Go supports this directly: go mod vendor copies every dependency into vendor/ for offline builds.
- Exit condition
- A written note of why the code is pinned and what would let you remove the pin, such as “upstream releases v2.4.0”. Without it, nobody knows if a pin is deliberate or abandoned.
Reglas de diseño
- Record why it's pinned and what ends it
- Next to the vendored code, write the pinned commit, the reason, any local patch, the exit condition and an owner. Without it, nobody can tell a deliberate pin from an abandoned one.
- Send every local patch upstream
- Each local patch is a conflict waiting at the next update. Send it upstream so the exit condition stays reachable. Otherwise you maintain a fork without having decided to.
- Prefer a package when one exists
- A released package at the right version is one line in a lockfile. Vendor only when there is no package, the fix is unreleased, or the build must work offline.
Errores habituales
- CI never runs git submodule update --init. The build fails on an empty folder with “file not found”.
- A vendored folder with local edits and no note. A year later nobody dares to update or delete it.
- A submodule pointer to a commit that was never pushed. Everyone else's git submodule update fails, because the server does not have it.
- Using submodules to fake atomic changes across repositories. Each change still needs two commits in two places.