Skip to content

Conversation

mikeland73
Copy link
Contributor

Summary

A few fish and local state improvements:

  • [fish] Don't add recursion protection if init hook is empty or default. Recursion protection is shell-specific so it may cause issues in some edge cases (e.g. if someone uses a shellenv --init-hooks output on a different shell).
  • [fish] Incorporate fish into local state hash. This means that if a user changes shell, we consider the local state stale. This is needed because we generate different files (e.g. shellrc) depending on the shell.
  • [local-state] Rename a bunch of stuff in local.go to be more accureately named ( stateHash instead of localLock)
  • [local-state] Decoupled stateHash from lock file. These are different and should not depend on each other. They are still in the same package, but we can change that in the future.
  • Removed the devbox interface from statehash. As @gcurtis warned us many moons ago, interfaces like this make code harder to follow.

How was it tested?

Tested empty/default init hooks not having recursion protection:

devbox init
devbox run echo foo
cat .devbox/gen/scripts/.hooks.sh

Tested local state with fish

devbox run echo 5
cat .devbox/local.lock
SHELL=fish devbox  run echo 5
cat .devbox/local.lock

@mikeland73 mikeland73 requested review from gcurtis and savil January 30, 2024 20:39
Copy link
Collaborator

@savil savil left a comment

Choose a reason for hiding this comment

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

thanks!

@@ -0,0 +1,116 @@
// Copyright 2023 Jetpack Technologies Inc and contributors. All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

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

would be nice to git mv such files to preserve history and make reviewing the code changes easier

Copy link
Contributor Author

Choose a reason for hiding this comment

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

github shows the file as deleted, but I split the PR into 2 commits. If you take a look a7160e3 it contains the changes to the file.

DevboxVersion string `json:"devbox_version"`
// fish has different generated scripts so we need to recompute them if user
// changes shell.
IsFish bool `json:"is_fish"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is fine for now, because our code gen logic switches on IsFish

Ideally, we'd have a shell enum value here (ref. the enum in devbox/shell.go) so that this is slightly more generalizable.

Comment on lines +50 to +71
filesystemLock, err := readStateHashFile(args.ProjectDir)
if err != nil {
return false, err
}
newLock, err := getCurrentStateHash(args)
if err != nil {
return false, err
}

return *filesystemLock == *newLock, nil
}

func readStateHashFile(projectDir string) (*stateHashFile, error) {
lockFile := &stateHashFile{}
err := cuecfg.ParseFile(stateHashFilePath(projectDir), lockFile)
if errors.Is(err, fs.ErrNotExist) {
return lockFile, nil
}
if err != nil {
return nil, err
}
return lockFile, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

its confusing to call these lockfiles in the local variables. Why not stateHashFile and similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

will change

return nil, err
}

newLock := &stateHashFile{
Copy link
Collaborator

Choose a reason for hiding this comment

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

newLock -> newHashFile

}

func stateHashFilePath(projectDir string) string {
return filepath.Join(projectDir, ".devbox", "local.lock")
Copy link
Collaborator

Choose a reason for hiding this comment

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

I find the .lock pretty confusing usage here. Lockfiles in my mind represent a need to ensure that some particular state can be reproduced as done with package.lock or devbox.lock.

This feels to be more of a local-state-cache-representation or .devbox/state_cache_key.json, or simply .devbox/cache_key.json

WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

for sure. I was not bold enough to change the name, but there's no downside really.

@@ -110,6 +110,11 @@ func writeInitHookFile(devbox devboxer, body, tmpl, filename string) (err error)
}
defer script.Close() // best effort: close file

if body == devconfig.DefaultInitHook || strings.TrimSpace(body) == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mind adding a comment to the effect of:

// skip adding init-hook recursion guard for the 
// default hook or any empty hook

As is, not obvious why the template below is being skipped.

Will also be a good idea to comment somewhere in the template that it will be skipped for default or empty hooks, in case we later introduce some other logic for init-hooks in the template files.

@mikeland73 mikeland73 force-pushed the landau/improve-local-state branch from 465b143 to a7160e3 Compare February 1, 2024 00:17
@mikeland73 mikeland73 merged commit 4f50c91 into main Feb 1, 2024
@mikeland73 mikeland73 deleted the landau/improve-local-state branch February 1, 2024 00:38
mikeland73 added a commit that referenced this pull request Feb 1, 2024
## Summary

I accidentally merged #1765
without committing the fixes requested in the PR. This PR contains them.

## How was it tested?
Copy link

sentry-io bot commented Feb 2, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ **json.SyntaxError: <redacted errors.withStack>: <redacted json.SyntaxError> go.jetpack.io/devbox/internal/cuecfg in Unmarshal View Issue
  • ‼️ **json.SyntaxError: <redacted errors.withStack>: <redacted json.SyntaxError> go.jetpack.io/devbox/internal/cuecfg in Unmarshal View Issue
  • ‼️ **json.SyntaxError: <redacted errors.withStack>: <redacted json.SyntaxError> go.jetpack.io/devbox/internal/cuecfg in Unmarshal View Issue
  • ‼️ **json.UnmarshalTypeError: <redacted *errors.withStack>: <redacted errors.withStack>: <redacted json.UnmarshalTypeError> go.jetpack.io/devbox/internal/cuecfg in Unmarshal View Issue
  • ‼️ **syscall.Errno: <redacted errors.withStack>: <redacted fs.PathError>: go.jetpack.io/devbox/internal/cuecfg in WriteFile View Issue

Did you find this useful? React with a 👍 or 👎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants