Skip to content

Commit beb4a7d

Browse files
committed
rename moar functions
1 parent 18881de commit beb4a7d

File tree

8 files changed

+46
-42
lines changed

8 files changed

+46
-42
lines changed

devbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Devbox interface {
2323
Install(ctx context.Context) error
2424
IsEnvEnabled() bool
2525
ListScripts() []string
26-
NixEnv(ctx context.Context, opts devopt.NixEnvOpts) (string, error)
26+
DevboxEnvExports(ctx context.Context, opts devopt.DevboxEnvExports) (string, error)
2727
PackageNames() []string
2828
ProjectDir() string
2929
Pull(ctx context.Context, opts devopt.PullboxOpts) error

internal/boxcli/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func runShellCmd(cmd *cobra.Command, flags shellCmdFlags) error {
6767
if flags.printEnv {
6868
// false for includeHooks is because init hooks is not compatible with .envrc files generated
6969
// by versions older than 0.4.6
70-
script, err := box.NixEnv(cmd.Context(), devopt.NixEnvOpts{})
70+
script, err := box.DevboxEnvExports(cmd.Context(), devopt.DevboxEnvExports{})
7171
if err != nil {
7272
return err
7373
}

internal/boxcli/shellenv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func shellEnvFunc(
9494
}
9595
}
9696

97-
envStr, err := box.NixEnv(cmd.Context(), devopt.NixEnvOpts{
97+
envStr, err := box.DevboxEnvExports(cmd.Context(), devopt.DevboxEnvExports{
9898
DontRecomputeEnvironment: !recomputeEnvIfNeeded,
9999
NoRefreshAlias: flags.noRefreshAlias,
100100
RunHooks: flags.runInitHook,

internal/impl/devbox.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func (d *Devbox) Shell(ctx context.Context) error {
177177
ctx, task := trace.NewTask(ctx, "devboxShell")
178178
defer task.End()
179179

180-
envs, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
180+
envs, err := d.computeCurrentDevboxEnv(ctx)
181181
if err != nil {
182182
return err
183183
}
@@ -218,7 +218,7 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
218218
return err
219219
}
220220

221-
env, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
221+
env, err := d.computeCurrentDevboxEnv(ctx)
222222
if err != nil {
223223
return err
224224
}
@@ -274,7 +274,10 @@ func (d *Devbox) ListScripts() []string {
274274
return keys
275275
}
276276

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

@@ -297,7 +300,7 @@ func (d *Devbox) NixEnv(ctx context.Context, opts devopt.NixEnvOpts) (string, er
297300

298301
envs, err = d.computeDevboxEnv(ctx, true /*usePrintDevEnvCache*/)
299302
} else {
300-
envs, err = d.ensurePackagesAreInstalledAndComputeEnv(ctx)
303+
envs, err = d.computeCurrentDevboxEnv(ctx)
301304
}
302305

303306
if err != nil {
@@ -322,7 +325,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
322325
ctx, task := trace.NewTask(ctx, "devboxEnvVars")
323326
defer task.End()
324327
// this only returns env variables for the shell environment excluding hooks
325-
envs, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
328+
envs, err := d.computeCurrentDevboxEnv(ctx)
326329
if err != nil {
327330
return nil, err
328331
}
@@ -895,15 +898,15 @@ func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool)
895898

896899
markEnvsAsSetByDevbox(pluginEnv, configEnv)
897900

898-
nixEnvPath := env["PATH"]
899-
debug.Log("PATH after plugins and config is: %s", nixEnvPath)
901+
devboxEnvPath := env["PATH"]
902+
debug.Log("PATH after plugins and config is: %s", devboxEnvPath)
900903

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

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

933936
runXPaths, err := d.RunXPaths(ctx)
934937
if err != nil {
935938
return nil, err
936939
}
937-
nixEnvPath = envpath.JoinPathLists(nixEnvPath, runXPaths)
940+
devboxEnvPath = envpath.JoinPathLists(devboxEnvPath, runXPaths)
938941

939942
pathStack := envpath.Stack(env, originalEnv)
940-
pathStack.Push(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
943+
pathStack.Push(env, d.projectDirHash(), devboxEnvPath, d.preservePathStack)
941944
env["PATH"] = pathStack.Path(env)
942945
debug.Log("New path stack is: %s", pathStack)
943946

@@ -957,13 +960,14 @@ func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool)
957960
return env, d.addHashToEnv(env)
958961
}
959962

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

966-
// When ensurePackagesAreInstalled is called with ensure=true, it always
970+
// When ensureProjectStateIsCurrent is called with ensure=true, it always
967971
// returns early if the lockfile is up to date. So we don't need to check here
968972
if err := d.ensureProjectStateIsCurrent(ctx, ensure); err != nil && !strings.Contains(err.Error(), "no such host") {
969973
return nil, err

internal/impl/devopt/devboxopts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type UpdateOpts struct {
5151
IgnoreMissingPackages bool
5252
}
5353

54-
type NixEnvOpts struct {
54+
type DevboxEnvExports struct {
5555
DontRecomputeEnvironment bool
5656
NoRefreshAlias bool
5757
RunHooks bool

internal/impl/envpath/stack.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
const (
1111
// PathStackEnv stores the string representation of the stack, as a ":" separated list.
1212
// Each element in the list is also the key to the env-var that stores the
13-
// nixEnvPath for that devbox-project. Except for the last element, which is InitPathEnv.
13+
// devboxEnvPath for that devbox-project. Except for the last element, which is InitPathEnv.
1414
PathStackEnv = "DEVBOX_PATH_STACK"
1515

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

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

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

65-
// Push adds the new nixEnvPath for the devbox-project identified by projectHash.
66-
// The nixEnvPath is pushed to the top of the stack (given highest priority),
64+
// Push adds the new PATH for the devbox-project identified by projectHash.
65+
// This PATH is pushed to the top of the stack (given highest priority),
6766
// unless preservePathStack is enabled.
6867
//
6968
// It also updates the env by modifying the PathStack env-var, and the env-var
70-
// for storing the nixEnvPath.
69+
// for storing this path.
7170
func (s *stack) Push(
7271
env map[string]string,
7372
projectHash string,
74-
nixEnvPath string,
73+
path string, // new PATH of the devbox-project of projectHash
7574
preservePathStack bool,
7675
) {
7776
key := Key(projectHash)
7877

79-
// Add this nixEnvPath to env
80-
env[key] = nixEnvPath
78+
// Add this path to env
79+
env[key] = path
8180

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

93-
// Has tests if the stack has the specified key. Refer to the Key function for constructing
94-
// the appropriate key for any devbox-project.
92+
// Has tests if the stack has the key corresponding to projectHash
9593
func (s *stack) Has(projectHash string) bool {
9694
return lo.Contains(s.keys, Key(projectHash))
9795
}

internal/impl/envpath/stack_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ func TestNewStack(t *testing.T) {
2828
// would usually be independent.
2929
testSteps := []struct {
3030
projectHash string
31-
nixEnvPath string
31+
devboxEnvPath string
3232
preservePathStack bool
3333
expectedKeysLength int
3434
expectedEnv map[string]string
3535
}{
3636
{
3737
projectHash: "fooProjectHash",
38-
nixEnvPath: "/foo1:/foo2",
38+
devboxEnvPath: "/foo1:/foo2",
3939
preservePathStack: false,
4040
expectedKeysLength: 2,
4141
expectedEnv: map[string]string{
@@ -46,7 +46,7 @@ func TestNewStack(t *testing.T) {
4646
},
4747
{
4848
projectHash: "barProjectHash",
49-
nixEnvPath: "/bar1:/bar2",
49+
devboxEnvPath: "/bar1:/bar2",
5050
preservePathStack: false,
5151
expectedKeysLength: 3,
5252
expectedEnv: map[string]string{
@@ -58,7 +58,7 @@ func TestNewStack(t *testing.T) {
5858
},
5959
{
6060
projectHash: "fooProjectHash",
61-
nixEnvPath: "/foo3:/foo2",
61+
devboxEnvPath: "/foo3:/foo2",
6262
preservePathStack: false,
6363
expectedKeysLength: 3,
6464
expectedEnv: map[string]string{
@@ -70,7 +70,7 @@ func TestNewStack(t *testing.T) {
7070
},
7171
{
7272
projectHash: "barProjectHash",
73-
nixEnvPath: "/bar3:/bar2",
73+
devboxEnvPath: "/bar3:/bar2",
7474
preservePathStack: true,
7575
expectedKeysLength: 3,
7676
expectedEnv: map[string]string{
@@ -86,7 +86,7 @@ func TestNewStack(t *testing.T) {
8686
t.Run(
8787
fmt.Sprintf("step_%d", idx), func(t *testing.T) {
8888
// Push to stack and update PATH env
89-
stack.Push(env, testStep.projectHash, testStep.nixEnvPath, testStep.preservePathStack)
89+
stack.Push(env, testStep.projectHash, testStep.devboxEnvPath, testStep.preservePathStack)
9090
env["PATH"] = stack.Path(env)
9191

9292
if len(stack.keys) != testStep.expectedKeysLength {

internal/impl/packages.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,14 @@ const (
236236
ensure installMode = "ensure"
237237
)
238238

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

0 commit comments

Comments
 (0)