What is the correct workflow for updating upstream changes to dependencies?

I am currently working with several forks of projects and finding myself needing to make incremental changes to the forked GitHub repos and then pull in the changes to the main project that is using said dependencies.

I find myself deleting the target directory, deleting Cargo.lock and then using cargo generate-lockfile or cargo build but I wondered if there's a recommended way of doing this.

I understand you to mean that you currently have:

[dependencies]
foo = { git = "<url>" }

and you want Cargo to use a newer revision in that repository than it currently is. In that case,
cargo update, or cargo update -p foo, should be all you need. The revision actually used is recorded in the Cargo.lock file, and cargo update will replace it. You do not need to delete target/.

You can also specify the revision explicitly:

[dependencies]
foo = { git = "<url>", rev = "<commithash>" }
# or if you use Git tags:
foo = { git = "<url>", tag = "v0.1.2" }
# or if you use Git branches:
foo = { git = "<url>", branch = "whatever" }

The advantage of doing this is that when you revisit the history of this project, Cargo will know what upstream version to use and you won't experience breaking changes. The lock file (if kept in version control) also serves that role, but is less explicit. It's like the difference of a regular crates.io dependency like foo = "1.2.3" vs foo = "*" — the latter exposes you to incompatible changes and the former does not.

3 Likes