Skip to content

Fix building Leeway with Leeway #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: clu/devcontainer
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,19 @@ func saveBuildResult(ctx context.Context, loc string, localCache cache.LocalCach
}
fin, err := os.OpenFile(br, os.O_RDONLY, 0644)
if err != nil {
fout.Close()
if closeErr := fout.Close(); closeErr != nil {
log.WithError(closeErr).Warn("failed to close output file")
}
log.WithError(err).Fatal("cannot copy build result")
}

_, err = io.Copy(fout, fin)
fout.Close()
fin.Close()
if err := fout.Close(); err != nil {
log.WithError(err).Warn("failed to close output file")
}
if err := fin.Close(); err != nil {
log.WithError(err).Warn("failed to close input file")
}
if err != nil {
log.WithError(err).Fatal("cannot copy build result")
}
Expand Down Expand Up @@ -246,7 +252,11 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, cache.LocalCache) {
if err != nil {
log.Fatal(err)
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
log.WithError(err).Warn("failed to close plan file")
}
}()

planOutlet = f
}
Expand Down
10 changes: 8 additions & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,11 @@ func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation,
return fmt.Errorf("execution failed in %s (%s): %w", loc.Name, loc.Dir, err)
}
_ = pty.InheritSize(ptmx, os.Stdin)
defer ptmx.Close()
defer func() {
if err := ptmx.Close(); err != nil {
log.WithError(err).Warn("failed to close pty")
}
}()

