forked from NixOS/nixpkgs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rustPlatform.importCargoLock: add support for git dependencies that u…
…se workspace inheritance Rust 1.64.0 added support for workspace inheritance, which allows for crates to inherit values such as dependency version constraints or package metadata information from their workspaces [0]. This works by having workspace members specify a value as a table, with `workspace` set to true. Thus, supporting this in importCargoLock is as simple as walking the crate's Cargo.toml, replacing inherited values with their workspace counterpart. This is also what a forthcoming Cargo release will do for `cargo vendor` [1], but we can get ahead of it ;) [0]: https://blog.rust-lang.org/2022/09/22/Rust-1.64.0.html#cargo-improvements-workspace-inheritance-and-multi-target-builds [1]: rust-lang/cargo#11414
- Loading branch information
Showing
7 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
import toml | ||
|
||
crate_manifest = toml.load(sys.argv[1]) | ||
workspace_manifest = toml.load(sys.argv[2])["workspace"] | ||
|
||
|
||
def replace_key(table, section, key): | ||
if "workspace" in table[key] and table[key]["workspace"] is True: | ||
print("replacing " + key) | ||
|
||
replaced = table[key] | ||
del replaced["workspace"] | ||
|
||
workspace_copy = workspace_manifest[section][key] | ||
|
||
if section == "dependencies": | ||
crate_features = replaced.get("features") | ||
|
||
if type(workspace_copy) is str: | ||
replaced["version"] = workspace_copy | ||
else: | ||
replaced.update(workspace_copy) | ||
|
||
merged_features = (crate_features or []) + (workspace_copy.get("features") or []) | ||
|
||
if len(merged_features) > 0: | ||
# Dictionaries are guaranteed to be ordered (https://stackoverflow.com/a/7961425) | ||
replaced["features"] = list(dict.fromkeys(merged_features)) | ||
elif section == "package": | ||
table[key] = replaced = workspace_copy | ||
|
||
|
||
def replace_dependencies(root): | ||
for key in ["dependencies", "dev-dependencies", "build-dependencies"]: | ||
if key in root: | ||
for k, v in root[key].items(): | ||
replace_key(root[key], "dependencies", k) | ||
|
||
|
||
for key in crate_manifest["package"].keys(): | ||
replace_key(crate_manifest["package"], "package", key) | ||
|
||
replace_dependencies(crate_manifest) | ||
|
||
if "target" in crate_manifest: | ||
for key in crate_manifest["target"].keys(): | ||
replace_dependencies(crate_manifest["target"][key]) | ||
|
||
with open(sys.argv[1], "w") as f: | ||
toml.dump(crate_manifest, f) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/crate.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
version = { workspace = true } | ||
|
||
[dependencies] | ||
foo = { workspace = true, features = ["cat"] } | ||
bar = "1.0.0" |
7 changes: 7 additions & 0 deletions
7
...uild-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/default.nix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ replaceWorkspaceValues, runCommand }: | ||
|
||
runCommand "git-dependency-workspace-inheritance-test" { } '' | ||
cp --no-preserve=mode ${./crate.toml} "$out" | ||
${replaceWorkspaceValues} "$out" ${./workspace.toml} | ||
diff -u "$out" ${./want.toml} | ||
'' |
9 changes: 9 additions & 0 deletions
9
.../build-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/want.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
version = "1.0.0" | ||
|
||
[dependencies] | ||
bar = "1.0.0" | ||
|
||
[dependencies.foo] | ||
features = [ "cat", "meow",] | ||
version = "1.0.0" |
5 changes: 5 additions & 0 deletions
5
...d-support/rust/test/import-cargo-lock/git-dependency-workspace-inheritance/workspace.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[workspace.package] | ||
version = "1.0.0" | ||
|
||
[workspace.dependencies] | ||
foo = { version = "1.0.0", features = ["meow"] } |