Skip to content
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

Keep temporary directory in case of failure #26

Merged
merged 1 commit into from
Feb 23, 2024
Merged
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
22 changes: 15 additions & 7 deletions cmd/mkuimage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"github.com/dustin/go-humanize"
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/mkuimage/uimage"
"github.com/u-root/mkuimage/uimage/builder"
"github.com/u-root/mkuimage/uimage/uflags"
"github.com/u-root/uio/llog"
)
Expand Down Expand Up @@ -137,19 +138,20 @@
v, recommendedVersions, recommendedVersions[0])
}

keepTempDir := f.KeepTempDir
if f.TempDir == "" {
var err error
f.TempDir, err = os.MkdirTemp("", "u-root")
if err != nil {
return err
}
if f.KeepTempDir {
defer func() {
defer func() {
if keepTempDir {
l.Infof("Keeping temp dir %s", f.TempDir)
}()
} else {
defer os.RemoveAll(f.TempDir)
}
} else {
os.RemoveAll(f.TempDir)
}
}()
} else if _, err := os.Stat(f.TempDir); os.IsNotExist(err) {
if err := os.MkdirAll(f.TempDir, 0o755); err != nil {
return fmt.Errorf("temporary directory %q did not exist; tried to mkdir but failed: %v", f.TempDir, err)
Expand All @@ -166,5 +168,11 @@
if err != nil {
return err
}
return uimage.Create(l, append(m, more...)...)

err = uimage.Create(l, append(m, more...)...)
if errors.Is(err, builder.ErrBusyboxFailed) {
l.Errorf("Preserving temp dir due to busybox build error")
keepTempDir = true

Check warning on line 175 in cmd/mkuimage/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/mkuimage/main.go#L174-L175

Added lines #L174 - L175 were not covered by tests
}
return err
}
1 change: 1 addition & 0 deletions uimage/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (

// Possible build errors.
var (
ErrBusyboxFailed = errors.New("gobusybox build failed")
ErrEnvMissing = errors.New("must specify Go build environment")
ErrTempDirMissing = errors.New("must supply temporary directory for build")
ErrNoGoFiles = errors.New("could not find package directory")
Expand Down
6 changes: 3 additions & 3 deletions uimage/builder/gbb.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@
var errGopath *bb.ErrGopathBuild
var errGomod *bb.ErrModuleBuild
if errors.As(err, &errGopath) {
return fmt.Errorf("preserving bb generated source directory at %s due to error. To reproduce build, `cd %s` and `GO111MODULE=off GOPATH=%s go build`: %w", opts.TempDir, errGopath.CmdDir, errGopath.GOPATH, err)
return fmt.Errorf("%w: to reproduce build, `cd %s` and `GO111MODULE=off GOPATH=%s go build`: %w", ErrBusyboxFailed, errGopath.CmdDir, errGopath.GOPATH, err)

Check warning on line 83 in uimage/builder/gbb.go

View check run for this annotation

Codecov / codecov/patch

uimage/builder/gbb.go#L83

Added line #L83 was not covered by tests
}
if errors.As(err, &errGomod) {
return fmt.Errorf("preserving bb generated source directory at %s due to error. To debug build, `cd %s` and use `go build` to build, or `go mod [why|tidy|graph]` to debug dependencies, or `go list -m all` to list all dependency versions:\n%w", opts.TempDir, errGomod.CmdDir, err)
return fmt.Errorf("%w: to debug build, `cd %s` and use `go build` to build, or `go mod [why|tidy|graph]` to debug dependencies, or `go list -m all` to list all dependency versions:\n%w", ErrBusyboxFailed, errGomod.CmdDir, err)

Check warning on line 86 in uimage/builder/gbb.go

View check run for this annotation

Codecov / codecov/patch

uimage/builder/gbb.go#L86

Added line #L86 was not covered by tests
}
return fmt.Errorf("preserving bb generated source directory at %s due to error:\n%w", opts.TempDir, err)
return fmt.Errorf("%w: %w", ErrBusyboxFailed, err)

Check warning on line 88 in uimage/builder/gbb.go

View check run for this annotation

Codecov / codecov/patch

uimage/builder/gbb.go#L88

Added line #L88 was not covered by tests
}

if err := af.AddFile(bbPath, "bbin/bb"); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion uimage/uimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,7 @@
BinaryDir: cmds.TargetDir(),
}
if err := cmds.Builder.Build(l, files, bOpts); err != nil {
return fmt.Errorf("error building: %v", err)
return fmt.Errorf("error building: %w", err)

Check warning on line 650 in uimage/uimage.go

View check run for this annotation

Codecov / codecov/patch

uimage/uimage.go#L650

Added line #L650 was not covered by tests
}
}

Expand Down
Loading