Skip to content

Conversation

savil
Copy link
Collaborator

@savil savil commented Oct 4, 2022

Summary

Motivation
Previously, a user would need to specify the directory of the devbox.json when
invoking devbox shell or devbox build and similar commands. In the absence,
of an explicit directory argument, we would assume the user is saying that
the devbox.json file is in the current directory.

This PR changes this to look for the devbox.json file in the specified-directory, or current directory if none specified,
AND any parent directory.

This is desirable for a few reasons:

  1. Convenience: it is nice especially for mono-repos to be located in a sub-directory
    and be able to invoke devbox shell or devbox build, in a manner similar to
    how git works.
  2. Enable devbox add and devbox rm to run from any sub-directory. These commands
    currently require the user to be located in the "directory of the devbox.json".
    With this PR, this requirement is relaxed so that the commands can be run more naturally
    in any sub-directory as well.

Fixes #185

How was it tested?

  • devbox shell and devbox build inside a sub-directory
# this directory has a devbox.json
> cd testdata/rust/rust-stable
> mkdir fake-dir
> cd fake-dir

# verify shell is using the rustc from the nix store
> devbox shell
(devbox) > which rustc

> devbox build
> docker run devbox

> devbox add openssl
> git diff ../devbox.json
diff --git a/testdata/rust/rust-stable/devbox.json b/testdata/rust/rust-stable/devbox.json
index 288dade..66982f9 100644
--- a/testdata/rust/rust-stable/devbox.json
+++ b/testdata/rust/rust-stable/devbox.json
@@ -1,3 +1,9 @@
 {
-  "packages": ["rust-bin.stable.latest.default"]
-}
+  "packages": [
+    "rust-bin.stable.latest.default",
+    "openssl"
+  ],
+  "shell": {
+    "init_hook": null
+  }
+}
\ No newline at end of file

> devbox rm openssl
> git diff
# none! as expected (not quite, I'm lying, there's been some unrelated changes which still show up)
  • devbox shell and devbox build in a directory containing a devbox.json

  • did these commands in testdata/rust/rust-stable

  • explicitly specify the folder of the devbox.json

> cd testdata/rust
> devbox shell rust-stable
  • Error scenario
> cd testdata/rust/
> devbox shell
Error: No devbox.json found in this directory, or any parent directories. Did you run `devbox init` yet?

> devbox build
Error: No devbox.json found in this directory, or any parent directories. Did you run `devbox init` yet?

Copy link
Collaborator Author

savil commented Oct 4, 2022

@savil savil force-pushed the savil/subfolder-search-config branch from c74e5ad to 207074a Compare October 4, 2022 04:10
@savil savil requested review from LucilleH and gcurtis October 4, 2022 04:10
@savil savil force-pushed the savil/subfolder-search-config branch from 207074a to 16b811f Compare October 4, 2022 04:11
devbox.go Outdated
Comment on lines 247 to 253
if cur == "" || cur == "." {
var err error
cur, err = os.Getwd()
if err != nil {
return "", errors.WithStack(err)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you need to call filepath.Abs to handle all the relative path cases. For example, if the config is in ./repo and the user runs devbox .. from ./repo/a.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The filepath.Abs was being called inside pathArgs: https://github.com/jetpack-io/devbox/blob/7ae5af019d7f568bb50e164401fa6b293a22e7c5/boxcli/args.go#L18
which is then passed in here.

But I do like the suggestion to sanitize the dir here as well via calling filepath.Abs

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, it might be a good idea to sanitize it here too. Otherwise it's not super obvious that this method requires it.

Copy link
Collaborator

@LucilleH LucilleH left a comment

Choose a reason for hiding this comment

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

Handy 👍

@savil savil force-pushed the savil/subfolder-search-config branch 2 times, most recently from 626eeb1 to a4901dc Compare October 4, 2022 21:56
@savil savil force-pushed the savil/subfolder-search-config branch from a4901dc to 8b8e7ab Compare October 4, 2022 23:16
@savil savil closed this Oct 4, 2022
@savil savil reopened this Oct 4, 2022
@savil
Copy link
Collaborator Author

savil commented Oct 4, 2022

will re-submit in the hopes that helps with the auto-checks running properly.

@savil savil closed this Oct 4, 2022
@savil savil reopened this Oct 4, 2022
@savil savil mentioned this pull request Oct 5, 2022
@savil savil merged commit d1f41ea into main Oct 5, 2022
@savil savil deleted the savil/subfolder-search-config branch October 5, 2022 16:18
@savil savil added the feature New feature or request label Oct 5, 2022
savil added a commit that referenced this pull request Oct 6, 2022
…, and not combined with srcDir (#212)

## Summary

~This should fix a consequence of a change introduced in #200 to make
the devbox.srcDir be an absolute path.
In the BuildPlanners, we need the paths to be relative so they are
inside the "docker context" and
can be used in Dockerfiles.~

New Fix: 
I think the problem is actually scoped to just the NodeJSPlanner. Within
NodeJSPlanner, the inputFiles were being set using
`filepath.Join(srcDir, <filename>)` when instead it should be just
`<filename>`, or more pedantically `path/of/filename/from/devbox-json`.

This is because the docker-context is the directory-of-devbox-json, and
any files `COPY --link`'d should be specified with paths relative to
this docker-context.

I inspected `git grep InputFiles` and it seems NodeJSPlanner is the only
one with this issue.

I also went back in history to a commit from Monday, built the binary
and found that the following would fail due to this reason:
```
> cd testdata/nodejs
> devbox build nodejs-18
```
It works now with this fix.
## How was it tested?

in www.jetpack.io repo,
1. did `devbox build` and `docker run -p 3000:3000 --expose 3000 -ti
devbox` and could open the website in localhost:3000
2. did `cd root-of-www.jetpack.io` and did `devbox build www && docker
run -p 3000:3000 --expose 3000 -ti devbox`. Works!
3. did `mkdir -p root-of-www.jetpack.io/www/fake-dir && cd
root-of-www.jetpack.io/www/fake-dir` and then: `devbox build ../ &&
docker run -p 3000:3000 --expose 3000 -ti devbox`. Works! Also, works
with just `devbox build` i.e. omitting `../` argument (due to #200)

For NodeJS testdata in `testdata/nodejs`:
1. `cd testdata/nodejs` and `devbox build nodejs-18 && docker run
devbox`. Works!
2. `cd testdata/nodejs/nodejs-18` and `devbox build && docker run
devbox`. Works!
3. `cd testdata/nodejs/nodejs-18/fake-dir` and `devbox build && docker
run devbox`. Works!

in devbox repo,
did `devbox build` and `docker run devbox` with:
- `testdata/rust/rust-stable`
- `testdata/python/pip-example`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Development

Successfully merging this pull request may close these issues.

Feature request: Subfolder search of the devbox.json
3 participants