diff --git a/cmd/up.go b/cmd/up.go index 0972a4986..ed25b5e3b 100644 --- a/cmd/up.go +++ b/cmd/up.go @@ -86,8 +86,17 @@ func (cmd *UpCmd) execute(cobraCmd *cobra.Command, args []string) error { if err != nil { return fmt.Errorf("prepare workspace client: %w", err) } - if cmd.ExtraDevContainerPath != "" && client.Provider() != "docker" { - return fmt.Errorf("extra devcontainer file is only supported with local provider") + if hasLocalExtraFeatures(cmd.ExtraDevContainerConfig) { + providerConfig, err := provider2.LoadProviderConfig( + devPodConfig.DefaultContext, + client.Provider(), + ) + if err != nil { + return fmt.Errorf("load provider config for local extra features: %w", err) + } + if err := validateExtraFeatureProvider(cmd.CLIOptions, providerConfig); err != nil { + return err + } } telemetry.CollectorCLI.SetClient(client) @@ -157,7 +166,9 @@ func (cmd *UpCmd) registerDevContainerFlags(upCmd *cobra.Command) { "(e.g., folder name in .devcontainer/FOLDER/devcontainer.json)") upCmd.Flags(). StringVar(&cmd.ExtraDevContainerPath, "extra-devcontainer-path", "", - "The path to an additional devcontainer.json file to override original devcontainer.json") + "The local path to an additional devcontainer.json for runtime settings and build features. "+ + "Feature options override the project's; --additional-features takes precedence. "+ + "Feature changes require rebuilding an existing container") upCmd.Flags(). StringVar(&cmd.FallbackImage, "fallback-image", "", "The fallback image to use if no devcontainer configuration has been detected") @@ -747,6 +758,69 @@ func mergeEnvFromFiles(baseOptions *provider2.CLIOptions) error { return nil } +// loadExtraDevContainerConfig reads the local file before forwarding options to a provider. +func loadExtraDevContainerConfig(options *provider2.CLIOptions) error { + if options.ExtraDevContainerConfig != nil || options.ExtraDevContainerPath == "" { + return nil + } + extraConfig, err := config2.ParseDevContainerJSONFile(options.ExtraDevContainerPath) + if err != nil { + return fmt.Errorf("parse --extra-devcontainer-path: %w", err) + } + features := make(map[string]any, len(extraConfig.Features)) + for featureID, featureOptions := range extraConfig.Features { + if strings.HasPrefix(featureID, "./") || strings.HasPrefix(featureID, "../") { + featureID, err = filepath.Abs( + filepath.Join(filepath.Dir(extraConfig.Origin), featureID), + ) + if err != nil { + return fmt.Errorf("resolve feature from --extra-devcontainer-path: %w", err) + } + } + features[featureID] = featureOptions + } + extraConfig.Features = features + options.ExtraDevContainerConfig = extraConfig + return nil +} + +func hasLocalExtraFeatures(extraConfig *config2.DevContainerConfig) bool { + if extraConfig == nil { + return false + } + for featureID := range extraConfig.Features { + if isLocalExtraFeature(featureID) { + return true + } + } + return false +} + +func isLocalExtraFeature(featureID string) bool { + return strings.HasPrefix(featureID, "./") || strings.HasPrefix(featureID, "../") || + filepath.IsAbs(featureID) +} + +func validateExtraFeatureProvider( + options provider2.CLIOptions, + providerConfig *provider2.ProviderConfig, +) error { + if options.ExtraDevContainerConfig == nil || providerConfig.Agent.Local == config.BoolTrue { + return nil + } + for featureID := range options.ExtraDevContainerConfig.Features { + if isLocalExtraFeature(featureID) { + return fmt.Errorf( + "local feature %q from --extra-devcontainer-path is unsupported by remote provider %q; "+ + "use an OCI or HTTP feature reference", + featureID, + providerConfig.Name, + ) + } + } + return nil +} + var inheritedEnvironmentVariables = []string{ "GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", @@ -765,6 +839,9 @@ func (cmd *UpCmd) prepareClient( if err := mergeDevPodUpOptions(&cmd.CLIOptions); err != nil { return nil, nil, err } + if err := loadExtraDevContainerConfig(&cmd.CLIOptions); err != nil { + return nil, nil, err + } var logger log.Logger = log.Default if cmd.Platform.Enabled { diff --git a/cmd/up_test.go b/cmd/up_test.go index b4765e610..43869cbb4 100644 --- a/cmd/up_test.go +++ b/cmd/up_test.go @@ -1,9 +1,17 @@ package cmd import ( + "encoding/json" + "os" + "path/filepath" "testing" "github.com/skevetter/devpod/cmd/flags" + "github.com/skevetter/devpod/pkg/client/clientimplementation" + "github.com/skevetter/devpod/pkg/config" + devcontainerconfig "github.com/skevetter/devpod/pkg/devcontainer/config" + "github.com/skevetter/devpod/pkg/provider" + "github.com/skevetter/devpod/pkg/types" "github.com/stretchr/testify/require" ) @@ -23,3 +31,125 @@ func TestUpMountFlagIsRepeatableAndPreservesValues(t *testing.T) { `type=bind,source=/tmp/data,target=/data,readonly`, }, mounts) } + +func TestExtraDevContainerConfigSurvivesForwardingWithoutLocalFile(t *testing.T) { + extraPath := filepath.Join(t.TempDir(), "extra.json") + require.NoError(t, os.WriteFile(extraPath, []byte(`{ + // Personal config stays on the client. + "features": {"ghcr.io/devcontainers/features/git:1": {"version": "latest"}}, + "forwardPorts": [3774], + }`), 0o600)) + options := provider.CLIOptions{ExtraDevContainerPath: extraPath} + require.NoError(t, loadExtraDevContainerConfig(&options)) + require.NoError(t, os.Remove(extraPath)) + + // Machine agents receive JSON; proxy providers forward the same options through the environment. + data, err := json.Marshal(options) + require.NoError(t, err) + var remote provider.CLIOptions + require.NoError(t, json.Unmarshal(data, &remote)) + for name, value := range clientimplementation.EncodeOptions(remote, config.EnvFlagsUp) { + t.Setenv(name, value) + } + remote = provider.CLIOptions{} + require.NoError(t, mergeDevPodUpOptions(&remote)) + require.NoError(t, loadExtraDevContainerConfig(&remote)) + require.NotNil(t, remote.ExtraDevContainerConfig) + require.Equal( + t, + options.ExtraDevContainerConfig.Features, + remote.ExtraDevContainerConfig.Features, + ) + require.Equal(t, types.StrIntArray{"3774"}, remote.ExtraDevContainerConfig.ForwardPorts) +} + +func TestLoadExtraDevContainerConfig(t *testing.T) { + options := provider.CLIOptions{} + require.NoError(t, loadExtraDevContainerConfig(&options)) + require.Nil(t, options.ExtraDevContainerConfig) + + extraPath := filepath.Join(t.TempDir(), "extra.json") + options.ExtraDevContainerPath = extraPath + for _, content := range []string{"", `{invalid`, `{"features": []}`} { + if content != "" { + require.NoError(t, os.WriteFile(extraPath, []byte(content), 0o600)) + } + err := loadExtraDevContainerConfig(&options) + require.ErrorContains(t, err, "--extra-devcontainer-path") + } +} + +func TestLoadExtraDevContainerConfigResolvesLocalFeatureBeforeForwarding(t *testing.T) { + extraDir := t.TempDir() + extraPath := filepath.Join(extraDir, "extra.json") + require.NoError(t, os.WriteFile(extraPath, []byte(`{"features":{"../feature":{}}}`), 0o600)) + options := provider.CLIOptions{ExtraDevContainerPath: extraPath} + require.NoError(t, loadExtraDevContainerConfig(&options)) + + resolvedFeaturePath, err := filepath.Abs(filepath.Join(extraDir, "../feature")) + require.NoError(t, err) + _, ok := options.ExtraDevContainerConfig.Features[resolvedFeaturePath] + require.True(t, ok) + + data, err := json.Marshal(options) + require.NoError(t, err) + var forwarded provider.CLIOptions + require.NoError(t, json.Unmarshal(data, &forwarded)) + _, ok = forwarded.ExtraDevContainerConfig.Features[resolvedFeaturePath] + require.True(t, ok) +} + +func TestValidateExtraFeatureProvider(t *testing.T) { + t.Run("remote provider rejects local feature references", func(t *testing.T) { + options := provider.CLIOptions{ + ExtraDevContainerConfig: &devcontainerconfig.DevContainerConfig{ + DevContainerConfigBase: devcontainerconfig.DevContainerConfigBase{ + Features: map[string]any{ + "../local-feature": map[string]any{}, + }, + }, + }, + } + remoteProvider := &provider.ProviderConfig{Name: "ssh"} + require.ErrorContains( + t, + validateExtraFeatureProvider(options, remoteProvider), + "local feature", + ) + require.ErrorContains( + t, + validateExtraFeatureProvider(options, remoteProvider), + "OCI or HTTP", + ) + }) + + t.Run("remote provider keeps OCI and HTTP features", func(t *testing.T) { + options := provider.CLIOptions{ + ExtraDevContainerConfig: &devcontainerconfig.DevContainerConfig{ + DevContainerConfigBase: devcontainerconfig.DevContainerConfigBase{ + Features: map[string]any{ + "ghcr.io/devcontainers/features/git:1": map[string]any{}, + "https://example.com/feature.tgz": map[string]any{}, + }, + }, + }, + } + remoteProvider := &provider.ProviderConfig{Name: "ssh"} + require.NoError(t, validateExtraFeatureProvider(options, remoteProvider)) + }) + + t.Run("renamed local provider allows local features", func(t *testing.T) { + options := provider.CLIOptions{ + ExtraDevContainerConfig: &devcontainerconfig.DevContainerConfig{ + DevContainerConfigBase: devcontainerconfig.DevContainerConfigBase{ + Features: map[string]any{"./feature": map[string]any{}}, + }, + }, + } + localProvider := &provider.ProviderConfig{ + Name: "personal-tools", + Agent: provider.ProviderAgentConfig{Local: types.StrBool(config.BoolTrue)}, + } + require.NoError(t, validateExtraFeatureProvider(options, localProvider)) + }) +} diff --git a/docs/pages/developing-in-workspaces/devcontainer-json.mdx b/docs/pages/developing-in-workspaces/devcontainer-json.mdx index 517b632d4..8354e9b76 100644 --- a/docs/pages/developing-in-workspaces/devcontainer-json.mdx +++ b/docs/pages/developing-in-workspaces/devcontainer-json.mdx @@ -100,12 +100,35 @@ field of the `devcontainer.json` file as follows: } ``` +### Personal features and runtime settings + +`--extra-devcontainer-path` lets you keep personal features and runtime settings outside the project repository. DevPod reads the file on your machine and passes its configuration to the selected provider, including remote targets: + +```json +{ + "features": { + "ghcr.io/devcontainers/features/github-cli:1": { + "version": "latest" + } + }, + "forwardPorts": [3774] +} +``` + +```bash +devpod up . --extra-devcontainer-path ../personal-devcontainer/extra.json +``` + +Features from the extra file participate in image, Dockerfile, and Docker Compose builds. For the same feature ID, the extra file replaces the project's feature options, and `--additional-features` takes precedence over both. The entire option value is replaced, rather than merging individual options. Other feature IDs are preserved. Feature option values support the same variable substitution as the project's configuration, including `${localEnv:VARIABLE}`. + +Feature changes follow the same create/rebuild lifecycle as the project's devcontainer configuration. Rebuild an existing container to apply them. A normal `up` on a running container does not install new features. + ## devcontainer.json Development Flow When working on the `devcontainer.json` itself, it's important to understand when DevPod will apply new configuration. A naive approach would be to delete and recreate a workspace after each `devcontainer.json` change (which obviously works), but DevPod allows you to make changes to the configuration on the fly and reapply them via `devpod up my-workspace --recreate`. -This will apply **ALL** new configurations including Dockerfile changes as well as new mounts, new features or any other configuration that is not included in the above command. DevPod will only replace the existing running container if the command has succeeded, so if there is a mistake in the new configuration, the existing workspace should not be impacted. +This reapplies configuration and replaces the running container if the operation succeeds. Docker Compose can restore persisted feature build files and reuse the previous feature image during recreation, so `--recreate` does not always force a fresh feature build or apply changed features. The Desktop app equivalent for this is the 'Rebuild' button. :::warning Recreating / Rebuilding diff --git a/e2e/tests/up-docker-compose/build.go b/e2e/tests/up-docker-compose/build.go index 57cdeb791..dbf5e1506 100644 --- a/e2e/tests/up-docker-compose/build.go +++ b/e2e/tests/up-docker-compose/build.go @@ -8,6 +8,7 @@ import ( "io" "os" "path/filepath" + "strings" "time" "github.com/onsi/ginkgo/v2" @@ -126,6 +127,51 @@ var _ = ginkgo.Describe( gomega.Expect(ids2[0]).To(gomega.Equal(ids[0]), "Should use original container") }) + ginkgo.It( + "should build features from an extra devcontainer file with a renamed provider", + func(ctx context.Context) { + framework.ExpectNoError(f.DevPodProviderRename(ctx, "docker", "personal-tools")) + ginkgo.DeferCleanup(func(cleanupCtx context.Context) { + framework.ExpectNoError( + f.DevPodProviderRename(cleanupCtx, "personal-tools", "docker"), + ) + }) + tempDir, err := setupWorkspace( + "tests/up-docker-compose/testdata/docker-compose-rebuild-success", + initialDir, + f, + ) + framework.ExpectNoError(err) + extraDir := ginkgo.GinkgoT().TempDir() + featureDir := filepath.Join(extraDir, "feature") + framework.ExpectNoError(os.Mkdir(featureDir, 0o750)) + framework.ExpectNoError( + os.WriteFile(filepath.Join(featureDir, "devcontainer-feature.json"), []byte(`{ + "id": "marker", "version": "1.0.0", "name": "Marker", + "options": {"value": {"type": "string", "default": "default"}} + }`), 0o600), + ) + framework.ExpectNoError( + os.WriteFile(filepath.Join(featureDir, "install.sh"), []byte( + "#!/bin/sh\nset -eu\nprintf '%s' \"$VALUE\" > /usr/local/share/extra-feature-marker\n", + ), 0o600), + ) + extraPath := filepath.Join(extraDir, "extra.json") + framework.ExpectNoError(os.WriteFile(extraPath, []byte( + `{"features": {"./feature": {"value": "from-extra"}}, "remoteEnv": {"EXTRA_RUNTIME": "kept"}}`, + ), 0o600)) + framework.ExpectNoError( + f.DevPodUp(ctx, tempDir, "--extra-devcontainer-path", extraPath), + ) + output, err := f.DevPodSSH(ctx, tempDir, + `test "$EXTRA_RUNTIME" = kept && cat /usr/local/share/extra-feature-marker`, + ) + framework.ExpectNoError(err) + gomega.Expect(strings.TrimSpace(output)).To(gomega.Equal("from-extra")) + }, + ginkgo.SpecTimeout(framework.GetTimeout()), + ) + ginkgo.It("should delete container upon successful rebuild", func(ctx context.Context) { tempDir, err := setupWorkspace( "tests/up-docker-compose/testdata/docker-compose-rebuild-success", diff --git a/pkg/devcontainer/build.go b/pkg/devcontainer/build.go index 2a94bd404..3d7ebb58d 100644 --- a/pkg/devcontainer/build.go +++ b/pkg/devcontainer/build.go @@ -43,14 +43,10 @@ func (r *runner) build( } // Add extra devcontainer config if provided - if options.ExtraDevContainerPath != "" { + if extraConfig := options.ExtraDevContainerConfig; extraConfig != nil { if buildInfo.ImageMetadata == nil { buildInfo.ImageMetadata = &config.ImageMetadataConfig{} } - extraConfig, err := config.ParseDevContainerJSONFile(options.ExtraDevContainerPath) - if err != nil { - return nil, err - } config.AddConfigToImageMetadata(extraConfig, buildInfo.ImageMetadata) } diff --git a/pkg/devcontainer/compose.go b/pkg/devcontainer/compose.go index 327f1bcde..c86c81674 100644 --- a/pkg/devcontainer/compose.go +++ b/pkg/devcontainer/compose.go @@ -272,14 +272,10 @@ func (r *runner) runDockerCompose( } } - if options.ExtraDevContainerPath != "" { + if extraConfig := options.ExtraDevContainerConfig; extraConfig != nil { if imageMetadataConfig == nil { imageMetadataConfig = &config.ImageMetadataConfig{} } - extraConfig, err := config.ParseDevContainerJSONFile(options.ExtraDevContainerPath) - if err != nil { - return nil, err - } config.AddConfigToImageMetadata(extraConfig, imageMetadataConfig) } @@ -465,14 +461,10 @@ func (r *runner) startContainer( return nil, fmt.Errorf("inspect image: %w", err) } - if options.ExtraDevContainerPath != "" { + if extraConfig := options.ExtraDevContainerConfig; extraConfig != nil { if extendResult.imageMetadata == nil { extendResult.imageMetadata = &config.ImageMetadataConfig{} } - extraConfig, err := config.ParseDevContainerJSONFile(options.ExtraDevContainerPath) - if err != nil { - return nil, err - } config.AddConfigToImageMetadata(extraConfig, extendResult.imageMetadata) } diff --git a/pkg/devcontainer/config.go b/pkg/devcontainer/config.go index b9e9388b6..fa9366ba1 100644 --- a/pkg/devcontainer/config.go +++ b/pkg/devcontainer/config.go @@ -196,6 +196,14 @@ func (r *runner) substitute( parsedConfig.ImageContainer = config.ImageContainer{Image: options.DevContainerImage} } + if err := mergeExtraFeatures( + parsedConfig, + substitutionContext, + options.ExtraDevContainerConfig, + ); err != nil { + return nil, nil, err + } + // merge additional features from CLI flag if options.AdditionalFeatures != "" { additionalFeatures := make(map[string]any) @@ -223,6 +231,53 @@ func (r *runner) substitute( }, substitutionContext, nil } +// mergeExtraFeatures applies extra-file features before the CLI feature overrides. +func mergeExtraFeatures( + parsedConfig *config.DevContainerConfig, + substitutionContext *config.SubstitutionContext, + extraConfig *config.DevContainerConfig, +) error { + if extraConfig == nil || len(extraConfig.Features) == 0 { + return nil + } + extraFeatures := map[string]any{} + for featureID, featureOptions := range extraConfig.Features { + resolvedID, err := resolveExtraFeatureID(featureID, extraConfig.Origin) + if err != nil { + return err + } + extraFeatures[resolvedID] = featureOptions + } + substitutedFeatures := map[string]any{} + if err := config.Substitute( + substitutionContext, + extraFeatures, + &substitutedFeatures, + ); err != nil { + return fmt.Errorf("substitute --extra-devcontainer-path features: %w", err) + } + if parsedConfig.Features == nil { + parsedConfig.Features = make(map[string]any) + } + maps.Copy(parsedConfig.Features, substitutedFeatures) + return nil +} + +func resolveExtraFeatureID(featureID, extraConfigOrigin string) (string, error) { + if !strings.HasPrefix(featureID, "./") && !strings.HasPrefix(featureID, "../") { + return featureID, nil + } + featurePath, err := filepath.Abs(filepath.Join(filepath.Dir(extraConfigOrigin), featureID)) + if err != nil { + return "", fmt.Errorf( + "resolve feature %s from --extra-devcontainer-path: %w", + featureID, + err, + ) + } + return featurePath, nil +} + const ( jsonObjectStart = "{" jsonArrayStart = "[" diff --git a/pkg/devcontainer/config_test.go b/pkg/devcontainer/config_test.go index 8aa0240b8..668951214 100644 --- a/pkg/devcontainer/config_test.go +++ b/pkg/devcontainer/config_test.go @@ -2,9 +2,12 @@ package devcontainer import ( "os" + "path/filepath" + "strings" "testing" "github.com/skevetter/devpod/pkg/devcontainer/config" + "github.com/skevetter/devpod/pkg/devcontainer/feature" provider2 "github.com/skevetter/devpod/pkg/provider" "github.com/skevetter/log" "github.com/stretchr/testify/suite" @@ -20,6 +23,57 @@ type SubstituteTestSuite struct { runner *runner } +func (s *SubstituteTestSuite) TestSubstitute_ExtraFeaturesResolveFromExtraFileDirectory() { + root := s.T().TempDir() + projectDir := filepath.Join(root, "project") + extraDir := filepath.Join(root, "personal", "settings") + localFeatureDirs := map[string]string{ + "./feature": filepath.Join(extraDir, "feature"), + "../feature": filepath.Join(root, "personal", "feature"), + } + for _, dir := range localFeatureDirs { + s.Require().NoError(os.MkdirAll(dir, 0o750)) + s.Require().NoError(os.WriteFile( + filepath.Join(dir, "devcontainer-feature.json"), + []byte(`{"id":"local"}`), + 0o600, + )) + } + s.Require().NoError(os.MkdirAll(projectDir, 0o750)) + extraPath := filepath.Join(extraDir, "extra.json") + s.Require().NoError(os.MkdirAll(extraDir, 0o750)) + s.Require().NoError(os.WriteFile( + extraPath, + []byte(`{"features":{"./feature":{},"../feature":{}}}`), + 0o600, + )) + extraConfig, err := config.ParseDevContainerJSONFile(extraPath) + s.Require().NoError(err) + + result, _, err := s.runner.substitute(provider2.CLIOptions{ + ExtraDevContainerConfig: extraConfig, + }, &config.DevContainerConfig{Origin: filepath.Join(projectDir, "devcontainer.json")}) + s.Require().NoError(err) + for featureID, expectedDir := range localFeatureDirs { + relativeToProject, err := filepath.Rel(projectDir, expectedDir) + s.Require().NoError(err) + s.True( + strings.HasPrefix(relativeToProject, ".."), + "feature fixture must remain outside the project", + ) + resolvedID, err := filepath.Abs(expectedDir) + s.Require().NoError(err) + options, ok := result.Config.Features[resolvedID] + s.True(ok, "extra feature %s should resolve from extra file directory", featureID) + featurePath, err := feature.ProcessFeatureID(resolvedID, result.Config, log.Discard, false) + s.Require().NoError(err) + s.Equal(expectedDir, featurePath) + _, err = config.ParseDevContainerFeature(featurePath) + s.Require().NoError(err) + s.Equal(map[string]any{}, options) + } +} + func TestSubstituteTestSuite(t *testing.T) { suite.Run(t, new(SubstituteTestSuite)) } @@ -253,6 +307,79 @@ func (s *SubstituteTestSuite) TestSubstitute_AdditionalFeaturesEmpty() { s.Nil(result.Config.Features) } +func (s *SubstituteTestSuite) TestSubstitute_ExtraFeatures() { + const node = "ghcr.io/devcontainers/features/node:1" + extraConfig := &config.DevContainerConfig{} + extraConfig.Features = map[string]any{ + node: map[string]any{ + "version": "${localEnv:NODE_VERSION}", + }, + "ghcr.io/devcontainers/features/git:1": map[string]any{}, + } + + for _, tc := range []struct { + name string + base map[string]any + additional string + version string + }{ + {name: "without base features", version: "20"}, + { + name: "extra replaces base options", + base: map[string]any{ + node: map[string]any{"version": "18", "installYarnUsingApt": false}, + }, + version: "20", + }, + { + name: "CLI replaces extra options", + base: map[string]any{node: map[string]any{"version": "18"}}, + additional: `{"ghcr.io/devcontainers/features/node:1": {"version": "22"}}`, + version: "22", + }, + } { + s.Run(tc.name, func() { + if tc.base != nil { + tc.base["base-only"] = map[string]any{"enabled": false} + } + rawConfig := &config.DevContainerConfig{ + Origin: "/workspace/.devcontainer/devcontainer.json", + DevContainerConfigBase: config.DevContainerConfigBase{Features: tc.base}, + } + original := config.CloneDevContainerConfig(rawConfig) + result, _, err := s.runner.substitute(provider2.CLIOptions{ + ExtraDevContainerConfig: extraConfig, + AdditionalFeatures: tc.additional, + InitEnv: []string{"NODE_VERSION=20"}, + }, rawConfig) + + s.Require().NoError(err) + expected := map[string]any{ + node: map[string]any{"version": tc.version}, + "ghcr.io/devcontainers/features/git:1": map[string]any{}, + } + if tc.base != nil { + expected["base-only"] = map[string]any{"enabled": false} + } + s.Equal(expected, result.Config.Features) + s.Equal(original, rawConfig) + s.Equal(original.Origin, result.Config.Origin) + }) + } +} + +func (s *SubstituteTestSuite) TestSubstitute_ExtraFileWithoutFeatures() { + for _, features := range []map[string]any{nil, {}} { + result, _, err := s.runner.substitute(provider2.CLIOptions{ + ExtraDevContainerConfig: &config.DevContainerConfig{ + DevContainerConfigBase: config.DevContainerConfigBase{Features: features}, + }, + }, &config.DevContainerConfig{}) + s.Require().NoError(err) + s.Nil(result.Config.Features) + } +} + func (s *SubstituteTestSuite) TestResolveCLIMounts_SubstitutesVariables() { substitutionContext := &config.SubstitutionContext{ DevContainerID: "test-id", diff --git a/pkg/devcontainer/feature/features.go b/pkg/devcontainer/feature/features.go index 9866a17d6..48b220305 100644 --- a/pkg/devcontainer/feature/features.go +++ b/pkg/devcontainer/feature/features.go @@ -111,8 +111,11 @@ func ProcessFeatureID( log, forceBuild, ) - } else if strings.HasPrefix(id, "./") || strings.HasPrefix(id, "../") { + } else if strings.HasPrefix(id, "./") || strings.HasPrefix(id, "../") || filepath.IsAbs(id) { log.Debugf("process feature: type=%s, id=%s", "local", id) + if filepath.IsAbs(id) { + return id, nil + } return filepath.Abs( path.Join(filepath.ToSlash(filepath.Dir(devContainerConfig.Origin)), id), ) diff --git a/pkg/devcontainer/single.go b/pkg/devcontainer/single.go index 51f2f6199..c124b359f 100644 --- a/pkg/devcontainer/single.go +++ b/pkg/devcontainer/single.go @@ -168,14 +168,10 @@ func (r *runner) mergeExistingContainerConfig( return nil, err } - if p.options.ExtraDevContainerPath != "" { + if extraConfig := p.options.ExtraDevContainerConfig; extraConfig != nil { if imageMetadataConfig == nil { imageMetadataConfig = &config.ImageMetadataConfig{} } - extraConfig, err := config.ParseDevContainerJSONFile(p.options.ExtraDevContainerPath) - if err != nil { - return nil, err - } config.AddConfigToImageMetadata(extraConfig, imageMetadataConfig) } @@ -218,10 +214,10 @@ func (r *runner) resolveNewContainer( ) (*resolvedContainer, error) { buildInfo, err := r.build(ctx, p.parsedConfig, p.substitutionContext, provider2.BuildOptions{ CLIOptions: provider2.CLIOptions{ - PrebuildRepositories: p.options.PrebuildRepositories, - ForceDockerless: p.options.ForceDockerless, - Platform: p.options.Platform, - ExtraDevContainerPath: p.options.ExtraDevContainerPath, + PrebuildRepositories: p.options.PrebuildRepositories, + ForceDockerless: p.options.ForceDockerless, + Platform: p.options.Platform, + ExtraDevContainerConfig: p.options.ExtraDevContainerConfig, }, NoBuild: p.options.NoBuild, RegistryCache: p.options.RegistryCache, diff --git a/pkg/provider/workspace.go b/pkg/provider/workspace.go index 8a6c057e5..59db60b93 100644 --- a/pkg/provider/workspace.go +++ b/pkg/provider/workspace.go @@ -235,6 +235,9 @@ type CLIOptions struct { UidMap []string `json:"uidMap,omitempty"` GidMap []string `json:"gidMap,omitempty"` + // ExtraDevContainerConfig carries the parsed local file to remote providers. + ExtraDevContainerConfig *devcontainerconfig.DevContainerConfig `json:"extraDevContainerConfig,omitempty"` + // build options // Repository specifies the container registry repository to push the built image to (e.g., ghcr.io/user/image). // When set, the image will be tagged and pushed to this repository after building.