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

[offline] Allow devbox to work offline if DNS fails #1985

Merged
merged 2 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions internal/devbox/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,10 +1007,13 @@ 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) {
ux.Fwarning(
d.stderr,
"Error connecting to the internet. Will attempt to use cached environment.\n",
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does the user experience if there was no cached environment?

i.e. can you rm -rf .devbox, turn off internet and see what you experience?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fails with an ugly error. I just added a check so we only keep going if the print dev env cache is there.

There are still a few other edge case failure modes, e.g. if some package has been gc'd from nix store, but I think that's fine.

} else if err != nil {
ux.Fwarning(d.stderr, "Error connecting to the internet. Will attempt to use cached environment.\n")
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")
}