Skip to content

Commit 7855f96

Browse files
authored
internal/devconfig: move project directory search into devbox.Find (#2172)
Some cleanup when I looked into supporting different config extensions (e.g., `devbox.jsonc` to make editors okay with comments). Instead of first searching for a project directory and then calling `devbox.Open` on that path, add a `devbox.Find` method that performs the search and open as a single step. - Add separate `Find` and `Open` functions to make it clearer when we're recursively searching for a config vs. not. - Try just reading files instead of performing a separate `os.Stat`. - Fix a bug where specifying any filename other than `devbox.json` with the `-C` flag would fail with a bad error message. Changes to user error messages: ```diff $ devbox add go -Error: No devbox.json found in this directory, or any parent directories. Did you run `devbox init` yet? +Error: no devbox.json found in the current directory (or any parent directories). Did you run `devbox init` yet? $ devbox -c badpath add go -Error: stat /var/folders/79/1yc1ywp10w9f2xnr_rpp_ff00000gn/T/tmp.bU25JVWovO/badpath: no such file or directory + +Error: the devbox config path "badpath" does not exist. $ mkdir child $ devbox -c child add go -Error: No devbox.json found in child. Did you run `devbox init` yet? +Error: no devbox.json found in "child". Did you run `devbox init` yet? ```
1 parent 56ba0c1 commit 7855f96

File tree

8 files changed

+423
-272
lines changed

8 files changed

+423
-272
lines changed

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ linters-settings:
3838
- name: bare-return
3939
- name: bool-literal-in-expr
4040
- name: cognitive-complexity
41+
exclude:
42+
- "**_test.go"
4143
arguments:
4244
- 32 # TODO: gradually reduce it
4345
- name: datarace

internal/devbox/devbox.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,24 @@ func InitConfig(dir string) (bool, error) {
7676
}
7777

7878
func Open(opts *devopt.Opts) (*Devbox, error) {
79-
projectDir, err := findProjectDir(opts.Dir)
80-
if err != nil {
81-
return nil, err
79+
var cfg *devconfig.Config
80+
var err error
81+
if opts.Dir == "" {
82+
cfg, err = devconfig.Find(".")
83+
if errors.Is(err, devconfig.ErrNotFound) {
84+
return nil, usererr.New("no devbox.json found in the current directory (or any parent directories). Did you run `devbox init` yet?")
85+
}
86+
} else {
87+
cfg, err = devconfig.Open(opts.Dir)
88+
if errors.Is(err, os.ErrNotExist) {
89+
return nil, usererr.New("the devbox config path %q does not exist.", opts.Dir)
90+
}
91+
if errors.Is(err, devconfig.ErrNotFound) {
92+
return nil, usererr.New("no devbox.json found in %q. Did you run `devbox init` yet?", opts.Dir)
93+
}
8294
}
83-
84-
cfg, err := devconfig.Open(projectDir)
8595
if err != nil {
86-
return nil, errors.WithStack(err)
96+
return nil, usererr.WithUserMessage(err, "Error loading devbox.json.")
8797
}
8898

8999
environment, err := validateEnvironment(opts.Environment)
@@ -96,7 +106,7 @@ func Open(opts *devopt.Opts) (*Devbox, error) {
96106
env: opts.Env,
97107
environment: environment,
98108
nix: &nix.Nix{},
99-
projectDir: projectDir,
109+
projectDir: filepath.Dir(cfg.Root.AbsRootPath),
100110
pluginManager: plugin.NewManager(),
101111
stderr: opts.Stderr,
102112
customProcessComposeFile: opts.CustomProcessComposeFile,

internal/devbox/dir.go

Lines changed: 0 additions & 113 deletions
This file was deleted.

internal/devbox/dir_test.go

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)