Skip to content

Conversation

mohsenari
Copy link
Collaborator

Summary

Fixes this bug:
https://github.com/jetify-com/devbox/pull/2174/files#r1659165116

How was it tested?

  • use compiled devbox
  • put export FOO = 'BBBAR' in .env file
  • put the following in a devbox.json file
  {
    "packages": [],
    "shell": {
      "init_hook": [
        "echo 'Welcome to devbox!' > /dev/null"
      ],
      "scripts": {
        "test": [
          "echo $FOO"
        ]
      }
    },
    "env_from": ".env"
  }
  • create a subdirectory besides devbox.json called test
  • cd test && devbox run test

}

file, err := os.Open(c.EnvFrom)
envFileAbsPath := path.Join(filepath.Dir(c.AbsRootPath), c.EnvFrom)
Copy link
Contributor

Choose a reason for hiding this comment

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

This breaks with absolute paths. I think we need to treat absolute and relative paths differently.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

pushed a commit to handle this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

What you want is:

envfileAbsPath := c.EnvFrom
if !filepath.IsAbs(c.EnvFrom) {
  envfileAbsPath = filepath.Join(c.AbsRootPath, c.EnvFrom)
}

@mohsenari mohsenari requested a review from mikeland73 October 15, 2024 19:53
Comment on lines 30 to 31
if filepath.IsAbs(c.EnvFrom) {
envFileAbsPath = path.Join(envFileAbsPath, path.Base(c.EnvFrom))
Copy link
Collaborator

Choose a reason for hiding this comment

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

should this scenario be envFileAbsPath = c.EnvFrom?

if the user has set

// devbox.json
{
   "env_from": "/my/absolute/path/foo.env`
}

Then, I'd think we want to just obey the absolute path directive.

Perhaps we should error if the env_from path is absolute, because that would mess up any reproducibility of the devbox.json config. But I'll be open to just letting it fly. We can expect our users to use their judgement for when they want to make their config a bit less reproducible. For example, for the sake of some quick changes they may want to experiment with.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree with the absolute path erroring out case. Reproducibility should be considered here. I'll push a commit to change this case to an error.

Copy link
Contributor

Choose a reason for hiding this comment

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

We allow absolute paths for flakes and plugins. I don't think env should be different. Absolute paths can be important for reproducibility as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking about it even more, I don't think relative paths are any more reproducible than absolute paths! relative paths are more reproducible only if .env file is in the repo, but in that case you are better off just adding to the env field in devbox.json

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@mikeland73 the idea is to prevent unintended mistakes that comes with putting absolute paths. For example, if user A puts /Users/A/projects/Aproject/.env and user B has the repo clone in /Users/B/... it will break the reproducibility.
Do you think this is an acceptable risk and we should allow absolute path here?

Copy link
Contributor

Choose a reason for hiding this comment

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

2 reasons:

  • erroring on abs path is not backward compatible. This reason alone is a blocker. path of least resistance is always to preserve backward compatibility, especially if it's easier!
  • relative poths also allow you to make it non reproducible! ../path/to/my/.env would also live outside the repo. Or just .env when .env is in gititnore.

Why would folks use this feature with .env file that is checked in to their repo? (I guess some convenience maybe, but seems low value). The use case for this feature is for people to have different .env files, so by definition reproducibility is not as important.

@mohsenari mohsenari requested a review from savil October 15, 2024 22:22
Comment on lines 29 to 31
envFileAbsPath := filepath.Dir(c.AbsRootPath)
if filepath.IsAbs(c.EnvFrom) {
envFileAbsPath = path.Join(envFileAbsPath, path.Base(c.EnvFrom))
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand this logic at all. Why would the path of the config matter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was handling the case when env_from was absolute. Your suggestion below works too, it's just handling if abs() vs if !abs()

Copy link
Contributor

Choose a reason for hiding this comment

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

If env_from is absolute you don't need to change it at all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah I agree, I overcomplicated the logic there.

}

file, err := os.Open(c.EnvFrom)
envFileAbsPath := path.Join(filepath.Dir(c.AbsRootPath), c.EnvFrom)
Copy link
Contributor

Choose a reason for hiding this comment

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

What you want is:

envfileAbsPath := c.EnvFrom
if !filepath.IsAbs(c.EnvFrom) {
  envfileAbsPath = filepath.Join(c.AbsRootPath, c.EnvFrom)
}

Copy link
Contributor

@mikeland73 mikeland73 left a comment

Choose a reason for hiding this comment

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

NACK

breaking absolute paths would be non-backward compatible

@mohsenari mohsenari requested a review from mikeland73 October 16, 2024 19:25
@mohsenari mohsenari merged commit 02a719f into main Oct 16, 2024
29 checks passed
@mohsenari mohsenari deleted the mohsen--fix-env-from-relativepath branch October 16, 2024 19:38
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.

3 participants