Skip to content

Commit 3f884b3

Browse files
committed
rename moar functions
1 parent 96630ea commit 3f884b3

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
@@ -66,7 +66,7 @@ func runShellCmd(cmd *cobra.Command, flags shellCmdFlags) error {
6666
if flags.printEnv {
6767
// false for includeHooks is because init hooks is not compatible with .envrc files generated
6868
// by versions older than 0.4.6
69-
script, err := box.NixEnv(cmd.Context(), devopt.NixEnvOpts{})
69+
script, err := box.DevboxEnvExports(cmd.Context(), devopt.DevboxEnvExports{})
7070
if err != nil {
7171
return err
7272
}

internal/boxcli/shellenv.go

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

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

internal/impl/devbox.go

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

173-
envs, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
173+
envs, err := d.computeCurrentDevboxEnv(ctx)
174174
if err != nil {
175175
return err
176176
}
@@ -211,7 +211,7 @@ func (d *Devbox) RunScript(ctx context.Context, cmdName string, cmdArgs []string
211211
return err
212212
}
213213

214-
env, err := d.ensurePackagesAreInstalledAndComputeEnv(ctx)
214+
env, err := d.computeCurrentDevboxEnv(ctx)
215215
if err != nil {
216216
return err
217217
}
@@ -267,7 +267,10 @@ func (d *Devbox) ListScripts() []string {
267267
return keys
268268
}
269269

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

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

291294
envs, err = d.computeDevboxEnv(ctx, true /*usePrintDevEnvCache*/)
292295
} else {
293-
envs, err = d.ensurePackagesAreInstalledAndComputeEnv(ctx)
296+
envs, err = d.computeCurrentDevboxEnv(ctx)
294297
}
295298

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

889892
markEnvsAsSetByDevbox(pluginEnv, configEnv)
890893

891-
nixEnvPath := env["PATH"]
892-
debug.Log("PATH after plugins and config is: %s", nixEnvPath)
894+
devboxEnvPath := env["PATH"]
895+
debug.Log("PATH after plugins and config is: %s", devboxEnvPath)
893896

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

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

926929
runXPaths, err := d.RunXPaths(ctx)
927930
if err != nil {
928931
return nil, err
929932
}
930-
nixEnvPath = envpath.JoinPathLists(nixEnvPath, runXPaths)
933+
devboxEnvPath = envpath.JoinPathLists(devboxEnvPath, runXPaths)
931934

932935
pathStack := envpath.Stack(env, originalEnv)
933-
pathStack.Push(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
936+
pathStack.Push(env, d.projectDirHash(), devboxEnvPath, d.preservePathStack)
934937
env["PATH"] = pathStack.Path(env)
935938
debug.Log("New path stack is: %s", pathStack)
936939

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

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

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

internal/impl/devopt/devboxopts.go

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

53-
type NixEnvOpts struct {
53+
type DevboxEnvExports struct {
5454
DontRecomputeEnvironment bool
5555
NoRefreshAlias bool
5656
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)