Skip to content

Commit 3167fa1

Browse files
authored
[Plugins] Do not overwrite existing user-config files of plugins (#1917)
## Summary Fixes #1909 Makes two changes: 1. The users control `devbox.d` directory files once the plugin has generated it for them. Devbox should never be overwriting them. Added code to check for this. 2. For the code that checks if filepath contains `.devbox` or `devbox.d`, I now try to make that stricter by checking if filepath contains `/.devbox/` or `/devbox.d/`. As pointed out in the issue, the jetpack-mongodb plugin is generating user-configs at `devbox.d/jetpack-io.devbox-plugins.mongodb` which also contains `.devbox` string. This will pass checks for `strings.Contains(filepath, ".devbox")` even though that is not the intention of that particular check. cc @mikeland73 ## How was it tested? Repro'd the scenario of the issue. Added the files via `git init && git commit -a` Ran `rm -rf .devbox` and `devbox install` Ran `git status` and confirmed that no `devbox.d` files were modified
1 parent eed56cd commit 3167fa1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

internal/devbox/packages.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (d *Devbox) ensureStateIsUpToDate(ctx context.Context, mode installMode) er
257257
if upToDate {
258258
return nil
259259
}
260-
ux.Finfo(d.stderr, "Ensuring packages are installed.")
260+
ux.Finfo(d.stderr, "Ensuring packages are installed.\n")
261261
}
262262

263263
if mode == install || mode == update || mode == ensure {

internal/plugin/plugin.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"bytes"
88
"cmp"
99
"encoding/json"
10+
"fmt"
1011
"io/fs"
1112
"os"
1213
"path/filepath"
@@ -16,6 +17,7 @@ import (
1617
"github.com/pkg/errors"
1718
"go.jetpack.io/devbox/internal/devconfig/configfile"
1819
"go.jetpack.io/devbox/internal/devpkg"
20+
"go.jetpack.io/devbox/internal/fileutil"
1921

2022
"go.jetpack.io/devbox/internal/debug"
2123
"go.jetpack.io/devbox/internal/lock"
@@ -24,6 +26,7 @@ import (
2426
)
2527

2628
const (
29+
// TODO rename to devboxPluginUserConfigDirName
2730
devboxDirName = "devbox.d"
2831
devboxHiddenDirName = ".devbox"
2932
pluginConfigName = "plugin.json"
@@ -215,14 +218,21 @@ func (m *Manager) shouldCreateFile(
215218
pkg *lock.Package,
216219
filePath string,
217220
) bool {
218-
// Only create files in devboxDir if they are not in the lockfile
221+
// Only create files in devbox.d directory if they are not in the lockfile
219222
pluginInstalled := pkg != nil && pkg.PluginVersion != ""
220-
if strings.Contains(filePath, devboxDirName) && pluginInstalled {
223+
if strings.Contains(filePath, "/"+devboxDirName+"/") && pluginInstalled {
224+
return false
225+
}
226+
227+
// We do not overwrite an existing file in devbox.d. That is user-config that is user-controlled
228+
// after the initial file creation via the Devbox plugin.
229+
if strings.Contains(filePath, "/"+devboxDirName+"/") && fileutil.Exists(filePath) {
230+
fmt.Printf("false for shouldCreateFile b/c it exists: %s\n", filePath)
221231
return false
222232
}
223233

224234
// Hidden .devbox files are always replaceable, so ok to recreate
225-
if strings.Contains(filePath, devboxHiddenDirName) {
235+
if strings.Contains(filePath, "/"+devboxHiddenDirName+"/") {
226236
return true
227237
}
228238
_, err := os.Stat(filePath)

0 commit comments

Comments
 (0)