Add --envrc-dir flag to allow specifying location of direnv config by pstephan-geico-external · Pull Request #2629 · jetify-com/devbox
@mikeland73 thanks for your feedback. I did incorporate a number of your comments. However, others relate to your intro comment:
--envrc-dirshould not control/override--config. It should be independent.The issue I was trying to address is that the current version incorrectly (in my opinion) uses
--configto specify the location of the.envrcfile when specified as part ofdevbox generate direnv. The change also allows a config file location to be specified that will be used withdirenvand the.envrcfile. Thedevbox generate direnvcommand does not use the devbox config file, but needs to know where it can be found so that it can properly configure the.envrcfile (by including the config file withdevbox generate direnv --print-envrcAND by including the config file with the output of that same command).
TLDR:
I don't think we're that far apart. I mostly just want to simplify the logic in internal/boxcli/generate.go and just pass the envrcDir down the stack. I also don't want configDir to mean "relative path from envrc to config dir" since this is only relevant to .envrc file, but not the devbox itself. You should only compute it when generating the .envrc itself.
Longer:
I'm not sure I agree 100% that --config works incorrectly. Specifically, --config is a semi-global flag that runs a command using the context of a specific devbox.json. This means that the working directory becomes the directory where the devbox.json resides. For example:
devbox run --config examples/development/php/latest ls
Will print out the content of the directory in examples/development/php/latest.
I do think devbox generate direnv is a bit different from most devbox commands, but it's not clear to me that creating .envrc in the working directory is strictly a better option (If I did, I would be OK with just changing the behavior of the existing flag).
That said, I like your solution of adding a new flag. I think it solves the edge case without making --config behave differently for a single command. With the new flag, I would expect --config to continue behaving the same way.
Specifically:
devbox generate direnv --config path/to/my/config --envrc-dir path/to/create/envrc
Would create a new .envrc file in path/to/create/envrc and point to the devbox project in path/to/my/config
You would need to figure out the relative path between them, but this is trivial (your code already does this).
What I don't totally understand is why when both flags are passed, you change the meaning of the config flag to be relative (from the envrc). This logic is needed when printing the .envrc, but outside of that context it is incorrect. In fact, you correct for this issue by rejoining paths:
box, err := devbox.Open(&devopt.Opts{
Dir: filepath.Join(envrcDir, configDir),
But at this point, configDir is a misleading variable name, since it's a relative path from the envrc.