Skip to content

Commit e0aad3e

Browse files
authored
[Renaming] ComputeNixEnv -> ComputeEnv; ensurePackagesAreInstalled -> ensureStateIsCurrent (#1675)
## Summary These names were getting rather stale. Hopefully, the new ones represent them more accurately. ## How was it tested? compiles and cicd tests should pass
1 parent 13f558b commit e0aad3e

File tree

11 files changed

+93
-80
lines changed

11 files changed

+93
-80
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+
EnvExports(ctx context.Context, opts devopt.EnvExportsOpts) (string, error)
2727
PackageNames() []string
2828
ProjectDir() string
2929
Pull(ctx context.Context, opts devopt.PullboxOpts) error
-45.1 KB
Binary file not shown.

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.EnvExports(cmd.Context(), devopt.EnvExportsOpts{})
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.EnvExports(cmd.Context(), devopt.EnvExportsOpts{
9898
DontRecomputeEnvironment: !recomputeEnvIfNeeded,
9999
NoRefreshAlias: flags.noRefreshAlias,
100100
RunHooks: flags.runInitHook,

internal/impl/devbox.go

Lines changed: 38 additions & 28 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.ensureStateIsUpToDateAndComputeEnv(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.ensureStateIsUpToDateAndComputeEnv(ctx)
222222
if err != nil {
223223
return err
224224
}
@@ -260,7 +260,7 @@ func (d *Devbox) Install(ctx context.Context) error {
260260
ctx, task := trace.NewTask(ctx, "devboxInstall")
261261
defer task.End()
262262

263-
return d.ensurePackagesAreInstalled(ctx, ensure)
263+
return d.ensureStateIsUpToDate(ctx, ensure)
264264
}
265265

266266
func (d *Devbox) ListScripts() []string {
@@ -274,8 +274,11 @@ func (d *Devbox) ListScripts() []string {
274274
return keys
275275
}
276276

277-
func (d *Devbox) NixEnv(ctx context.Context, opts devopt.NixEnvOpts) (string, error) {
278-
ctx, task := trace.NewTask(ctx, "devboxNixEnv")
277+
// EnvExports 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) EnvExports(ctx context.Context, opts devopt.EnvExportsOpts) (string, error) {
281+
ctx, task := trace.NewTask(ctx, "devboxEnvExports")
279282
defer task.End()
280283

281284
var envs map[string]string
@@ -295,9 +298,9 @@ func (d *Devbox) NixEnv(ctx context.Context, opts devopt.NixEnvOpts) (string, er
295298
)
296299
}
297300

298-
envs, err = d.computeNixEnv(ctx, true /*usePrintDevEnvCache*/)
301+
envs, err = d.computeEnv(ctx, true /*usePrintDevEnvCache*/)
299302
} else {
300-
envs, err = d.ensurePackagesAreInstalledAndComputeEnv(ctx)
303+
envs, err = d.ensureStateIsUpToDateAndComputeEnv(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.ensureStateIsUpToDateAndComputeEnv(ctx)
326329
if err != nil {
327330
return nil, err
328331
}
@@ -482,7 +485,7 @@ func (d *Devbox) GenerateEnvrcFile(ctx context.Context, force bool, envFlags dev
482485
}
483486

484487
// generate all shell files to ensure we can refer to them in the .envrc script
485-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil {
488+
if err := d.ensureStateIsUpToDate(ctx, ensure); err != nil {
486489
return err
487490
}
488491

@@ -754,7 +757,7 @@ func (d *Devbox) StartProcessManager(
754757
)
755758
}
756759

757-
// computeNixEnv computes the set of environment variables that define a Devbox
760+
// computeEnv computes the set of environment variables that define a Devbox
758761
// environment. The "devbox run" and "devbox shell" commands source these
759762
// variables into a shell before executing a command or showing an interactive
760763
// prompt.
@@ -778,11 +781,10 @@ func (d *Devbox) StartProcessManager(
778781
// programs.
779782
//
780783
// Note that the shellrc.tmpl template (which sources this environment) does
781-
// some additional processing. The computeNixEnv environment won't necessarily
784+
// some additional processing. The computeEnv environment won't necessarily
782785
// represent the final "devbox run" or "devbox shell" environments.
783-
// TODO: Rename to computeDevboxEnv?
784-
func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (map[string]string, error) {
785-
defer trace.StartRegion(ctx, "computeNixEnv").End()
786+
func (d *Devbox) computeEnv(ctx context.Context, usePrintDevEnvCache bool) (map[string]string, error) {
787+
defer trace.StartRegion(ctx, "devboxComputeEnv").End()
786788

787789
// Append variables from current env if --pure is not passed
788790
currentEnv := os.Environ()
@@ -896,15 +898,21 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
896898

897899
markEnvsAsSetByDevbox(pluginEnv, configEnv)
898900

899-
nixEnvPath := env["PATH"]
900-
debug.Log("PATH after plugins and config is: %s", nixEnvPath)
901+
// devboxEnvPath starts with the initial PATH from print-dev-env, and is
902+
// transformed to be the "PATH of the Devbox environment"
903+
// TODO: The prior statement is not fully true,
904+
// since env["PATH"] is written to above and so it is already no longer "PATH
905+
// from print-dev-env". Consider moving devboxEnvPath higher up in this function
906+
// where env["PATH"] is written to.
907+
devboxEnvPath := env["PATH"]
908+
debug.Log("PATH after plugins and config is: %s", devboxEnvPath)
901909

902910
// We filter out nix store paths of devbox-packages (represented here as buildInputs).
903911
// Motivation: if a user removes a package from their devbox it should no longer
904912
// be available in their environment.
905913
buildInputs := strings.Split(env["buildInputs"], " ")
906914
var glibcPatchPath []string
907-
nixEnvPath = filterPathList(nixEnvPath, func(path string) bool {
915+
devboxEnvPath = filterPathList(devboxEnvPath, func(path string) bool {
908916
// TODO(gcurtis): this is a massive hack. Please get rid
909917
// of this and install the package to the profile.
910918
if strings.Contains(path, "patched-glibc") {
@@ -921,24 +929,24 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
921929
}
922930
return true
923931
})
924-
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, nixEnvPath)
932+
debug.Log("PATH after filtering with buildInputs (%v) is: %s", buildInputs, devboxEnvPath)
925933

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

934942
runXPaths, err := d.RunXPaths(ctx)
935943
if err != nil {
936944
return nil, err
937945
}
938-
nixEnvPath = envpath.JoinPathLists(nixEnvPath, runXPaths)
946+
devboxEnvPath = envpath.JoinPathLists(devboxEnvPath, runXPaths)
939947

940948
pathStack := envpath.Stack(env, originalEnv)
941-
pathStack.Push(env, d.projectDirHash(), nixEnvPath, d.preservePathStack)
949+
pathStack.Push(env, d.projectDirHash(), devboxEnvPath, d.preservePathStack)
942950
env["PATH"] = pathStack.Path(env)
943951
debug.Log("New path stack is: %s", pathStack)
944952

@@ -958,25 +966,27 @@ func (d *Devbox) computeNixEnv(ctx context.Context, usePrintDevEnvCache bool) (m
958966
return env, d.addHashToEnv(env)
959967
}
960968

961-
// ensurePackagesAreInstalledAndComputeEnv does what it says :P
962-
func (d *Devbox) ensurePackagesAreInstalledAndComputeEnv(
969+
// ensureStateIsUpToDateAndComputeEnv will return a map of the env-vars for the Devbox Environment
970+
// while ensuring these reflect the current (up to date) state of the project.
971+
// TODO: find a better name for this function.
972+
func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
963973
ctx context.Context,
964974
) (map[string]string, error) {
965975
defer debug.FunctionTimer().End()
966976

967-
// When ensurePackagesAreInstalled is called with ensure=true, it always
977+
// When ensureStateIsUpToDate is called with ensure=true, it always
968978
// returns early if the lockfile is up to date. So we don't need to check here
969-
if err := d.ensurePackagesAreInstalled(ctx, ensure); err != nil && !strings.Contains(err.Error(), "no such host") {
979+
if err := d.ensureStateIsUpToDate(ctx, ensure); err != nil && !strings.Contains(err.Error(), "no such host") {
970980
return nil, err
971981
} else if err != nil {
972982
ux.Fwarning(d.stderr, "Error connecting to the internet. Will attempt to use cached environment.\n")
973983
}
974984

975-
// Since ensurePackagesAreInstalled calls computeNixEnv when not up do date,
985+
// Since ensureStateIsUpToDate calls computeEnv when not up do date,
976986
// it's ok to use usePrintDevEnvCache=true here always. This does end up
977987
// doing some non-nix work twice if lockfile is not up to date.
978988
// TODO: Improve this to avoid extra work.
979-
return d.computeNixEnv(ctx, true /*usePrintDevEnvCache*/)
989+
return d.computeEnv(ctx, true /*usePrintDevEnvCache*/)
980990
}
981991

982992
func (d *Devbox) nixPrintDevEnvCachePath() string {

internal/impl/devbox_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,21 @@ func (n *testNix) PrintDevEnv(ctx context.Context, args *nix.PrintDevEnvArgs) (*
6767
}, nil
6868
}
6969

70-
func TestComputeNixEnv(t *testing.T) {
70+
func TestComputeEnv(t *testing.T) {
7171
d := devboxForTesting(t)
7272
d.nix = &testNix{}
7373
ctx := context.Background()
74-
env, err := d.computeNixEnv(ctx, false /*use cache*/)
75-
require.NoError(t, err, "computeNixEnv should not fail")
76-
assert.NotNil(t, env, "computeNixEnv should return a valid env")
74+
env, err := d.computeEnv(ctx, false /*use cache*/)
75+
require.NoError(t, err, "computeEnv should not fail")
76+
assert.NotNil(t, env, "computeEnv should return a valid env")
7777
}
7878

79-
func TestComputeNixPathIsIdempotent(t *testing.T) {
79+
func TestComputeDevboxPathIsIdempotent(t *testing.T) {
8080
devbox := devboxForTesting(t)
8181
devbox.nix = &testNix{"/tmp/my/path"}
8282
ctx := context.Background()
83-
env, err := devbox.computeNixEnv(ctx, false /*use cache*/)
84-
require.NoError(t, err, "computeNixEnv should not fail")
83+
env, err := devbox.computeEnv(ctx, false /*use cache*/)
84+
require.NoError(t, err, "computeEnv should not fail")
8585
path := env["PATH"]
8686
assert.NotEmpty(t, path, "path should not be nil")
8787

@@ -90,19 +90,19 @@ func TestComputeNixPathIsIdempotent(t *testing.T) {
9090
t.Setenv(envpath.PathStackEnv, env[envpath.PathStackEnv])
9191
t.Setenv(envpath.Key(devbox.projectDirHash()), env[envpath.Key(devbox.projectDirHash())])
9292

93-
env, err = devbox.computeNixEnv(ctx, false /*use cache*/)
94-
require.NoError(t, err, "computeNixEnv should not fail")
93+
env, err = devbox.computeEnv(ctx, false /*use cache*/)
94+
require.NoError(t, err, "computeEnv should not fail")
9595
path2 := env["PATH"]
9696

9797
assert.Equal(t, path, path2, "path should be the same")
9898
}
9999

100-
func TestComputeNixPathWhenRemoving(t *testing.T) {
100+
func TestComputeDevboxPathWhenRemoving(t *testing.T) {
101101
devbox := devboxForTesting(t)
102102
devbox.nix = &testNix{"/tmp/my/path"}
103103
ctx := context.Background()
104-
env, err := devbox.computeNixEnv(ctx, false /*use cache*/)
105-
require.NoError(t, err, "computeNixEnv should not fail")
104+
env, err := devbox.computeEnv(ctx, false /*use cache*/)
105+
require.NoError(t, err, "computeEnv should not fail")
106106
path := env["PATH"]
107107
assert.NotEmpty(t, path, "path should not be nil")
108108
assert.Contains(t, path, "/tmp/my/path", "path should contain /tmp/my/path")
@@ -113,8 +113,8 @@ func TestComputeNixPathWhenRemoving(t *testing.T) {
113113
t.Setenv(envpath.Key(devbox.projectDirHash()), env[envpath.Key(devbox.projectDirHash())])
114114

115115
devbox.nix.(*testNix).path = ""
116-
env, err = devbox.computeNixEnv(ctx, false /*use cache*/)
117-
require.NoError(t, err, "computeNixEnv should not fail")
116+
env, err = devbox.computeEnv(ctx, false /*use cache*/)
117+
require.NoError(t, err, "computeEnv should not fail")
118118
path2 := env["PATH"]
119119
assert.NotContains(t, path2, "/tmp/my/path", "path should not contain /tmp/my/path")
120120

internal/impl/devopt/devboxopts.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import (
44
"io"
55
)
66

7+
// Naming Convention:
8+
// - suffix Opts for structs corresponding to a Devbox api function
9+
// - omit suffix Opts for other structs that are composed into an Opts struct
10+
711
type Opts struct {
812
Dir string
913
Env map[string]string
@@ -51,7 +55,7 @@ type UpdateOpts struct {
5155
IgnoreMissingPackages bool
5256
}
5357

54-
type NixEnvOpts struct {
58+
type EnvExportsOpts struct {
5559
DontRecomputeEnvironment bool
5660
NoRefreshAlias bool
5761
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
}

0 commit comments

Comments
 (0)