Skip to content

Commit 1fee618

Browse files
Chris PattersonSergio Schvezov
andauthored
go plugin: do not remove install /bin directory before build (#2957)
Packages configured in a part's stage-packages are installed prior to build and installed to $SNAPCRAFT_PART_INSTALL. Cleaning $SNAPCRAFT_PART_INSTALL/bin will purge any files that may have been installed there. Running go build should overwrite any previously installed files, which will be OK for incremental builds. However, as the go plugin does not enable out_of_source_build, the entire build directory is purged on every build anyways. Add spread test for coverage. Signed-off-by: Chris Patterson <chris.patterson@canonical.com> Co-authored-by: Sergio Schvezov <sergio.schvezov@canonical.com>
1 parent 125ec0c commit 1fee618

4 files changed

Lines changed: 80 additions & 4 deletions

File tree

snapcraft/plugins/go.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,10 +284,8 @@ def _build_go_packages(self) -> None:
284284
def build(self) -> None:
285285
super().build()
286286

287-
# Clear the installation before continuing.
288-
if os.path.exists(self._install_bin_dir):
289-
shutil.rmtree(self._install_bin_dir)
290-
os.makedirs(self._install_bin_dir)
287+
# Ensure install directory exists.
288+
os.makedirs(self._install_bin_dir, exist_ok=True)
291289

292290
if self._is_using_go_mod(cwd=self.builddir):
293291
self._build()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// -*- Mode: Go; indent-tabs-mode: t -*-
2+
3+
/*
4+
* Copyright (C) 2020 Canonical Ltd
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 3 as
8+
* published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*
18+
*/
19+
20+
package main
21+
22+
import "fmt"
23+
24+
func main() {
25+
fmt.Println("hello world")
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: go-hello
2+
version: "0.1"
3+
summary: A simple go project
4+
description: |
5+
Test to ensure stage packages ship in the final snap.
6+
7+
grade: devel
8+
confinement: strict
9+
base: core18
10+
11+
apps:
12+
go-hello:
13+
command: go-hello
14+
15+
parts:
16+
go-hello:
17+
plugin: go
18+
go-channel: ""
19+
source: go-hello
20+
stage-packages: [grep]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
summary: Build part of a go snap using stage packages
2+
3+
environment:
4+
SNAP_DIR: ../snaps/go-use-stage-packages
5+
6+
prepare: |
7+
#shellcheck source=tests/spread/tools/snapcraft-yaml.sh
8+
. "$TOOLS_DIR/snapcraft-yaml.sh"
9+
set_base "$SNAP_DIR/snap/snapcraft.yaml"
10+
11+
restore: |
12+
#shellcheck source=tests/spread/tools/snapcraft-yaml.sh
13+
. "$TOOLS_DIR/snapcraft-yaml.sh"
14+
cd "$SNAP_DIR"
15+
snapcraft clean
16+
rm -f ./*.snap
17+
restore_yaml snap/snapcraft.yaml
18+
19+
execute: |
20+
cd "$SNAP_DIR"
21+
22+
snapcraft prime
23+
24+
if [[ ! -f "prime/bin/go-hello" ]]; then
25+
echo "Go-hello not installed into /bin."
26+
exit 1
27+
fi
28+
29+
if [[ ! -f "prime/bin/grep" ]]; then
30+
echo "Missing stage package files."
31+
exit 1
32+
fi

0 commit comments

Comments
 (0)