From 450c4a59093a7c70504a9a66c7414e70def197ee Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 26 Sep 2026 22:48:46 -0600 Subject: [PATCH 1/3] feat(devcontainer): warn on legacy selection drift --- e2e/tests/up-docker-compose/config.go | 79 +++++++ .../.devcontainer/legacy/devcontainer.json | 7 + pkg/devcontainer/config.go | 36 ++- pkg/devcontainer/config/discover.go | 6 + pkg/devcontainer/config/discover_test.go | 77 +++++++ pkg/devcontainer/config_selection.go | 77 +++++++ pkg/devcontainer/config_test.go | 209 +++++++++++++++++- pkg/devcontainer/run.go | 6 + .../devcontainer-json.mdx | 6 + 9 files changed, 494 insertions(+), 9 deletions(-) create mode 100644 e2e/tests/up-docker-compose/testdata/devcontainer-selection-drift/.devcontainer/legacy/devcontainer.json create mode 100644 pkg/devcontainer/config/discover_test.go create mode 100644 pkg/devcontainer/config_selection.go diff --git a/e2e/tests/up-docker-compose/config.go b/e2e/tests/up-docker-compose/config.go index f29342a1b5..6fb6027168 100644 --- a/e2e/tests/up-docker-compose/config.go +++ b/e2e/tests/up-docker-compose/config.go @@ -180,6 +180,85 @@ var _ = ginkgo.Describe( framework.ExpectNoError(err) }, ginkgo.SpecTimeout(framework.TimeoutLong())) + ginkgo.It( + "legacy devcontainer selection drift warns and supports explicit adoption", + func(ctx context.Context) { + tempDir, err := setupWorkspace( + "tests/up-docker-compose/testdata/devcontainer-selection-drift", + tc.initialDir, + tc.f, + ) + framework.ExpectNoError(err) + + err = tc.f.DevsyUp(ctx, tempDir) + framework.ExpectNoError(err) + + workspace, err := tc.f.FindWorkspace(ctx, tempDir) + framework.ExpectNoError(err) + err = tc.f.DevsyWorkspaceStop(ctx, tempDir) + framework.ExpectNoError(err) + + rootConfig := filepath.Join(tempDir, ".devcontainer", "devcontainer.json") + err = os.WriteFile(rootConfig, []byte(`{ + "name": "root", + "image": "ghcr.io/devsy-org/test-images/go:1", + "containerEnv": {"SELECTION_MARKER": "root"} +}`), 0o600) + framework.ExpectNoError(err) + + stdout, _, err := tc.f.ExecCommandCapture(ctx, []string{ + cmdWorkspace, "up", flagDebug, flagIDE, ideNone, tempDir, + }) + framework.ExpectNoError(err) + gomega.Expect(stdout).To( + gomega.ContainSubstring( + `"warnings":["This existing workspace is continuing to use \".devcontainer/legacy/devcontainer.json\"`, + ), + ) + gomega.Expect(stdout).To( + gomega.ContainSubstring( + `Current project discovery would select \".devcontainer/devcontainer.json\"`, + ), + ) + + assertSelectionMarker := func(want string) { + err := tc.f.ExecCommand( + ctx, + true, + true, + "["+want+"]", + []string{ + cmdWorkspace, cmdSSH, flagCommand, + "echo \"[$SELECTION_MARKER]\"", workspace.ID, + }, + ) + framework.ExpectNoError(err) + } + assertSelectionMarker("legacy") + + err = tc.f.DevsyWorkspaceStop(ctx, tempDir) + framework.ExpectNoError(err) + err = tc.f.DevsyUpRecreate( + ctx, + names.Flag(names.DevContainer), + ".devcontainer/devcontainer.json", + tempDir, + ) + framework.ExpectNoError(err) + assertSelectionMarker("root") + + err = tc.f.DevsyWorkspaceStop(ctx, tempDir) + framework.ExpectNoError(err) + err = tc.f.DevsyUp(ctx, tempDir) + framework.ExpectNoError(err) + assertSelectionMarker("root") + + err = tc.f.DevsyWorkspaceDelete(ctx, tempDir) + framework.ExpectNoError(err) + }, + ginkgo.SpecTimeout(framework.TimeoutLong()), + ) + ginkgo.It("environment variables", func(ctx context.Context) { _, workspace, err := tc.setupAndStartWorkspace( ctx, diff --git a/e2e/tests/up-docker-compose/testdata/devcontainer-selection-drift/.devcontainer/legacy/devcontainer.json b/e2e/tests/up-docker-compose/testdata/devcontainer-selection-drift/.devcontainer/legacy/devcontainer.json new file mode 100644 index 0000000000..92a1b6eecd --- /dev/null +++ b/e2e/tests/up-docker-compose/testdata/devcontainer-selection-drift/.devcontainer/legacy/devcontainer.json @@ -0,0 +1,7 @@ +{ + "name": "legacy", + "image": "ghcr.io/devsy-org/test-images/go:1", + "containerEnv": { + "SELECTION_MARKER": "legacy" + } +} diff --git a/pkg/devcontainer/config.go b/pkg/devcontainer/config.go index 4b1c8d074f..d396a448c0 100644 --- a/pkg/devcontainer/config.go +++ b/pkg/devcontainer/config.go @@ -65,8 +65,18 @@ type devContainerSelection struct { source string path string id string + origin devContainerSelectionOrigin } +type devContainerSelectionOrigin uint8 + +const ( + selectionNone devContainerSelectionOrigin = iota + selectionCLI + selectionPersistedWorkspace + selectionLastResolvedCompatibility +) + // effectiveDevContainerSelection returns the one effective selector for this // operation. Current CLI input wins over persisted workspace state, followed // by the last resolved path for compatibility with older workspaces. @@ -113,7 +123,10 @@ func (r *runner) lastConfigPathSelection() (devContainerSelection, bool) { if !devContainerConfigExists(r.workspaceFolder(), relativePath) { return devContainerSelection{}, false } - return devContainerSelection{path: relativePath}, true + return devContainerSelection{ + path: relativePath, + origin: selectionLastResolvedCompatibility, + }, true } func devContainerConfigExists(workspaceFolder, relativePath string) bool { @@ -129,14 +142,23 @@ func (r *runner) persistedDevContainerSelection() (devContainerSelection, bool) workspace := r.workspaceConfig.Workspace switch { case workspace.DevContainerSource != "": - return devContainerSelection{source: workspace.DevContainerSource}, true + return devContainerSelection{ + source: workspace.DevContainerSource, + origin: selectionPersistedWorkspace, + }, true case workspace.DevContainerConfig != nil: // Preserve pre-persistence precedence for embedded configs. return devContainerSelection{}, false case workspace.DevContainerPath != "": - return devContainerSelection{path: workspace.DevContainerPath}, true + return devContainerSelection{ + path: workspace.DevContainerPath, + origin: selectionPersistedWorkspace, + }, true case workspace.DevContainerID != "": - return devContainerSelection{id: workspace.DevContainerID}, true + return devContainerSelection{ + id: workspace.DevContainerID, + origin: selectionPersistedWorkspace, + }, true default: return devContainerSelection{}, false } @@ -182,11 +204,11 @@ func (r *runner) workspaceFolder() string { func newDevContainerSelection(source, path, id string) (devContainerSelection, bool) { switch { case source != "": - return devContainerSelection{source: source}, true + return devContainerSelection{source: source, origin: selectionCLI}, true case path != "": - return devContainerSelection{path: path}, true + return devContainerSelection{path: path, origin: selectionCLI}, true case id != "": - return devContainerSelection{id: id}, true + return devContainerSelection{id: id, origin: selectionCLI}, true default: return devContainerSelection{}, false } diff --git a/pkg/devcontainer/config/discover.go b/pkg/devcontainer/config/discover.go index 558d6025a0..06fd8f29c6 100644 --- a/pkg/devcontainer/config/discover.go +++ b/pkg/devcontainer/config/discover.go @@ -18,6 +18,12 @@ const devcontainerDirName = ".devcontainer" // contains more than one configuration (or when selection is forced). type ConfigSelector func(candidates []string) (string, error) +// DiscoverDevContainerPath returns the config a new workspace would select. +// It returns an empty path when no config exists. +func DiscoverDevContainerPath(folder string) (string, error) { + return resolveDevContainerPath(folder, "", SelectSingle(folder), false) +} + // SelectByID returns a selector that picks the config whose parent directory // name matches id (the devcontainer id, e.g. ".devcontainer//devcontainer.json"). func SelectByID(id string) ConfigSelector { diff --git a/pkg/devcontainer/config/discover_test.go b/pkg/devcontainer/config/discover_test.go new file mode 100644 index 0000000000..a187f25c9d --- /dev/null +++ b/pkg/devcontainer/config/discover_test.go @@ -0,0 +1,77 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestDiscoverDevContainerPath(t *testing.T) { + tests := []struct { + name string + files []string + want string + wantErrText string + }{ + { + name: "devcontainer directory root takes precedence", + files: []string{".devcontainer/devcontainer.json", ".devcontainer.json", ".devcontainer/profile/devcontainer.json"}, + want: ".devcontainer/devcontainer.json", + }, + { + name: "root file takes precedence over nested config", + files: []string{".devcontainer.json", ".devcontainer/profile/devcontainer.json"}, + want: ".devcontainer.json", + }, + { + name: "single nested config", + files: []string{".devcontainer/profile/devcontainer.json"}, + want: ".devcontainer/profile/devcontainer.json", + }, + { + name: "ambiguous nested configs", + files: []string{".devcontainer/one/devcontainer.json", ".devcontainer/two/devcontainer.json"}, + wantErrText: "multiple devcontainer configurations found", + }, + { + name: "no config", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + folder := t.TempDir() + for _, relativePath := range tt.files { + file := filepath.Join(folder, filepath.FromSlash(relativePath)) + if err := os.MkdirAll(filepath.Dir(file), 0o750); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(file, []byte(`{"image":"test"}`), 0o600); err != nil { + t.Fatal(err) + } + } + + got, err := DiscoverDevContainerPath(folder) + if tt.wantErrText != "" { + if err == nil || !strings.Contains(err.Error(), tt.wantErrText) { + t.Fatalf("DiscoverDevContainerPath() error = %v, want containing %q", err, tt.wantErrText) + } + return + } + if err != nil { + t.Fatalf("DiscoverDevContainerPath(): %v", err) + } + if tt.want == "" { + if got != "" { + t.Fatalf("DiscoverDevContainerPath() = %q, want empty", got) + } + return + } + want := filepath.Join(folder, filepath.FromSlash(tt.want)) + if got != want { + t.Fatalf("DiscoverDevContainerPath() = %q, want %q", got, want) + } + }) + } +} diff --git a/pkg/devcontainer/config_selection.go b/pkg/devcontainer/config_selection.go new file mode 100644 index 0000000000..7497c0a8bb --- /dev/null +++ b/pkg/devcontainer/config_selection.go @@ -0,0 +1,77 @@ +package devcontainer + +import ( + "fmt" + "path/filepath" + + "github.com/devsy-org/devsy/pkg/devcontainer/config" + "github.com/devsy-org/devsy/pkg/log" +) + +type devContainerSelectionDrift struct { + pinnedPath string + discoveredPath string +} + +func (r *runner) compatibilitySelectionWarning( + selection devContainerSelection, +) string { + drift, err := r.compatibilitySelectionDrift(selection) + if err != nil { + log.Debugf("could not check devcontainer selection drift: %v", err) + return "" + } + if drift == nil { + return "" + } + + return fmt.Sprintf( + "This existing workspace is continuing to use %q, its previously resolved devcontainer configuration. Current project discovery would select %q. Devsy does not switch an existing workspace implicitly; run workspace up with --devcontainer %s to adopt it explicitly.", + drift.pinnedPath, + drift.discoveredPath, + drift.discoveredPath, + ) +} + +func (r *runner) compatibilitySelectionDrift( + selection devContainerSelection, +) (*devContainerSelectionDrift, error) { + if selection.origin != selectionLastResolvedCompatibility || selection.path == "" { + return nil, nil + } + + folder := r.workspaceFolder() + discoveredPath, err := config.DiscoverDevContainerPath(folder) + if err != nil || discoveredPath == "" { + return nil, err + } + + pinnedPath, err := workspaceRelativePath(folder, selection.path) + if err != nil { + return nil, err + } + discoveredRelativePath, err := workspaceRelativePath(folder, discoveredPath) + if err != nil { + return nil, err + } + if pinnedPath == discoveredRelativePath { + return nil, nil + } + + return &devContainerSelectionDrift{ + pinnedPath: pinnedPath, + discoveredPath: discoveredRelativePath, + }, nil +} + +func workspaceRelativePath(folder, value string) (string, error) { + path := filepath.FromSlash(value) + if !filepath.IsAbs(path) { + path = filepath.Join(folder, path) + } + relativePath, err := filepath.Rel(folder, path) + if err != nil { + return "", err + } + return filepath.ToSlash(filepath.Clean(relativePath)), nil +} diff --git a/pkg/devcontainer/config_test.go b/pkg/devcontainer/config_test.go index e450fb825d..035e5b8686 100644 --- a/pkg/devcontainer/config_test.go +++ b/pkg/devcontainer/config_test.go @@ -16,6 +16,8 @@ const ( testDevContainerProfile = "max" testEmbeddedImage = "embedded" testNestedSubPath = "app" + testLegacyConfigPath = ".devcontainer/legacy/devcontainer.json" + testRootConfigPath = ".devcontainer/devcontainer.json" ) type SubstituteTestSuite struct { @@ -666,6 +668,209 @@ func TestGetRawConfig_LastConfigPathCompatibilityFallback(t *testing.T) { } } +func TestEffectiveDevContainerSelection_Origins(t *testing.T) { + tests := []struct { + name string + setup func(*runner) + options provider2.CLIOptions + wantOrigin devContainerSelectionOrigin + }{ + { + name: "CLI", + options: provider2.CLIOptions{DevContainerPath: testRootConfigPath}, + wantOrigin: selectionCLI, + }, + { + name: "persisted workspace", + setup: func(r *runner) { + r.workspaceConfig.Workspace.DevContainerPath = testRootConfigPath + }, + wantOrigin: selectionPersistedWorkspace, + }, + { + name: "last resolved compatibility", + setup: func(r *runner) { + seedConfigAt(t, r.localWorkspaceFolder, testRootConfigPath) + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: testRootConfigPath, + } + }, + wantOrigin: selectionLastResolvedCompatibility, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + r := newRunnerAt(t.TempDir()) + if tt.setup != nil { + tt.setup(r) + } + selection := r.effectiveDevContainerSelection(tt.options) + if selection.origin != tt.wantOrigin { + t.Fatalf("selection origin = %v, want %v", selection.origin, tt.wantOrigin) + } + }) + } +} + +func TestCompatibilitySelectionWarning_NewRootConfigDetected(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, testLegacyConfigPath) + seedConfigAt(t, folder, testRootConfigPath) + r := newRunnerAt(folder) + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: testLegacyConfigPath, + } + + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + if selection.path != testLegacyConfigPath { + t.Fatalf("pinned selection path = %q, want %q", selection.path, testLegacyConfigPath) + } + warning := r.compatibilitySelectionWarning(selection) + for _, want := range []string{ + testLegacyConfigPath, + testRootConfigPath, + "--devcontainer " + testRootConfigPath, + } { + if !strings.Contains(warning, want) { + t.Errorf("warning %q does not contain %q", warning, want) + } + } + if strings.Contains(warning, folder) { + t.Errorf("warning contains absolute workspace path: %q", warning) + } +} + +func TestCompatibilitySelectionWarning_SamePathNoWarning(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, testRootConfigPath) + r := newRunnerAt(folder) + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: testRootConfigPath, + } + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + if warning := r.compatibilitySelectionWarning(selection); warning != "" { + t.Fatalf("warning = %q, want empty", warning) + } +} + +func TestCompatibilitySelectionWarning_ExplicitSelectionsNoWarning(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, testLegacyConfigPath) + seedConfigAt(t, folder, testRootConfigPath) + r := newRunnerAt(folder) + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: testLegacyConfigPath, + } + for _, selection := range []devContainerSelection{ + {path: testRootConfigPath, origin: selectionCLI}, + {path: testLegacyConfigPath, origin: selectionPersistedWorkspace}, + {}, + } { + if warning := r.compatibilitySelectionWarning(selection); warning != "" { + t.Errorf("warning for origin %v = %q, want empty", selection.origin, warning) + } + } +} + +func TestCompatibilitySelectionWarning_AmbiguousDiscoveryNoWarning(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, testLegacyConfigPath) + seedConfigAt(t, folder, ".devcontainer/other/devcontainer.json") + r := newRunnerAt(folder) + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: testLegacyConfigPath, + } + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + if selection.path != testLegacyConfigPath { + t.Fatalf("pinned selection path = %q, want %q", selection.path, testLegacyConfigPath) + } + if warning := r.compatibilitySelectionWarning(selection); warning != "" { + t.Fatalf("warning = %q, want empty", warning) + } +} + +func TestCompatibilitySelectionWarning_GitSubpathUsesWorkspaceRelativePaths(t *testing.T) { + folder := t.TempDir() + subPath := filepath.Join("app", "app") + pinnedPath := filepath.ToSlash(filepath.Join(subPath, testLegacyConfigPath)) + rootPath := filepath.ToSlash(filepath.Join(subPath, testRootConfigPath)) + seedConfigAt(t, folder, pinnedPath) + seedConfigAt(t, folder, rootPath) + r := newRunnerAt(folder) + r.workspaceConfig.Workspace.Source.GitSubPath = subPath + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: pinnedPath, + } + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + warning := r.compatibilitySelectionWarning(selection) + if !strings.Contains(warning, testLegacyConfigPath) || + !strings.Contains(warning, testRootConfigPath) || strings.Contains(warning, folder) { + t.Fatalf("unexpected workspace-relative warning: %q", warning) + } +} + +func TestCompatibilitySelectionWarning_MissingPathFallsBackToDiscovery(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, testRootConfigPath) + r := newRunnerAt(folder) + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: ".devcontainer/removed/devcontainer.json", + } + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + if selection.origin != selectionNone { + t.Fatalf("selection origin = %v, want none", selection.origin) + } + if warning := r.compatibilitySelectionWarning(selection); warning != "" { + t.Fatalf("warning = %q, want empty", warning) + } + resolved, err := r.getRawConfig(provider2.CLIOptions{}) + if err != nil { + t.Fatalf("getRawConfig(): %v", err) + } + if resolved.Image != "seed" { + t.Fatalf("resolved image = %q, want discovered root config", resolved.Image) + } +} + +func TestCompatibilitySelectionDrift_EmbeddedConfigNoWarning(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, testLegacyConfigPath) + seedConfigAt(t, folder, testRootConfigPath) + r := newRunnerAt(folder) + r.workspaceConfig.Workspace.DevContainerConfig = &config.DevContainerConfig{} + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: testLegacyConfigPath, + } + + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + if selection.origin != selectionNone { + t.Fatalf("selection origin = %v, want none", selection.origin) + } + if warning := r.compatibilitySelectionWarning(selection); warning != "" { + t.Fatalf("warning = %q, want empty", warning) + } +} + +func TestCompatibilitySelectionDrift_PersistedExplicitSelectionNoWarning(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, testLegacyConfigPath) + seedConfigAt(t, folder, testRootConfigPath) + r := newRunnerAt(folder) + r.workspaceConfig.Workspace.DevContainerID = "legacy" + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: testLegacyConfigPath, + } + + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + if selection.origin != selectionPersistedWorkspace { + t.Fatalf("selection origin = %v, want persisted workspace", selection.origin) + } + if warning := r.compatibilitySelectionWarning(selection); warning != "" { + t.Fatalf("warning = %q, want empty", warning) + } +} + func TestEffectiveDevContainerSelection_LastPathStripsGitSubPath(t *testing.T) { folder := t.TempDir() seedConfigAt(t, folder, "devsy/jupyter-notebook-hello-world/.devcontainer/devcontainer.json") @@ -676,7 +881,7 @@ func TestEffectiveDevContainerSelection_LastPathStripsGitSubPath(t *testing.T) { } selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) - if selection.path != ".devcontainer/devcontainer.json" { + if selection.path != testRootConfigPath { t.Fatalf("selection path = %q, want .devcontainer/devcontainer.json", selection.path) } } @@ -711,7 +916,7 @@ func TestEffectiveDevContainerSelection_LastPathLeadingSlashSubPath(t *testing.T } selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) - if selection.path != ".devcontainer/devcontainer.json" { + if selection.path != testRootConfigPath { t.Fatalf("selection path = %q, want .devcontainer/devcontainer.json", selection.path) } } diff --git a/pkg/devcontainer/run.go b/pkg/devcontainer/run.go index 5ce15cafbf..b97b297c16 100644 --- a/pkg/devcontainer/run.go +++ b/pkg/devcontainer/run.go @@ -139,6 +139,9 @@ func (r *runner) Up( reporter = status.Nop() } r.reporter = reporter + selectionWarning := r.compatibilitySelectionWarning( + r.effectiveDevContainerSelection(options.CLIOptions), + ) log.Debugf( "Up devcontainer for workspace %q with timeout %s", @@ -216,6 +219,9 @@ func (r *runner) Up( ) if result != nil { result.RecoveryContainer = r.recovering + if selectionWarning != "" { + result.HostWarnings = append(result.HostWarnings, selectionWarning) + } } return result, err } diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx index 355050a71c..558754c367 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx @@ -41,6 +41,12 @@ You can specify a specific file to use via the `--devcontainer-path` CLI flag: devsy workspace up github.com/my-org/my-repo --devcontainer-path ./my-git-path-to/a-devcontainer-json-file.json ``` +### Existing workspace selection + +Devsy remembers the effective devcontainer configuration for an existing workspace so that restart and other lifecycle operations keep the same environment. If fresh project discovery later prefers a different config, a legacy workspace warns about the selection drift and continues using its previously resolved config. Devsy does not switch the workspace implicitly. + +To adopt another config intentionally, run `devsy workspace up --devcontainer ` or select a named profile with `--devcontainer id:`. The choice is persisted for subsequent lifecycle operations. + A `devcontainer.json` is not able to import or inherit any settings from other `devcontainer.json` files, so make sure all dependent files and folders are available within the configuration subdirectory. ### Using a Dockerfile From 0892a7c9da61966c0df47c2ca32833460c7764f6 Mon Sep 17 00:00:00 2001 From: Samuel K Date: Sat, 26 Sep 2026 23:03:07 -0600 Subject: [PATCH 2/3] fix(devcontainer): satisfy lint and formatting checks --- pkg/devcontainer/config/discover_test.go | 132 ++++++++++++----------- pkg/devcontainer/config_selection.go | 4 +- 2 files changed, 72 insertions(+), 64 deletions(-) diff --git a/pkg/devcontainer/config/discover_test.go b/pkg/devcontainer/config/discover_test.go index a187f25c9d..d77b15792d 100644 --- a/pkg/devcontainer/config/discover_test.go +++ b/pkg/devcontainer/config/discover_test.go @@ -7,71 +7,77 @@ import ( "testing" ) -func TestDiscoverDevContainerPath(t *testing.T) { - tests := []struct { - name string - files []string - want string - wantErrText string - }{ - { - name: "devcontainer directory root takes precedence", - files: []string{".devcontainer/devcontainer.json", ".devcontainer.json", ".devcontainer/profile/devcontainer.json"}, - want: ".devcontainer/devcontainer.json", - }, - { - name: "root file takes precedence over nested config", - files: []string{".devcontainer.json", ".devcontainer/profile/devcontainer.json"}, - want: ".devcontainer.json", - }, - { - name: "single nested config", - files: []string{".devcontainer/profile/devcontainer.json"}, - want: ".devcontainer/profile/devcontainer.json", - }, - { - name: "ambiguous nested configs", - files: []string{".devcontainer/one/devcontainer.json", ".devcontainer/two/devcontainer.json"}, - wantErrText: "multiple devcontainer configurations found", - }, - { - name: "no config", - }, +const rootConfigFilePath = ".devcontainer.json" + +func TestDiscoverDevContainerPathDirectoryRootPrecedence(t *testing.T) { + folder := t.TempDir() + seedDiscoveryConfigs( + t, + folder, + ".devcontainer/devcontainer.json", + rootConfigFilePath, + ".devcontainer/profile/devcontainer.json", + ) + assertDiscoveredPath(t, folder, ".devcontainer/devcontainer.json") +} + +func TestDiscoverDevContainerPathRootFilePrecedence(t *testing.T) { + folder := t.TempDir() + seedDiscoveryConfigs(t, folder, rootConfigFilePath, ".devcontainer/profile/devcontainer.json") + assertDiscoveredPath(t, folder, rootConfigFilePath) +} + +func TestDiscoverDevContainerPathSingleNested(t *testing.T) { + folder := t.TempDir() + seedDiscoveryConfigs(t, folder, ".devcontainer/profile/devcontainer.json") + assertDiscoveredPath(t, folder, ".devcontainer/profile/devcontainer.json") +} + +func TestDiscoverDevContainerPathAmbiguousNested(t *testing.T) { + folder := t.TempDir() + seedDiscoveryConfigs( + t, + folder, + ".devcontainer/one/devcontainer.json", + ".devcontainer/two/devcontainer.json", + ) + path, err := DiscoverDevContainerPath(folder) + if err == nil || !strings.Contains(err.Error(), "multiple devcontainer configurations found") { + t.Fatalf("DiscoverDevContainerPath() = %q, %v, want ambiguity error", path, err) } +} - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - folder := t.TempDir() - for _, relativePath := range tt.files { - file := filepath.Join(folder, filepath.FromSlash(relativePath)) - if err := os.MkdirAll(filepath.Dir(file), 0o750); err != nil { - t.Fatal(err) - } - if err := os.WriteFile(file, []byte(`{"image":"test"}`), 0o600); err != nil { - t.Fatal(err) - } - } +func TestDiscoverDevContainerPathNoConfig(t *testing.T) { + assertDiscoveredPath(t, t.TempDir(), "") +} - got, err := DiscoverDevContainerPath(folder) - if tt.wantErrText != "" { - if err == nil || !strings.Contains(err.Error(), tt.wantErrText) { - t.Fatalf("DiscoverDevContainerPath() error = %v, want containing %q", err, tt.wantErrText) - } - return - } - if err != nil { - t.Fatalf("DiscoverDevContainerPath(): %v", err) - } - if tt.want == "" { - if got != "" { - t.Fatalf("DiscoverDevContainerPath() = %q, want empty", got) - } - return - } - want := filepath.Join(folder, filepath.FromSlash(tt.want)) - if got != want { - t.Fatalf("DiscoverDevContainerPath() = %q, want %q", got, want) - } - }) +func seedDiscoveryConfigs(t *testing.T, folder string, paths ...string) { + t.Helper() + for _, relativePath := range paths { + file := filepath.Join(folder, filepath.FromSlash(relativePath)) + if err := os.MkdirAll(filepath.Dir(file), 0o750); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(file, []byte(`{"image":"test"}`), 0o600); err != nil { + t.Fatal(err) + } + } +} + +func assertDiscoveredPath(t *testing.T, folder, want string) { + t.Helper() + got, err := DiscoverDevContainerPath(folder) + if err != nil { + t.Fatalf("DiscoverDevContainerPath(): %v", err) + } + if want == "" { + if got != "" { + t.Fatalf("DiscoverDevContainerPath() = %q, want empty", got) + } + return + } + wantPath := filepath.Join(folder, filepath.FromSlash(want)) + if got != wantPath { + t.Fatalf("DiscoverDevContainerPath() = %q, want %q", got, wantPath) } } diff --git a/pkg/devcontainer/config_selection.go b/pkg/devcontainer/config_selection.go index 7497c0a8bb..3f5a762855 100644 --- a/pkg/devcontainer/config_selection.go +++ b/pkg/devcontainer/config_selection.go @@ -26,7 +26,9 @@ func (r *runner) compatibilitySelectionWarning( } return fmt.Sprintf( - "This existing workspace is continuing to use %q, its previously resolved devcontainer configuration. Current project discovery would select %q. Devsy does not switch an existing workspace implicitly; run workspace up with --devcontainer %s to adopt it explicitly.", + "This existing workspace is continuing to use %q, its previously resolved devcontainer configuration. "+ + "Current project discovery would select %q. Devsy does not switch an existing workspace implicitly; "+ + "run workspace up with --devcontainer %s to adopt it explicitly.", drift.pinnedPath, drift.discoveredPath, drift.discoveredPath, From 3cbb75be90a2317ad613b3aea6aa7a1c0704cd5d Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 09:46:43 -0600 Subject: [PATCH 3/3] fix(devcontainer): make adoption guidance actionable --- pkg/devcontainer/config_selection.go | 2 +- pkg/devcontainer/config_test.go | 21 ++++++++++++++++++- .../devcontainer-json.mdx | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/devcontainer/config_selection.go b/pkg/devcontainer/config_selection.go index 3f5a762855..a519500c10 100644 --- a/pkg/devcontainer/config_selection.go +++ b/pkg/devcontainer/config_selection.go @@ -28,7 +28,7 @@ func (r *runner) compatibilitySelectionWarning( return fmt.Sprintf( "This existing workspace is continuing to use %q, its previously resolved devcontainer configuration. "+ "Current project discovery would select %q. Devsy does not switch an existing workspace implicitly; "+ - "run workspace up with --devcontainer %s to adopt it explicitly.", + "run workspace up --recreate --devcontainer %q to adopt it explicitly.", drift.pinnedPath, drift.discoveredPath, drift.discoveredPath, diff --git a/pkg/devcontainer/config_test.go b/pkg/devcontainer/config_test.go index 035e5b8686..3914ee4fbf 100644 --- a/pkg/devcontainer/config_test.go +++ b/pkg/devcontainer/config_test.go @@ -730,7 +730,7 @@ func TestCompatibilitySelectionWarning_NewRootConfigDetected(t *testing.T) { for _, want := range []string{ testLegacyConfigPath, testRootConfigPath, - "--devcontainer " + testRootConfigPath, + "--recreate --devcontainer \"" + testRootConfigPath + "\"", } { if !strings.Contains(warning, want) { t.Errorf("warning %q does not contain %q", warning, want) @@ -741,6 +741,25 @@ func TestCompatibilitySelectionWarning_NewRootConfigDetected(t *testing.T) { } } +func TestCompatibilitySelectionWarning_QuotesDiscoveredPathWithSpaces(t *testing.T) { + folder := t.TempDir() + seedConfigAt(t, folder, "custom/devcontainer.json") + seedConfigAt(t, folder, ".devcontainer/my profile/devcontainer.json") + r := newRunnerAt(folder) + r.workspaceConfig.LastDevContainerConfig = &config.DevContainerConfigWithPath{ + Path: "custom/devcontainer.json", + } + + selection := r.effectiveDevContainerSelection(provider2.CLIOptions{}) + warning := r.compatibilitySelectionWarning(selection) + if !strings.Contains( + warning, + `--recreate --devcontainer ".devcontainer/my profile/devcontainer.json"`, + ) { + t.Errorf("warning %q does not quote the suggested path", warning) + } +} + func TestCompatibilitySelectionWarning_SamePathNoWarning(t *testing.T) { folder := t.TempDir() seedConfigAt(t, folder, testRootConfigPath) diff --git a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx index 558754c367..61bbf7071f 100644 --- a/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx +++ b/sites/docs-devsy-sh/content/docs/developing-in-workspaces/devcontainer-json.mdx @@ -45,7 +45,7 @@ devsy workspace up github.com/my-org/my-repo --devcontainer-path ./my-git-path-t Devsy remembers the effective devcontainer configuration for an existing workspace so that restart and other lifecycle operations keep the same environment. If fresh project discovery later prefers a different config, a legacy workspace warns about the selection drift and continues using its previously resolved config. Devsy does not switch the workspace implicitly. -To adopt another config intentionally, run `devsy workspace up --devcontainer ` or select a named profile with `--devcontainer id:`. The choice is persisted for subsequent lifecycle operations. +To adopt another config intentionally, run `devsy workspace up --recreate --devcontainer ""` or select a named profile with `--recreate --devcontainer "id:"`. The choice is persisted for subsequent lifecycle operations. A `devcontainer.json` is not able to import or inherit any settings from other `devcontainer.json` files, so make sure all dependent files and folders are available within the configuration subdirectory.