Issue with rebuilding a rust snap

Hello,

I am trying to package a rust application as a snap.

I have installed snapcraft with snap install snapcraft --classic as described here: https://snapcraft.io/first-snap/rust.

The first time executing snapcraft it creates a new multipass instance an then builds the snap successfully.

But when I try to rebuild the snap I get this error

/root/.cargo/bin/cargo +stable install --path /root/parts/xsv/build --root /root/parts/xsv/install
  Installing xsv v0.13.0 (/root/parts/xsv/build)
error: binary `xsv` already exists in destination as part of `xsv v0.13.0 (/root/parts/xsv/build)`
Add --force to overwrite
Failed to run '/root/.cargo/bin/cargo +stable install --path /root/parts/xsv/build --root /root/parts/xsv/install' for 'xsv': Exited with code 101.
Verify that the part is using the correct parameters and try again.

This happens both with my rust app and the xsv app documented in this guide: https://snapcraft.io/first-snap/rust/linux/package.

I think this happens because it is reusing the multipass instance and rerunning some form of cargo install. By default cargo will not overwrite an existing binary with cargo install. So you have to pass --force to do that, which is what it suggests. However I have no idea how to get snapcraft to pass this flag to cargo and I don’t want to delete and rebuild the multipass instance every time.

I have been unable to find any reference to this issue by googling.

Is there a way to pass parameters to cargo or anything else I am missing?

Thanks,
Flo

1 Like

This is a indeed a bug. As a workaround, in the part using the rust plugin you can do something like:

parts:
    <part>:
        override-build:|
            [ -f "$SNAPCRAFT_PART_INSTALL/bin/<binary>" ] && rm "$SNAPCRAFT_PART_INSTALL/bin/<binary>"
            snapcraftctl build
2 Likes

Thanks @sergiusens it helped a lot :smiley: but there needed to be a space between : | otherwise snapcraft complained about the syntax.

Here is it for the next guy to copy & paste

parts:
    <part>:
        override-build: |
            [ -f "$SNAPCRAFT_PART_INSTALL/bin/<binary>" ] && rm "$SNAPCRAFT_PART_INSTALL/bin/<binary>"
            snapcraftctl build

This should no longer be needed with snapcraft 3.7 Call for testing: snapcraft 3.7