From d6cf147c1bda601e811bf5813b0d46ca1c8ad9b9 Mon Sep 17 00:00:00 2001
From: Przemek Rzad <przemek@parity.io>
Date: Tue, 28 May 2024 19:57:43 +0200
Subject: [PATCH] Filter workspace dependencies in the templates (#4599)

This detaches the templates from monorepo's workspace dependencies.

Currently the templates [re-use the monorepo's
dependencies](https://github.com/paritytech/polkadot-sdk-minimal-template/blob/bd8afe66ec566d61f36b0e3d731145741a9e9e19/Cargo.toml#L45-L58),
most of which are not needed.

The simplest approach is to specify versions directly and not use
workspace dependencies in the templates.

Another approach would be to programmatically filter dependencies that
are actually needed - but not sure if it's worth it, given that it would
complicate the synchronization job.

cc @kianenigma @gupnik
---
 .github/workflows/misc-sync-templates.yml | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/.github/workflows/misc-sync-templates.yml b/.github/workflows/misc-sync-templates.yml
index b040c2fc89b..d8027014863 100644
--- a/.github/workflows/misc-sync-templates.yml
+++ b/.github/workflows/misc-sync-templates.yml
@@ -104,8 +104,6 @@ jobs:
 
           toml set templates/${{ matrix.template }}/Cargo.toml 'workspace.package.edition' "$(toml get --raw Cargo.toml 'workspace.package.edition')" > Cargo.temp
           mv Cargo.temp ./templates/${{ matrix.template }}/Cargo.toml
-
-          toml get Cargo.toml 'workspace.dependencies' --output-toml >> ./templates/${{ matrix.template }}/Cargo.toml
         working-directory: polkadot-sdk
       - name: Print the result Cargo.tomls for debugging
         if: runner.debug == '1'
@@ -118,6 +116,18 @@ jobs:
       - name: Copy over the new changes
         run: |
           cp -r polkadot-sdk/templates/${{ matrix.template }}/* "${{ env.template-path }}/"
+      - name: Copy over required workspace dependencies
+        run: |
+          echo -e "\n[workspace.dependencies]" >> Cargo.toml
+          set +e
+          # If a workspace dependency is required..
+          while cargo tree --depth 1 --prefix none --no-dedupe 2>&1 | grep 'was not found in `workspace.dependencies`'; do
+            # Get its name..
+            missing_dep=$(cargo tree --depth 1 --prefix none --no-dedupe 2>&1 | grep 'was not found in `workspace.dependencies`' | sed -E 's/(.*)`dependency.(.*)` was not found in `workspace.dependencies`/\2/')
+            # And copy the dependency from the monorepo.
+            toml get ../polkadot-sdk/Cargo.toml 'workspace.dependencies' --output-toml | grep "^${missing_dep} = " >> Cargo.toml
+          done;
+        working-directory: "${{ env.template-path }}"
 
       # 3. Verify the build. Push the changes or create a PR.
 
-- 
GitLab