Override build steps

How to find out project root or location of snapcraft.yml? I don’t see any environment variables pointing to it. Why? There is a custom version file in project root, maintained by other people, and all parts should be built for version specified there.

I am doing out of tree builds, and usual “parent of stage” doesn’t work here.

1 Like

Added SNAPCRAFT_PROJECT_DIR is a part of “out of tree build” PR.

https://github.com/snapcore/snapcraft/pull/2463/commits/85f9815483cdda206b5acd0a7b1cb94744f95c39

Tests are not easy.

I would like to ask if the shell environment of the scriptlet has defined any shell options like errexit and nounset?

UPDATE: It seems that only errexit is enforced:

while we should stop execution on errors, nounset should really be in the hands of the developer (i personally am for example not a fan of it since it makes scripts a lot bigger and harder to read if you have to zero-init variables everywhere)

1 Like

@degville hey there! Just had a thought about making this less about scriptlets and more about overriding steps.

Good idea! I’ve linked to this from the new Snapcraft build, debug and publishing docs roadmap plan we have so we can update this page to reflect that ideal.

This is helpful, but for me at least left me with more questions. The content by Dan at Proposal: Expose SNAPCRAFT_SNAP and SNAPCRAFT_PARTS environment variables completed the picture on how to apply a patch.

@degville hey, just a mention that the prime example is missing a call to snapcraftctl prime. I would fix it myself, but I have no edit button :slight_smile:

1 Like

Thanks! I’ve made this a wiki and added the missing command.

1 Like

The proper way to apply a patch is to install them to the /stage tree by a different part and applying them in the consuming part’s override-{pull,build} scriptlets, refer the following one for an example:

What is the scope of a variable I define arbitrarily inside a scriptlet? I’m assuming it’s local to just that scriptlet but I don’t see that written down explicitly anywhere.

Issue

Noticed this in another doc page, but it looks like the anchor is broken here too. Like the other doc page, the problem seems to be with how the anchor is rendered in the document. When you click on the anchor, nothing seems to happen on the page.

Potential solution

I found that adding an extra space between the anchor and the heading text fixes the issue:

Instead of:

  • ## Overriding the **build** step[:anchor:](#heading--overriding-the-build-step)

do:

  • ## Overriding the **build** step [:anchor:](#heading--overriding-the-build-step)

Thanks for letting us know! I’ve updated all the headings in this doc to use the new anchor scheme.

1 Like

Are we still referring to overrides as “scriptlets”? I thought that terminology was being phased out.

I’m not sure, but the term isn’t terribly useful outside the context of the snippets themselves. It might make better sense to simply rename this 'Overriding build steps` (actually, as originally suggested by Sergio a couple of years ago). I’ll do that now.

I would shorten and just call them “overrides”.

Yep, good plan - I was mainly talking about changing the title for this page, but I’ll do both.

Should also mention core22 “craftctl default” variant.

Let’s say the default build/install process ends up installing files with absolute paths in them, which need to be fixed up to look inside the snap:

@kyrofa is it really necessary? I run my bash script with #!/bin/sh and it is able to find all its stage-packages without patched stuff. If system level /bin/sh is used to run the script, the system level sh should not be able to discover snap’s bin.

Totally depends on the snap and what absolute paths we’re talking about, here. This was just an example for where overriding might be useful, that’s all.