Skip to content

internal/devconfig: move project directory search into devbox.Find #2172

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

Merged
merged 1 commit into from
Jul 16, 2024

Conversation

gcurtis
Copy link
Collaborator

@gcurtis gcurtis commented Jun 25, 2024

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:

 $ 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?

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.

- Keeping `Find` a distinct function from `Open` makes it clearer when we're
  recursively searching for a config vs. not.
- `Find` can just try reading a potential config file path instead of performing
  a separate `os.Stat`. This halves the number of system calls at startup and
  eliminates any potential races between the call to `os.Stat` and `os.Open`.

```go
// Open loads a Devbox config from a file or project directory.
// For use with `devbox -c some/dir <subcmd>`.
func Open(path string) (*Config, error)

// Find is like [Open] except it recursively searches up the directory tree.
// For use with `devbox <subcmd>` (without a `-c` flag).
func Find(path string) (*Config, error)
```

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?
```
@gcurtis gcurtis marked this pull request as ready for review June 26, 2024 18:29
@gcurtis gcurtis requested review from savil and mikeland73 July 8, 2024 13:17
@@ -38,6 +38,8 @@ linters-settings:
- name: bare-return
- name: bool-literal-in-expr
- name: cognitive-complexity
exclude:
- "**_test.go"
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

// Open does not recursively search outside of path. See [Find] to load a config
// by walking up the directory tree.
func Open(path string) (*Config, error) {
start := time.Now()
Copy link
Contributor

Choose a reason for hiding this comment

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

If you want to profile performance, you can use defer debug.FunctionTimer().End()

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can log that to slog as well?

@gcurtis gcurtis merged commit 7855f96 into main Jul 16, 2024
24 checks passed
@gcurtis gcurtis deleted the gcurtis/find-config branch July 16, 2024 19:30
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