Stage-packages causes build failure

Whenever I have stage-packages: my snapcraft.yaml, I get following build error:

Sorry, an error occurred in Snapcraft:
[Errno 13] Permission denied: ‘ubuntu-keyring-2012-cdimage.gpg’
We would appreciate it if you anonymously reported this issue.
No other data than the traceback and the version of snapcraft in use will be sent.
Would you like to send this error data? (Yes/No/Always/View) [no]:

Build command I use is:
SNAPCRAFT_BUILD_ENVIRONMENT=host snapcraft

Workaround:
Comment (#) stage-packages: and build the snap. Then un-comment stage-packages: and rebuild. Build goes fine then.

device:~$ snap version
snap 2.43.3
snapd 2.43.3
series 16
ubuntu 18.04
kernel 4.15.0-72-generic
device:~$

did you consider typing yes here ?

Not before, but I have now and sent it. Repasting here as well.

Traceback (most recent call last):
  File "/snap/snapcraft/4308/bin/snapcraft", line 11, in <module>
    load_entry_point('snapcraft==3.9.1', 'console_scripts', 'snapcraft')()
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/core.py", line 1236, in invoke
    return Command.invoke(self, ctx)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/cli/_runner.py", line 104, in run
    snap_command.invoke(ctx)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/cli/_command.py", line 88, in invoke
    return super().invoke(ctx)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/cli/lifecycle.py", line 280, in snap
    _execute(steps.PRIME, parts=[], pack_project=True, output=output, **kwargs)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/cli/lifecycle.py", line 78, in _execute
    lifecycle.execute(step, project_config, parts)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 131, in execute
    executor.run(step, part_names)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 185, in run
    self._handle_step(part_names, part, step, current_step, cli_config)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 199, in _handle_step
    getattr(self, "_run_{}".format(current_step.name))(part)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 241, in _run_pull
    self._run_step(step=steps.PULL, part=part, progress="Pulling")
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 315, in _run_step
    self._prepare_step(step=step, part=part)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 307, in _prepare_step
    preparation_function()
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/pluginhandler/__init__.py", line 449, in prepare_pull
    self._fetch_stage_packages()
 File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/pluginhandler/__init__.py", line 435, in _fetch_stage_packages
    self.stage_packages = self._stage_packages_repo.get(stage_packages)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 484, in get
    with self._apt.archive(self._cache.base_dir) as apt_cache:
  File "/snap/snapcraft/4308/usr/lib/python3.6/contextlib.py", line 81, in __enter__
    return next(self.gen)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 231, in archive
    raise e
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 222, in archive
    apt_cache = self._setup_apt(cache_dir)
  File "/snap/snapcraft/4308/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 136, in _setup_apt
    shutil.rmtree(cached_trusted_parts)
  File "/snap/snapcraft/4308/usr/lib/python3.6/shutil.py", line 486, in rmtree
    _rmtree_safe_fd(fd, path, onerror)
  File "/snap/snapcraft/4308/usr/lib/python3.6/shutil.py", line 444, in _rmtree_safe_fd
    onerror(os.unlink, fullname, sys.exc_info())
  File "/snap/snapcraft/4308/usr/lib/python3.6/shutil.py", line 442, in _rmtree_safe_fd
    os.unlink(name, dir_fd=topfd)
PermissionError: [Errno 13] Permission denied: 'ubuntu-keyring-2012-cdimage.gpg'

This is not really documented behavior and really a shortcut to using --destructive-mode. Keep in mind that this would not be a problem with the normal behavior or when using --use-lxd. While we can solve the Traceback, it will still result on PermissionError. To work around this, if you want to use this envvar, you should run sudo snapcraft with the implications of --destructive-mode.

This gets me into a different problem. I pull few others repo’s while building my snap and I see pull failure:

device:~/ostpm_24mar/os-tpm$ sudo SNAPCRAFT_BUILD_ENVIRONMENT=host snapcraft
Pulling os-util
Cloning into ‘/home/busybox/ostpm_24mar/os-tpm/parts/os-util/src’…
git@stash.xyz.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
Failed to pull source, command ‘git clone --recursive --branch brd_sys_info_getenv ssh://git@stash.xyz.com/emb/os-util.git /home/busybox/ostpm_24mar/os-tpm/parts/os-util/src’ exited with code 128.
device:~/ostpm_24mar/os-tpm$

which is not seen without stage-packages