fix(rust): normalize dependencies inherited from workspace (#463)

This commit is contained in:
Yusuf Bera Ertan 2023-01-27 22:39:34 +03:00 committed by GitHub
parent 029dcc6358
commit d91e7381fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 4 deletions

View File

@ -0,0 +1,48 @@
def normalizeWorkspaceDep:
if ($workspaceDependencies."\(.key)" | type) == "object"
then [., $workspaceDependencies."\(.key)"] | add
else [., {"version":$workspaceDependencies."\(.key)"}] | add
end
# remove workspace option from the dependency
| del(.workspace)
;
# normalizes workspace inherited dependencies for one list
def mapWorkspaceDepsFor(name):
if has(name)
then
."\(name)" = (
."\(name)"
| to_entries
| map(
if (.value | type) == "object" and .value.workspace == true
then .value = (.value | normalizeWorkspaceDep)
else .
end
)
| from_entries
)
else .
end
;
# shorthand for normalizing all the dependencies list
def mapWorkspaceDeps:
mapWorkspaceDepsFor("dependencies")
| mapWorkspaceDepsFor("dev-dependencies")
| mapWorkspaceDepsFor("build-dependencies")
;
# normalize workspace inherited deps
mapWorkspaceDeps
| if has("target")
then
# normalize workspace inherited deps in target specific deps
.target = (
.target
| to_entries
| map(.value = (.value | mapWorkspaceDeps))
| from_entries
)
else .
end

View File

@ -49,18 +49,21 @@ in rec {
sources = l.map makeSource deps;
findCrateSource = source: let
inherit (pkgs) cargo jq;
cargo = "${pkgs.cargo}/bin/cargo";
jq = "${pkgs.jq}/bin/jq";
yj = "${pkgs.yj}/bin/yj";
sponge = "${pkgs.moreutils}/bin/sponge";
pkg = source.dep;
in ''
# If the target package is in a workspace, or if it's the top-level
# crate, we should find the crate path using `cargo metadata`.
crateCargoTOML=$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path $tree/Cargo.toml | \
${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path')
crateCargoTOML=$(${cargo} metadata --format-version 1 --no-deps --manifest-path $tree/Cargo.toml | \
${jq} -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path')
# If the repository is not a workspace the package might be in a subdirectory.
if [[ -z $crateCargoTOML ]]; then
for manifest in $(find $tree -name "Cargo.toml"); do
echo Looking at $manifest
crateCargoTOML=$(${cargo}/bin/cargo metadata --format-version 1 --no-deps --manifest-path "$manifest" | ${jq}/bin/jq -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path' || :)
crateCargoTOML=$(${cargo} metadata --format-version 1 --no-deps --manifest-path "$manifest" | ${jq} -r '.packages[] | select(.name == "${pkg.name}") | .manifest_path' || :)
if [[ ! -z $crateCargoTOML ]]; then
break
fi
@ -69,6 +72,20 @@ in rec {
>&2 echo "Cannot find path for crate '${pkg.name}-${pkg.version}' in the tree in: $tree"
exit 1
fi
else
# we need to patch dependencies with `workspace = true` (workspace inheritance)
workspaceDependencies="$(cat "$tree/Cargo.toml" | ${yj} -tj | ${jq} -cr '.workspace.dependencies')"
if [[ "$workspaceDependencies" != "null" ]]; then
tree="$(pwd)/${pkg.name}-${pkg.version}"
cp -prd --no-preserve=mode,ownership "$(dirname $crateCargoTOML)" "$tree"
crateCargoTOML="$tree/Cargo.toml"
cat "$crateCargoTOML" \
| ${yj} -tj \
| ${jq} -cr --argjson workspaceDependencies "$workspaceDependencies" \
--from-file ${./patch-workspace-deps.jq} \
| ${yj} -jt \
| ${sponge} "$crateCargoTOML"
fi
fi
echo Found crate ${pkg.name} at $crateCargoTOML
tree="$(dirname $crateCargoTOML)"