Skip to content

Commit

Permalink
[offline] Allow devbox to work offline if DNS fails (#1985)
Browse files Browse the repository at this point in the history
## Summary

Fixes #1977

(with another hack).

Unfortunately the way we structure our flake makes it really difficult
to remove our network requests (when state is stale). Ideally, the flake
can be built off of local only nix store references, but that's a much
bigger project.

In the meantime, this is a quick hack that fixes issue above.

## How was it tested?

Added synthetic error that returns `connection refused` in the message.
Ensures I was still able to run/shell with a stale state.
  • Loading branch information
mikeland73 committed Apr 11, 2024
1 parent 39620da commit 6aca4d4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,21 @@ func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(

// When ensureStateIsUpToDate is called with ensure=true, it always
// returns early if the lockfile is up to date. So we don't need to check here
if err := d.ensureStateIsUpToDate(ctx, ensure); err != nil && !strings.Contains(err.Error(), "no such host") {
return nil, err
if err := d.ensureStateIsUpToDate(ctx, ensure); isConnectionError(err) {
if !fileutil.Exists(d.nixPrintDevEnvCachePath()) {
ux.Ferror(
d.stderr,
"Error connecting to the internet and no cached environment found. Aborting.\n",
)
return nil, err
}
ux.Fwarning(
d.stderr,
"Error connecting to the internet. Will attempt to use cached environment.\n",
)
} else if err != nil {
ux.Fwarning(d.stderr, "Error connecting to the internet. Will attempt to use cached environment.\n")
// Some other non connection error, just return it.
return nil, err
}

// Since ensureStateIsUpToDate calls computeEnv when not up do date,
Expand Down
12 changes: 12 additions & 0 deletions internal/devbox/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package devbox

import "strings"

func isConnectionError(err error) bool {
if err == nil {
return false
}

return strings.Contains(err.Error(), "no such host") ||
strings.Contains(err.Error(), "connection refused")
}

0 comments on commit 6aca4d4

Please sign in to comment.