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: https://github.com/solvespace/solvespace/blob/master/pkg/snap/build.sh

You could do something like this:

#!/bin/sh -xe

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

cd "$dir"

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

snapcraft "$@"

Use it as build.sh 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