Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] plugin env PATH fix #2418

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

Conversation

mohsenari
Copy link
Collaborator

@mohsenari mohsenari commented Nov 21, 2024

Summary

When a plugin modifies PATH and also config in devbox.json modifies PATH via env:{}, the env overwrites the plugin and so PATH modification from the plugin is lost. This is because we do maps.Copy() to merge the env from plugin to the one from config.
The copy is fine for all other env variables but for PATH we need to handle merging the two rather than overwriting.
Fixes #2138

How was it tested?

bug recreate:

  • devbox init
  • devbox add ruby (ruby plugin modifies PATH)
  • devbox shell (see PATH is prepended with ruby plugin)
  • modify devbox.json and add "env": {"PATH": "/my/config/bin:$PATH"}
  • re-enter devbox shell and see PATH prepend from ruby plugin is lost, only "/my/config/bin" is prepended.
    fix recreate:
  • follow same steps, but in last step both PATH prepends from devbox.json and from plugin are present.

Comment on lines +360 to +361
rootEnv := mergePATHsFromTwoEnvs(env, c.Root.Env)
maps.Copy(env, rootEnv)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use OSExpandEnvMap instead?

Basically take the environment provided by the plugins and use it to expand the environment of the config.

Copy link
Collaborator Author

@mohsenari mohsenari Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikeland73 after your suggestion, I tried OSExpandEnvMap and it it doesn't work.

The issue with OSExpandEnvMap is it merges the two PATHs correctly but it applies the (non-nix) system PATH to the :$PATH at the end. So with OSExpandEnvMap PATH becomes: /my/config:/my/plugin:/opt/homebrew/sbin:/usr/local/bin:...

With mergePATHsFromTwoEnvs PATH becomes: /my/config:/my/plugin:/Users/mohsenansari/code/go.jetpack.io/devbox/examples/stacks/rails/.devbox/nix/profile/default/bin:...:/opt/homebrew/sbin:/usr/local/bin:...

So OSExpandEnvMap can't work here because I don't want to expand $PATH env yet, I just want to merge them correctly without losing :$PATH suffix or $PATH: prefix to it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Essentially, OSExpandEnvMap expands PATH with non-devboxified PATH value, but I don't want that.

Comment on lines +354 to +355
pluginEnv := mergePATHsFromTwoEnvs(env, i.Env())
maps.Copy(env, pluginEnv)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use OSExpandEnvMap instead of mergePATHsFromTwoEnvs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Cannot set PATH env var in both a plugin and devbox.json
2 participants