Skip to content

Commit 2c8e57f

Browse files
authored
[cache] Fallback for failed build with cache (#2082)
## Summary * If we fail to build with caches, try again without them. * Added logging when build fails and user has substituters. ## How was it tested? Ran install on devbox project that installs cached terraform. Commented out `--extra-substituters` flag to force it to fail. Observed output indicating substituter error, and fallback to building from source. <img width="935" alt="image" src="https://github.com/jetify-com/devbox/assets/544948/0c1e6863-d506-4c11-b2d3-8556313be587">
1 parent 4e65e5d commit 2c8e57f

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

internal/devbox/packages.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"strings"
1717
"time"
1818

19-
"github.com/fatih/color"
2019
"github.com/pkg/errors"
2120
"github.com/samber/lo"
2221
"go.jetpack.io/devbox/internal/devbox/devopt"
@@ -404,12 +403,26 @@ func (d *Devbox) installPackages(ctx context.Context, mode installMode) error {
404403
}
405404

406405
if err := d.installNixPackagesToStore(ctx, mode); err != nil {
406+
if caches, _ := nixcache.CachedReadCaches(ctx); len(caches) > 0 {
407+
err = d.handleInstallFailure(ctx, mode)
408+
}
407409
return err
408410
}
409411

410412
return d.InstallRunXPackages(ctx)
411413
}
412414

415+
func (d *Devbox) handleInstallFailure(ctx context.Context, mode installMode) error {
416+
ux.Fwarning(d.stderr, "Failed to build from cache, building from source.\n")
417+
telemetry.Event(telemetry.EventNixBuildWithSubstitutersFailed, telemetry.Metadata{
418+
Packages: lo.Map(
419+
d.InstallablePackages(), func(p *devpkg.Package, _ int) string { return p.Raw }),
420+
})
421+
nixcache.DisableReadCaches()
422+
devpkg.ClearNarInfoCache()
423+
return d.installNixPackagesToStore(ctx, mode)
424+
}
425+
413426
func (d *Devbox) InstallRunXPackages(ctx context.Context) error {
414427
for _, pkg := range lo.Filter(d.InstallablePackages(), devpkg.IsRunX) {
415428
lockedPkg, err := d.lockfile.Resolve(pkg.Raw)
@@ -516,7 +529,6 @@ func (d *Devbox) installNixPackagesToStore(ctx context.Context, mode installMode
516529
args.AllowInsecure = allowInsecure
517530
err = nix.Build(ctx, args, installables...)
518531
if err != nil {
519-
color.New(color.FgRed).Fprintf(d.stderr, "Fail\n")
520532
return err
521533
}
522534
telemetry.Event(telemetry.EventNixBuildSuccess, telemetry.Metadata{

internal/devbox/providers/nixcache/nixcache.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ func CachedReadCaches(ctx context.Context) ([]*nixv1alpha1.NixBinCache, error) {
101101
return cachedReadCaches.Do(ctx)
102102
}
103103

104+
func DisableReadCaches() {
105+
cachedReadCaches = goutil.OnceWithContext(
106+
func(ctx context.Context) ([]*nixv1alpha1.NixBinCache, error) {
107+
return nil, nil
108+
},
109+
)
110+
}
111+
104112
func WriteCaches(
105113
ctx context.Context,
106114
) ([]*nixv1alpha1.NixBinCache, error) {

internal/devpkg/narinfo_cache.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,7 @@ func readCaches(ctx context.Context) ([]string, error) {
323323
}
324324
return cacheURIs, nil
325325
}
326+
327+
func ClearNarInfoCache() {
328+
narInfoStatusFnCache = sync.Map{}
329+
}

internal/telemetry/telemetry.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const (
4242
EventShellInteractive
4343
EventShellReady
4444
EventNixBuildSuccess
45+
EventNixBuildWithSubstitutersFailed
4546
)
4647

4748
var (

0 commit comments

Comments
 (0)