Include Parent Directories when Building Snap

I am trying to build a snap in SNAP_DIR, but the build depends on code present in SNAP_DIR/../sharedCode. How can I make this directory visible to the build part step? Thank you.

Only directories below the project dir are accessible in the VM/container at build time.
A possible straightforward solution: Use a build script that wraps the snapcraft command and copies the necessary files. E.g. solvespace uses this to support its unorthodox fs layout:

You could do something like this:

#!/bin/sh -xe

dir="$(dirname "$(readlink -f "$0")")"
trap "rm -rf $shared_code" EXIT

cd "$dir"

rsync --filter=":- .gitignore" -r "$shared_code_src"/ "$shared_code"

snapcraft "$@"

Use it as and call it like you would call snapcraft. The shared code would be available in the directory shared-code at build time.

However, this is probably not a very good pattern as the coupling is very implicit. I’d suggest using git submodules to include shared code. You can do that with local repositories too.

I have since modified my building step to copy the shared directory into SNAP_DIR when building, similar to your provided code, and it works fine, additionally fixing make artifacts. Thank you!

1 Like