Skip to content

Commit

Permalink
rename moar functions
Browse files Browse the repository at this point in the history
  • Loading branch information
savil committed Dec 7, 2023
1 parent 96630ea commit 3f884b3
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 42 deletions.
2 changes: 1 addition & 1 deletion devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Devbox interface {
Install(ctx context.Context) error
IsEnvEnabled() bool
ListScripts() []string
NixEnv(ctx context.Context, opts devopt.NixEnvOpts) (string, error)
DevboxEnvExports(ctx context.Context, opts devopt.DevboxEnvExports) (string, error)
PackageNames() []string
ProjectDir() string
Pull(ctx context.Context, opts devopt.PullboxOpts) error
Expand Down
2 changes: 1 addition & 1 deletion internal/boxcli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func runShellCmd(cmd *cobra.Command, flags shellCmdFlags) error {
if flags.printEnv {
// false for includeHooks is because init hooks is not compatible with .envrc files generated
// by versions older than 0.4.6
script, err := box.NixEnv(cmd.Context(), devopt.NixEnvOpts{})
script, err := box.DevboxEnvExports(cmd.Context(), devopt.DevboxEnvExports{})
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/boxcli/shellenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func shellEnvFunc(
}
}

envStr, err := box.NixEnv(cmd.Context(), devopt.NixEnvOpts{
envStr, err := box.DevboxEnvExports(cmd.Context(), devopt.DevboxEnvExports{
DontRecomputeEnvironment: !recomputeEnvIfNeeded,
NoRefreshAlias: flags.noRefreshAlias,
RunHooks: flags.runInitHook,
Expand Down
36 changes: 20 additions & 16 deletions internal/impl/devbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (d *Devbox) Shell(ctx context.Context) error {
ctx, task := trace.NewTask(ctx, "devboxShell")
defer task.End()

envs, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
envs, err := d.computeCurrentDevboxEnv(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
return err
}

env, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
env, err := d.computeCurrentDevboxEnv(ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -267,7 +267,10 @@ func (d *Devbox) ListScripts() []string {
return keys
}

func (d *Devbox) NixEnv(ctx context.Context, opts devopt.NixEnvOpts) (string, error) {
// DevboxEnvExports returns a string of the env-vars that would need to be applied
// to define a Devbox environment. The string is of the form `export KEY=VALUE` for each
// env-var that needs to be applied.
func (d *Devbox) DevboxEnvExports(ctx context.Context, opts devopt.DevboxEnvExports) (string, error) {
ctx, task := trace.NewTask(ctx, "devboxNixEnv")
defer task.End()

Expand All @@ -290,7 +293,7 @@ func (d *Devbox) NixEnv(ctx context.Context, opts devopt.NixEnvOpts) (string, er

envs, err = d.computeDevboxEnv(ctx, true /*usePrintDevEnvCache*/)
} else {
envs, err = d.ensurePackagesAreInstalledAndComputeEnv(ctx)
envs, err = d.computeCurrentDevboxEnv(ctx)
}

if err != nil {
Expand All @@ -315,7 +318,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
ctx, task := trace.NewTask(ctx, "devboxEnvVars")
defer task.End()
// this only returns env variables for the shell environment excluding hooks
envs, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
envs, err := d.computeCurrentDevboxEnv(ctx)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -888,15 +891,15 @@ func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool)

markEnvsAsSetByDevbox(pluginEnv, configEnv)

nixEnvPath := env["PATH"]
debug.Log("PATH after plugins and config is: %s", nixEnvPath)
devboxEnvPath := env["PATH"]
debug.Log("PATH after plugins and config is: %s", devboxEnvPath)

// We filter out nix store paths of devbox-packages (represented here as buildInputs).
// Motivation: if a user removes a package from their devbox it should no longer
// be available in their environment.
buildInputs := strings.Split(env["buildInputs"], " ")
var glibcPatchPath []string
nixEnvPath = filterPathList(nixEnvPath, func(path string) bool {
devboxEnvPath = filterPathList(devboxEnvPath, func(path string) bool {
// TODO(gcurtis): this is a massive hack. Please get rid
// of this and install the package to the profile.
if strings.Contains(path, "patched-glibc") {
Expand All @@ -913,24 +916,24 @@ func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool)
}
return true
})
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, devboxEnvPath)

// TODO(gcurtis): this is a massive hack. Please get rid
// of this and install the package to the profile.
if len(glibcPatchPath) != 0 {
patchedPath := strings.Join(glibcPatchPath, string(filepath.ListSeparator))
nixEnvPath = envpath.JoinPathLists(patchedPath, nixEnvPath)
debug.Log("PATH after glibc-patch hack is: %s", nixEnvPath)
devboxEnvPath = envpath.JoinPathLists(patchedPath, devboxEnvPath)
debug.Log("PATH after glibc-patch hack is: %s", devboxEnvPath)
}

runXPaths, err := d.RunXPaths(ctx)
if err != nil {
return nil, err
}
nixEnvPath = envpath.JoinPathLists(nixEnvPath, runXPaths)
devboxEnvPath = envpath.JoinPathLists(devboxEnvPath, runXPaths)

pathStack := envpath.Stack(env, originalEnv)
pathStack.Push(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
pathStack.Push(env, d.projectDirHash(), devboxEnvPath, d.preservePathStack)
env["PATH"] = pathStack.Path(env)
debug.Log("New path stack is: %s", pathStack)

Expand All @@ -950,13 +953,14 @@ func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool)
return env, d.addHashToEnv(env)
}

// ensurePackagesAreInstalledAndComputeEnv does what it says :P
func (d *Devbox) ensurePackagesAreInstalledAndComputeEnv(
// computeCurrentDevboxEnv will return a map of the env-vars for the Devbox Environment
// while ensuring these reflect the current (up to date) state of the project.
func (d *Devbox) computeCurrentDevboxEnv(
ctx context.Context,
) (map[string]string, error) {
defer debug.FunctionTimer().End()

// When ensurePackagesAreInstalled is called with ensure=true, it always
// When ensureProjectStateIsCurrent 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.ensureProjectStateIsCurrent(ctx, ensure); err != nil && !strings.Contains(err.Error(), "no such host") {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion internal/impl/devopt/devboxopts.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type UpdateOpts struct {
IgnoreMissingPackages bool
}

type NixEnvOpts struct {
type DevboxEnvExports struct {
DontRecomputeEnvironment bool
NoRefreshAlias bool
RunHooks bool
Expand Down
26 changes: 12 additions & 14 deletions internal/impl/envpath/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
const (
// PathStackEnv stores the string representation of the stack, as a ":" separated list.
// Each element in the list is also the key to the env-var that stores the
// nixEnvPath for that devbox-project. Except for the last element, which is InitPathEnv.
// devboxEnvPath for that devbox-project. Except for the last element, which is InitPathEnv.
PathStackEnv = "DEVBOX_PATH_STACK"

// InitPathEnv stores the path prior to any devbox shellenv modifying the environment
Expand All @@ -19,9 +19,9 @@ const (

// stack has the following design:
// 1. The stack enables tracking which sub-paths in PATH come from which devbox-project
// 2. It is an ordered-list of keys to env-vars that store nixEnvPath values of devbox-projects.
// 3. The final PATH is reconstructed by concatenating the env-var values of each nixEnvPathKey.
// 5. The stack is stored in its own env-var PathStackEnv, shared by all devbox-projects in this shell.
// 2. It is an ordered-list of keys to env-vars that store devboxEnvPath values of devbox-projects.
// 3. The final PATH is reconstructed by concatenating the env-var values of each of these keys.
// 4. The stack is stored in its own env-var PathStackEnv, shared by all devbox-projects in this shell.
type stack struct {
// keys holds the stack elements.
// Earlier (lower index number) keys get higher priority.
Expand Down Expand Up @@ -56,28 +56,27 @@ func (s *stack) Path(env map[string]string) string {
}

// Key is the element stored in the stack for a devbox-project. It represents
// a pointer to the nixEnvPath value stored in its own env-var, also using this same
// Key.
// a pointer to the devboxEnvPath value stored in its own env-var, also using this same Key.
func Key(projectHash string) string {
return "DEVBOX_NIX_ENV_PATH_" + projectHash
}

// Push adds the new nixEnvPath for the devbox-project identified by projectHash.
// The nixEnvPath is pushed to the top of the stack (given highest priority),
// Push adds the new PATH for the devbox-project identified by projectHash.
// This PATH is pushed to the top of the stack (given highest priority),
// unless preservePathStack is enabled.
//
// It also updates the env by modifying the PathStack env-var, and the env-var
// for storing the nixEnvPath.
// for storing this path.
func (s *stack) Push(
env map[string]string,
projectHash string,
nixEnvPath string,
path string, // new PATH of the devbox-project of projectHash
preservePathStack bool,
) {
key := Key(projectHash)

// Add this nixEnvPath to env
env[key] = nixEnvPath
// Add this path to env
env[key] = path

// Common case: ensure this key is at the top of the stack
if !preservePathStack ||
Expand All @@ -90,8 +89,7 @@ func (s *stack) Push(
env[PathStackEnv] = s.String()
}

// Has tests if the stack has the specified key. Refer to the Key function for constructing
// the appropriate key for any devbox-project.
// Has tests if the stack has the key corresponding to projectHash
func (s *stack) Has(projectHash string) bool {
return lo.Contains(s.keys, Key(projectHash))
}
12 changes: 6 additions & 6 deletions internal/impl/envpath/stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ func TestNewStack(t *testing.T) {
// would usually be independent.
testSteps := []struct {
projectHash string
nixEnvPath string
devboxEnvPath string
preservePathStack bool
expectedKeysLength int
expectedEnv map[string]string
}{
{
projectHash: "fooProjectHash",
nixEnvPath: "/foo1:/foo2",
devboxEnvPath: "/foo1:/foo2",
preservePathStack: false,
expectedKeysLength: 2,
expectedEnv: map[string]string{
Expand All @@ -46,7 +46,7 @@ func TestNewStack(t *testing.T) {
},
{
projectHash: "barProjectHash",
nixEnvPath: "/bar1:/bar2",
devboxEnvPath: "/bar1:/bar2",
preservePathStack: false,
expectedKeysLength: 3,
expectedEnv: map[string]string{
Expand All @@ -58,7 +58,7 @@ func TestNewStack(t *testing.T) {
},
{
projectHash: "fooProjectHash",
nixEnvPath: "/foo3:/foo2",
devboxEnvPath: "/foo3:/foo2",
preservePathStack: false,
expectedKeysLength: 3,
expectedEnv: map[string]string{
Expand All @@ -70,7 +70,7 @@ func TestNewStack(t *testing.T) {
},
{
projectHash: "barProjectHash",
nixEnvPath: "/bar3:/bar2",
devboxEnvPath: "/bar3:/bar2",
preservePathStack: true,
expectedKeysLength: 3,
expectedEnv: map[string]string{
Expand All @@ -86,7 +86,7 @@ func TestNewStack(t *testing.T) {
t.Run(
fmt.Sprintf("step_%d", idx), func(t *testing.T) {
// Push to stack and update PATH env
stack.Push(env, testStep.projectHash, testStep.nixEnvPath, testStep.preservePathStack)
stack.Push(env, testStep.projectHash, testStep.devboxEnvPath, testStep.preservePathStack)
env["PATH"] = stack.Path(env)

if len(stack.keys) != testStep.expectedKeysLength {
Expand Down
6 changes: 4 additions & 2 deletions internal/impl/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,14 @@ const (
ensure installMode = "ensure"
)

// ensureProjectStateIsCurrent ensures:
// ensureProjectStateIsCurrent ensures the Devbox project state is up to date.
// Namely:
// 1. Packages are installed, in nix-profile or runx.
// Extraneous packages are removed (references purged, not uninstalled).
// 2. Plugins are installed
// 3. Files for devbox shellenv are generated
// 4. Lockfile is synced
// 4. The Devbox environment is re-computed, if necessary, and cached
// 5. Lockfile is synced
//
// The `mode` is used for:
// 1. Skipping certain operations that may not apply.
Expand Down

0 comments on commit 3f884b3

Please sign in to comment.