Skip to content

Commit 5ed63b1

Browse files
committed
requested changes
1 parent beb4a7d commit 5ed63b1

File tree

9 files changed

+56
-45
lines changed

9 files changed

+56
-45
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-
DevboxEnvExports(ctx context.Context, opts devopt.DevboxEnvExports) (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.DevboxEnvExports(cmd.Context(), devopt.DevboxEnvExports{})
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.DevboxEnvExports(cmd.Context(), devopt.DevboxEnvExports{
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: 27 additions & 20 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.computeCurrentDevboxEnv(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.computeCurrentDevboxEnv(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.ensureProjectStateIsCurrent(ctx, ensure)
263+
return d.ensureStateIsCurrent(ctx, ensure)
264264
}
265265

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

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

284284
var envs map[string]string
@@ -298,9 +298,9 @@ func (d *Devbox) DevboxEnvExports(ctx context.Context, opts devopt.DevboxEnvExpo
298298
)
299299
}
300300

301-
envs, err = d.computeDevboxEnv(ctx, true /*usePrintDevEnvCache*/)
301+
envs, err = d.computeEnv(ctx, true /*usePrintDevEnvCache*/)
302302
} else {
303-
envs, err = d.computeCurrentDevboxEnv(ctx)
303+
envs, err = d.ensureStateIsUpToDateAndComputeEnv(ctx)
304304
}
305305

306306
if err != nil {
@@ -325,7 +325,7 @@ func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) {
325325
ctx, task := trace.NewTask(ctx, "devboxEnvVars")
326326
defer task.End()
327327
// this only returns env variables for the shell environment excluding hooks
328-
envs, err := d.computeCurrentDevboxEnv(ctx)
328+
envs, err := d.ensureStateIsUpToDateAndComputeEnv(ctx)
329329
if err != nil {
330330
return nil, err
331331
}
@@ -485,7 +485,7 @@ func (d *Devbox) GenerateEnvrcFile(ctx context.Context, force bool, envFlags dev
485485
}
486486

487487
// generate all shell files to ensure we can refer to them in the .envrc script
488-
if err := d.ensureProjectStateIsCurrent(ctx, ensure); err != nil {
488+
if err := d.ensureStateIsCurrent(ctx, ensure); err != nil {
489489
return err
490490
}
491491

@@ -757,7 +757,7 @@ func (d *Devbox) StartProcessManager(
757757
)
758758
}
759759

760-
// computeDevboxEnv computes the set of environment variables that define a Devbox
760+
// computeEnv computes the set of environment variables that define a Devbox
761761
// environment. The "devbox run" and "devbox shell" commands source these
762762
// variables into a shell before executing a command or showing an interactive
763763
// prompt.
@@ -781,10 +781,10 @@ func (d *Devbox) StartProcessManager(
781781
// programs.
782782
//
783783
// Note that the shellrc.tmpl template (which sources this environment) does
784-
// some additional processing. The computeDevboxEnv environment won't necessarily
784+
// some additional processing. The computeEnv environment won't necessarily
785785
// represent the final "devbox run" or "devbox shell" environments.
786-
func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool) (map[string]string, error) {
787-
defer trace.StartRegion(ctx, "computeDevboxEnv").End()
786+
func (d *Devbox) computeEnv(ctx context.Context, usePrintDevEnvCache bool) (map[string]string, error) {
787+
defer trace.StartRegion(ctx, "devboxComputeEnv").End()
788788

789789
// Append variables from current env if --pure is not passed
790790
currentEnv := os.Environ()
@@ -898,6 +898,12 @@ func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool)
898898

899899
markEnvsAsSetByDevbox(pluginEnv, configEnv)
900900

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.
901907
devboxEnvPath := env["PATH"]
902908
debug.Log("PATH after plugins and config is: %s", devboxEnvPath)
903909

@@ -960,26 +966,27 @@ func (d *Devbox) computeDevboxEnv(ctx context.Context, usePrintDevEnvCache bool)
960966
return env, d.addHashToEnv(env)
961967
}
962968

963-
// computeCurrentDevboxEnv will return a map of the env-vars for the Devbox Environment
969+
// ensureStateIsUpToDateAndComputeEnv will return a map of the env-vars for the Devbox Environment
964970
// while ensuring these reflect the current (up to date) state of the project.
965-
func (d *Devbox) computeCurrentDevboxEnv(
971+
// TODO: find a better name for this function.
972+
func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
966973
ctx context.Context,
967974
) (map[string]string, error) {
968975
defer debug.FunctionTimer().End()
969976

970-
// When ensureProjectStateIsCurrent is called with ensure=true, it always
977+
// When ensureStateIsCurrent is called with ensure=true, it always
971978
// returns early if the lockfile is up to date. So we don't need to check here
972-
if err := d.ensureProjectStateIsCurrent(ctx, ensure); err != nil && !strings.Contains(err.Error(), "no such host") {
979+
if err := d.ensureStateIsCurrent(ctx, ensure); err != nil && !strings.Contains(err.Error(), "no such host") {
973980
return nil, err
974981
} else if err != nil {
975982
ux.Fwarning(d.stderr, "Error connecting to the internet. Will attempt to use cached environment.\n")
976983
}
977984

978-
// Since ensurePackagesAreInstalled calls computeDevboxEnv when not up do date,
985+
// Since ensureStateIsCurrent calls computeEnv when not up do date,
979986
// it's ok to use usePrintDevEnvCache=true here always. This does end up
980987
// doing some non-nix work twice if lockfile is not up to date.
981988
// TODO: Improve this to avoid extra work.
982-
return d.computeDevboxEnv(ctx, true /*usePrintDevEnvCache*/)
989+
return d.computeEnv(ctx, true /*usePrintDevEnvCache*/)
983990
}
984991

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

internal/impl/devbox_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ func TestComputeDevboxEnv(t *testing.T) {
7171
d := devboxForTesting(t)
7272
d.nix = &testNix{}
7373
ctx := context.Background()
74-
env, err := d.computeDevboxEnv(ctx, false /*use cache*/)
75-
require.NoError(t, err, "computeDevboxEnv should not fail")
76-
assert.NotNil(t, env, "computeDevboxEnv 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

7979
func TestComputeDevboxPathIsIdempotent(t *testing.T) {
8080
devbox := devboxForTesting(t)
8181
devbox.nix = &testNix{"/tmp/my/path"}
8282
ctx := context.Background()
83-
env, err := devbox.computeDevboxEnv(ctx, false /*use cache*/)
84-
require.NoError(t, err, "computeDevboxEnv 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,8 +90,8 @@ func TestComputeDevboxPathIsIdempotent(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.computeDevboxEnv(ctx, false /*use cache*/)
94-
require.NoError(t, err, "computeDevboxEnv 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")
@@ -101,8 +101,8 @@ func TestComputeDevboxPathWhenRemoving(t *testing.T) {
101101
devbox := devboxForTesting(t)
102102
devbox.nix = &testNix{"/tmp/my/path"}
103103
ctx := context.Background()
104-
env, err := devbox.computeDevboxEnv(ctx, false /*use cache*/)
105-
require.NoError(t, err, "computeDevboxEnv 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 TestComputeDevboxPathWhenRemoving(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.computeDevboxEnv(ctx, false /*use cache*/)
117-
require.NoError(t, err, "computeDevboxEnv 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 DevboxEnvExports struct {
58+
type EnvExportsOpts struct {
5559
DontRecomputeEnvironment bool
5660
NoRefreshAlias bool
5761
RunHooks bool

internal/impl/packages.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,12 @@ func (d *Devbox) Add(ctx context.Context, pkgsNames []string, opts devopt.AddOpt
9898
addedPackageNames = append(addedPackageNames, packageNameForConfig)
9999
}
100100

101-
// Options must be set before ensureProjectStateIsCurrent. See comment in function
101+
// Options must be set before ensureStateIsCurrent. See comment in function
102102
if err := d.setPackageOptions(addedPackageNames, opts); err != nil {
103103
return err
104104
}
105105

106-
if err := d.ensureProjectStateIsCurrent(ctx, install); err != nil {
106+
if err := d.ensureStateIsCurrent(ctx, install); err != nil {
107107
return usererr.WithUserMessage(err, "There was an error installing nix packages")
108108
}
109109

@@ -135,7 +135,7 @@ func (d *Devbox) setPackageOptions(pkgs []string, opts devopt.AddOpts) error {
135135
}
136136
}
137137

138-
// Resolving here ensures we allow insecure before running ensureProjectStateIsCurrent
138+
// Resolving here ensures we allow insecure before running ensureStateIsCurrent
139139
// which will call print-dev-env. Resolving does not save the lockfile, we
140140
// save at the end when everything has succeeded.
141141
if opts.AllowInsecure {
@@ -218,14 +218,14 @@ func (d *Devbox) Remove(ctx context.Context, pkgs ...string) error {
218218
}
219219

220220
// this will clean up the now-extra package from nix profile and the lockfile
221-
if err := d.ensureProjectStateIsCurrent(ctx, uninstall); err != nil {
221+
if err := d.ensureStateIsCurrent(ctx, uninstall); err != nil {
222222
return err
223223
}
224224

225225
return d.saveCfg()
226226
}
227227

228-
// installMode is an enum for helping with ensureProjectStateIsCurrent implementation
228+
// installMode is an enum for helping with ensureStateIsCurrent implementation
229229
type installMode string
230230

231231
const (
@@ -236,7 +236,7 @@ const (
236236
ensure installMode = "ensure"
237237
)
238238

239-
// ensureProjectStateIsCurrent ensures the Devbox project state is up to date.
239+
// ensureStateIsCurrent ensures the Devbox project state is up to date.
240240
// Namely:
241241
// 1. Packages are installed, in nix-profile or runx.
242242
// Extraneous packages are removed (references purged, not uninstalled).
@@ -248,8 +248,8 @@ const (
248248
// The `mode` is used for:
249249
// 1. Skipping certain operations that may not apply.
250250
// 2. User messaging to explain what operations are happening, because this function may take time to execute.
251-
func (d *Devbox) ensureProjectStateIsCurrent(ctx context.Context, mode installMode) error {
252-
defer trace.StartRegion(ctx, "ensureProjectStateIsCurrent").End()
251+
func (d *Devbox) ensureStateIsCurrent(ctx context.Context, mode installMode) error {
252+
defer trace.StartRegion(ctx, "devboxEnsureStateIsCurrent").End()
253253
defer debug.FunctionTimer().End()
254254

255255
// if mode is install or uninstall, then we need to update the nix-profile
@@ -293,7 +293,7 @@ func (d *Devbox) ensureProjectStateIsCurrent(ctx context.Context, mode installMo
293293
// Use the printDevEnvCache if we are adding or removing or updating any package,
294294
// AND we are not in the shellenv-enabled environment of the current devbox-project.
295295
usePrintDevEnvCache := mode != ensure && !d.IsEnvEnabled()
296-
if _, err := d.computeDevboxEnv(ctx, usePrintDevEnvCache); err != nil {
296+
if _, err := d.computeEnv(ctx, usePrintDevEnvCache); err != nil {
297297
return err
298298
}
299299

internal/impl/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (d *Devbox) Update(ctx context.Context, opts devopt.UpdateOpts) error {
6565
}
6666
}
6767

68-
if err := d.ensureProjectStateIsCurrent(ctx, update); err != nil {
68+
if err := d.ensureStateIsCurrent(ctx, update); err != nil {
6969
return err
7070
}
7171

0 commit comments

Comments
 (0)