//nolint:errcheck
go io.Copy(textio.NewPrefixWriter(os.Stdout, prefix), ptmx)
Expand Down Expand Up @@ -358,7 +362,9 @@ func (c filesystemExecCache) MarkExecuted(ctx context.Context, loc commandExecLo
if err != nil {
return err
}
f.Close()
if err := f.Close(); err != nil {
log.WithError(err).Warn("failed to close file")
}
log.WithField("name", fn).Debug("marked executed")
return nil
}
Expand Down
12 changes: 10 additions & 2 deletions cmd/experiemental-unmount.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,21 @@ var unmountCmd = &cobra.Command{
if err != nil {
return err
}
defer src.Close()
defer func() {
if err := src.Close(); err != nil {
logrus.WithError(err).Warn("failed to close source file")
}
}()

f, err := os.OpenFile(dst, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if err != nil {
return err
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
logrus.WithError(err).Warn("failed to close destination file")
}
}()

logrus.WithField("dest", dst).Debug("applying change: copying content")
_, err = io.Copy(f, src)
Expand Down
6 changes: 5 additions & 1 deletion cmd/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ func formatBuildYaml(fn string, inPlace, fix bool) error {
if err != nil {
return err
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to close file: %v\n", err)
}
}()

var out io.Writer = os.Stdout
if inPlace {
Expand Down
10 changes: 7 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ var initCmd = &cobra.Command{
err = yaml.Unmarshal(tpl, &pkg)
if err != nil {
log.WithField("template", string(tpl)).Warn("broken package template")
return fmt.Errorf("This is a leeway bug. Cannot parse package template: %w", err)
return fmt.Errorf("this is a leeway bug, cannot parse package template: %w", err)
}

f, err := os.OpenFile("BUILD.yaml", os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
fmt.Fprintf(os.Stderr, "warning: failed to close BUILD.yaml file: %v\n", err)
}
}()

var cmp yaml.Node
err = yaml.NewDecoder(f).Decode(&cmp)
Expand All @@ -75,7 +79,7 @@ var initCmd = &cobra.Command{

cmps := cmp.Content[0].Content
for i, nde := range cmps {
if !(nde.Value == "packages" && i < len(cmps)-1 && cmps[i+1].Kind == yaml.SequenceNode) {
if nde.Value != "packages" || i >= len(cmps)-1 || cmps[i+1].Kind != yaml.SequenceNode {
continue
}

Expand Down
6 changes: 5 additions & 1 deletion cmd/provenance-assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ var provenanceAssertCmd = &cobra.Command{
if err != nil {
log.WithError(err).Fatalf("cannot open attestation bundle %s", bundleFN)
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
log.WithError(err).Warn("failed to close attestation bundle file")
}
}()

err = provutil.DecodeBundle(f, assert)
} else {
Expand Down
6 changes: 5 additions & 1 deletion cmd/provenance-export.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ var provenanceExportCmd = &cobra.Command{
if err != nil {
log.WithError(err).Fatal("cannot open attestation bundle")
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
log.WithError(err).Warn("failed to close attestation bundle file")
}
}()
err = provutil.DecodeBundle(f, export)
if err != nil {
log.WithError(err).Fatal("cannot extract attestation bundle")
Expand Down
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ func Execute() {
log.WithError(err).Fatal("cannot start trace but LEEWAY_TRACE is set")
return
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
log.WithError(err).Warn("failed to close trace file")
}
}()
err = trace.Start(f)
if err != nil {
log.WithError(err).Fatal("cannot start trace but LEEWAY_TRACE is set")
Expand Down
39 changes: 27 additions & 12 deletions pkg/leeway/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ func newBuildContext(options buildOptions) (ctx *buildContext, err error) {
if err != nil {
return nil, xerrors.Errorf("cannot compute hash of myself: %w", err)
}
defer self.Close()
defer func() {
if err := self.Close(); err != nil {
log.WithError(err).Warn("failed to close self")
}
}()
leewayHash := sha256.New()
_, err = io.Copy(leewayHash, self)
if err != nil {
Expand Down Expand Up @@ -974,7 +978,8 @@ func (p *Package) buildYarn(buildctx *buildContext, wd, result string) (bld *pac
}

var commands = make(map[PackageBuildPhase][][]string)
if cfg.Packaging == YarnOfflineMirror {
switch cfg.Packaging {
case YarnOfflineMirror:
err := os.Mkdir(filepath.Join(wd, "_mirror"), 0755)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1007,7 +1012,8 @@ func (p *Package) buildYarn(buildctx *buildContext, wd, result string) (bld *pac
}

tgt := p.BuildLayoutLocation(deppkg)
if cfg.Packaging == YarnOfflineMirror {
switch cfg.Packaging {
case YarnOfflineMirror:
fn := fmt.Sprintf("%s.tar.gz", tgt)
commands[PackageBuildPhasePrep] = append(commands[PackageBuildPhasePrep], []string{"cp", builtpkg, filepath.Join("_mirror", fn)})
builtpkg = filepath.Join(wd, "_mirror", fn)
Expand Down Expand Up @@ -1136,7 +1142,8 @@ func (p *Package) buildYarn(buildctx *buildContext, wd, result string) (bld *pac
pkgCommands [][]string
resultDir string
)
if cfg.Packaging == YarnOfflineMirror {
switch cfg.Packaging {
case YarnOfflineMirror:
builtinScripts := map[string]string{
"get_yarn_lock.sh": getYarnLockScript,
"install.sh": installScript,
Expand All @@ -1161,12 +1168,12 @@ func (p *Package) buildYarn(buildctx *buildContext, wd, result string) (bld *pac
),
}...)
resultDir = "_mirror"
} else if cfg.Packaging == YarnLibrary {
case YarnLibrary:
pkgCommands = append(pkgCommands, [][]string{
{"sh", "-c", fmt.Sprintf("yarn generate-lock-entry --resolved file://%s > %s", result, pkgYarnLock)},
{"yarn", "pack", "--filename", result},
}...)
} else if cfg.Packaging == YarnApp {
case YarnApp:
err := os.Mkdir(filepath.Join(wd, "_pkg"), 0755)
if err != nil {
return nil, err
Expand All @@ -1189,12 +1196,12 @@ func (p *Package) buildYarn(buildctx *buildContext, wd, result string) (bld *pac
),
}...)
resultDir = "_pkg"
} else if cfg.Packaging == YarnArchive {
case YarnArchive:
pkgCommands = append(pkgCommands, BuildTarCommand(
WithOutputFile(result),
WithCompression(!buildctx.DontCompress),
))
} else {
default:
return nil, xerrors.Errorf("unknown Yarn packaging: %s", cfg.Packaging)
}
res.Commands[PackageBuildPhasePackage] = pkgCommands
Expand Down Expand Up @@ -1345,7 +1352,7 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (res *packa
testCommand = append(testCommand, "-v")
}

if buildctx.buildOptions.CoverageOutputPath != "" {
if buildctx.CoverageOutputPath != "" {
testCommand = append(testCommand, fmt.Sprintf("-coverprofile=%v", codecovComponentName(p.FullName())))
} else {
testCommand = append(testCommand, "-coverprofile=testcoverage.out")
Expand Down Expand Up @@ -1377,7 +1384,7 @@ func (p *Package) buildGo(buildctx *buildContext, wd, result string) (res *packa
)
if !cfg.DontTest && !buildctx.DontTest {
commands[PackageBuildPhasePackage] = append(commands[PackageBuildPhasePackage], [][]string{
{"sh", "-c", fmt.Sprintf(`if [ -f "%v" ]; then cp -f %v %v; fi`, codecovComponentName(p.FullName()), codecovComponentName(p.FullName()), buildctx.buildOptions.CoverageOutputPath)},
{"sh", "-c", fmt.Sprintf(`if [ -f "%v" ]; then cp -f %v %v; fi`, codecovComponentName(p.FullName()), codecovComponentName(p.FullName()), buildctx.CoverageOutputPath)},
}...)
}

Expand Down Expand Up @@ -1745,13 +1752,21 @@ func extractImageNameFromCache(pkgName, cacheBundleFN string) (imgname string, e
if err != nil {
return "", err
}
defer f.Close()
defer func() {
if err := f.Close(); err != nil {
log.WithError(err).Warn("failed to close file")
}
}()

gzin, err := gzip.NewReader(f)
if err != nil {
return "", err
}
defer gzin.Close()
defer func() {
if err := gzin.Close(); err != nil {
log.WithError(err).Warn("failed to close gzip reader")
}
}()

tarin := tar.NewReader(gzin)
for {
Expand Down
6 changes: 5 additions & 1 deletion pkg/leeway/build_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func executeCommandsForPackageSafe(buildctx *buildContext, p *Package, wd string
}

if !log.IsLevelEnabled(log.DebugLevel) {
defer os.RemoveAll(tmpdir)
defer func() {
if err := os.RemoveAll(tmpdir); err != nil {
log.WithError(err).Warn("failed to remove temporary build directory")
}
}()
}

log.WithField("tmpdir", tmpdir).WithField("package", p.FullName()).Debug("preparing build runc environment")
Expand Down
20 changes: 16 additions & 4 deletions pkg/leeway/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,15 @@ func TestBuildDockerDeps(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(pth) })
t.Cleanup(func() {
if err := os.RemoveAll(pth); err != nil {
t.Logf("warning: failed to remove temporary directory: %v", err)
}
})

os.Setenv("PATH", pth+":"+os.Getenv("PATH"))
if err := os.Setenv("PATH", pth+":"+os.Getenv("PATH")); err != nil {
t.Fatalf("failed to set PATH: %v", err)
}
log.WithField("path", os.Getenv("PATH")).Debug("modified path to use dummy docker")
}
testutil.RunDUT()
Expand Down Expand Up @@ -160,9 +166,15 @@ func TestDockerPostProcessing(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.RemoveAll(pth) })
t.Cleanup(func() {
if err := os.RemoveAll(pth); err != nil {
t.Logf("warning: failed to remove temporary directory: %v", err)
}
})

os.Setenv("PATH", pth+":"+os.Getenv("PATH"))
if err := os.Setenv("PATH", pth+":"+os.Getenv("PATH")); err != nil {
t.Fatalf("failed to set PATH: %v", err)
}
log.WithField("path", os.Getenv("PATH")).Debug("modified path to use dummy docker")
}
testutil.RunDUT()
Expand Down
4 changes: 3 additions & 1 deletion pkg/leeway/cache/local/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func TestNewFilesystemCache(t *testing.T) {

if test.Expectation.Error == "" {
// Cleanup created directory
os.RemoveAll(test.Location)
if err := os.RemoveAll(test.Location); err != nil {
t.Logf("Failed to clean up test directory: %v", err)
}
}
})
}
Expand Down
16 changes: 13 additions & 3 deletions pkg/leeway/cache/remote/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,13 +457,19 @@ func (s *S3Storage) GetObject(ctx context.Context, key string, dest string) (int
if err != nil {
return 0, fmt.Errorf("failed to create destination file: %w", err)
}
defer file.Close()
defer func() {
if err := file.Close(); err != nil {
log.WithError(err).Warn("failed to close file")
}
}()

// Set up cleanup in case of error
var downloadErr error
defer func() {
if downloadErr != nil {
os.Remove(dest)
if err := os.Remove(dest); err != nil {
log.WithError(err).Warn("failed to remove destination file after download error")
}
}
}()

Expand Down Expand Up @@ -504,7 +510,11 @@ func (s *S3Storage) UploadObject(ctx context.Context, key string, src string) er
log.WithError(err).WithField("key", key).Warn("failed to open source file for upload")
return fmt.Errorf("failed to open source file: %w", err)
}
defer file.Close()
defer func() {
if err := file.Close(); err != nil {
log.WithError(err).Warn("failed to close file")
}
}()

uploader := manager.NewUploader(s.client, func(u *manager.Uploader) {
u.PartSize = defaultS3PartSize
Expand Down
4 changes: 3 additions & 1 deletion pkg/leeway/cache/remote/s3_download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ func TestS3CacheDownload(t *testing.T) {
// Clean up the temp directory
files, _ := filepath.Glob(filepath.Join(tmpDir, "*"))
for _, f := range files {
os.Remove(f)
if err := os.Remove(f); err != nil {
t.Logf("Failed to remove test file %s: %v", f, err)
}
}

mockStorage := &mockS3Storage{
Expand Down
Loading