Skip to content

Commit 9542b07

Browse files
author
savil
committed
recompute env in shell and run
1 parent 399b7f3 commit 9542b07

File tree

5 files changed

+52
-48
lines changed

5 files changed

+52
-48
lines changed

internal/boxcli/run.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ import (
2121

2222
type runCmdFlags struct {
2323
envFlag
24-
config configFlags
25-
omitNixEnv bool
26-
pure bool
27-
listScripts bool
24+
config configFlags
25+
omitNixEnv bool
26+
pure bool
27+
listScripts bool
28+
recomputeEnv bool
2829
}
2930

3031
// runFlagDefaults are the flag default values that differ
@@ -62,6 +63,7 @@ func runCmd(defaults runFlagDefaults) *cobra.Command {
6263
"shell environment will omit the env-vars from print-dev-env",
6364
)
6465
_ = command.Flags().MarkHidden("omit-nix-env")
66+
command.Flags().BoolVar(&flags.recomputeEnv, "recompute-env", false, "recompute environment if needed")
6567

6668
command.ValidArgs = listScripts(command, flags)
6769

@@ -110,10 +112,11 @@ func runScriptCmd(cmd *cobra.Command, args []string, flags runCmdFlags) error {
110112

111113
// Check the directory exists.
112114
box, err := devbox.Open(&devopt.Opts{
113-
Dir: path,
114-
Environment: flags.config.environment,
115-
Stderr: cmd.ErrOrStderr(),
116-
Env: env,
115+
Dir: path,
116+
Env: env,
117+
Environment: flags.config.environment,
118+
RecomputeEnv: flags.recomputeEnv,
119+
Stderr: cmd.ErrOrStderr(),
117120
})
118121
if err != nil {
119122
return redact.Errorf("error reading devbox.json: %w", err)

internal/boxcli/shell.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import (
1717

1818
type shellCmdFlags struct {
1919
envFlag
20-
config configFlags
21-
omitNixEnv bool
22-
printEnv bool
23-
pure bool
20+
config configFlags
21+
omitNixEnv bool
22+
printEnv bool
23+
pure bool
24+
recomputeEnv bool
2425
}
2526

2627
// shellFlagDefaults are the flag default values that differ
@@ -53,6 +54,7 @@ func shellCmd(defaults shellFlagDefaults) *cobra.Command {
5354
"shell environment will omit the env-vars from print-dev-env",
5455
)
5556
_ = command.Flags().MarkHidden("omit-nix-env")
57+
command.Flags().BoolVar(&flags.recomputeEnv, "recompute-env", false, "recompute environment if needed")
5658

5759
flags.config.register(command)
5860
flags.envFlag.register(command)
@@ -92,8 +94,9 @@ func runShellCmd(cmd *cobra.Command, flags shellCmdFlags) error {
9294
}
9395

9496
return box.Shell(cmd.Context(), devopt.EnvOptions{
95-
OmitNixEnv: flags.omitNixEnv,
96-
Pure: flags.pure,
97+
OmitNixEnv: flags.omitNixEnv,
98+
Pure: flags.pure,
99+
RecomputeEnv: flags.recomputeEnv,
97100
})
98101
}
99102

internal/boxcli/shellenv.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ func shellEnvFunc(
116116
}
117117

118118
envStr, err := box.EnvExports(ctx, devopt.EnvExportsOpts{
119-
DontRecomputeEnvironment: !flags.recomputeEnv,
120119
EnvOptions: devopt.EnvOptions{
121120
OmitNixEnv: flags.omitNixEnv,
122121
PreservePathStack: flags.preservePathStack,
123122
Pure: flags.pure,
123+
RecomputeEnv: flags.recomputeEnv,
124124
},
125125
NoRefreshAlias: flags.noRefreshAlias,
126126
RunHooks: flags.runInitHook,

internal/devbox/devbox.go

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -351,22 +351,7 @@ func (d *Devbox) EnvExports(ctx context.Context, opts devopt.EnvExportsOpts) (st
351351
var envs map[string]string
352352
var err error
353353

354-
if opts.DontRecomputeEnvironment {
355-
upToDate, _ := d.lockfile.IsUpToDateAndInstalled(isFishShell())
356-
if !upToDate {
357-
ux.FHidableWarning(
358-
ctx,
359-
d.stderr,
360-
StateOutOfDateMessage,
361-
d.refreshAliasOrCommand(),
362-
)
363-
}
364-
365-
envs, err = d.computeEnv(ctx, true /*usePrintDevEnvCache*/, opts.EnvOptions)
366-
} else {
367-
envs, err = d.ensureStateIsUpToDateAndComputeEnv(ctx, opts.EnvOptions)
368-
}
369-
354+
envs, err = d.ensureStateIsUpToDateAndComputeEnv(ctx, opts.EnvOptions)
370355
if err != nil {
371356
return "", err
372357
}
@@ -819,23 +804,35 @@ func (d *Devbox) ensureStateIsUpToDateAndComputeEnv(
819804
) (map[string]string, error) {
820805
defer debug.FunctionTimer().End()
821806

822-
// When ensureStateIsUpToDate is called with ensure=true, it always
823-
// returns early if the lockfile is up to date. So we don't need to check here
824-
if err := d.ensureStateIsUpToDate(ctx, ensure); isConnectionError(err) {
825-
if !fileutil.Exists(d.nixPrintDevEnvCachePath()) {
826-
ux.Ferrorf(
807+
if !envOpts.RecomputeEnv {
808+
upToDate, _ := d.lockfile.IsUpToDateAndInstalled(isFishShell())
809+
if !upToDate {
810+
ux.FHidableWarning(
811+
ctx,
827812
d.stderr,
828-
"Error connecting to the internet and no cached environment found. Aborting.\n",
813+
StateOutOfDateMessage,
814+
d.refreshAliasOrCommand(),
829815
)
816+
}
817+
} else {
818+
// When ensureStateIsUpToDate is called with ensure=true, it always
819+
// returns early if the lockfile is up to date. So we don't need to check here
820+
if err := d.ensureStateIsUpToDate(ctx, ensure); isConnectionError(err) {
821+
if !fileutil.Exists(d.nixPrintDevEnvCachePath()) {
822+
ux.Ferrorf(
823+
d.stderr,
824+
"Error connecting to the internet and no cached environment found. Aborting.\n",
825+
)
826+
return nil, err
827+
}
828+
ux.Fwarningf(
829+
d.stderr,
830+
"Error connecting to the internet. Will attempt to use cached environment.\n",
831+
)
832+
} else if err != nil {
833+
// Some other non connection error, just return it.
830834
return nil, err
831835
}
832-
ux.Fwarningf(
833-
d.stderr,
834-
"Error connecting to the internet. Will attempt to use cached environment.\n",
835-
)
836-
} else if err != nil {
837-
// Some other non connection error, just return it.
838-
return nil, err
839836
}
840837

841838
// Since ensureStateIsUpToDate calls computeEnv when not up do date,

internal/devbox/devopt/devboxopts.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type Opts struct {
1414
Environment string
1515
IgnoreWarnings bool
1616
CustomProcessComposeFile string
17+
RecomputeEnv bool
1718
Stderr io.Writer
1819
}
1920

@@ -62,10 +63,9 @@ type UpdateOpts struct {
6263
}
6364

6465
type EnvExportsOpts struct {
65-
DontRecomputeEnvironment bool
66-
EnvOptions EnvOptions
67-
NoRefreshAlias bool
68-
RunHooks bool
66+
EnvOptions EnvOptions
67+
NoRefreshAlias bool
68+
RunHooks bool
6969
}
7070

7171
// EnvOptions configure the Devbox Environment in the `computeEnv` function.
@@ -76,4 +76,5 @@ type EnvOptions struct {
7676
OmitNixEnv bool
7777
PreservePathStack bool
7878
Pure bool
79+
RecomputeEnv bool
7980
}

0 commit comments

Comments
 (0)