diff --git a/desktop/src/renderer/src/lib/components/provider/ProviderSheet.test.ts b/desktop/src/renderer/src/lib/components/provider/ProviderSheet.test.ts index a03259b8f..4dc1e732d 100644 --- a/desktop/src/renderer/src/lib/components/provider/ProviderSheet.test.ts +++ b/desktop/src/renderer/src/lib/components/provider/ProviderSheet.test.ts @@ -254,7 +254,7 @@ describe("ProviderSheet", () => { }) it("saves only changed visible options and preserves the machine mode", async () => { - const expression = "${PROVIDER_FOLDER}/config.json" + const expression = ["$", "{PROVIDER_FOLDER}/config.json"].join("") providerOptions.mockResolvedValue({ CONFIG: { name: "CONFIG", default: expression }, HOST: { name: "HOST", default: "localhost" }, diff --git a/desktop/src/renderer/src/lib/components/provider/ProviderWizard.test.ts b/desktop/src/renderer/src/lib/components/provider/ProviderWizard.test.ts index 37f77e5fa..a7f175eba 100644 --- a/desktop/src/renderer/src/lib/components/provider/ProviderWizard.test.ts +++ b/desktop/src/renderer/src/lib/components/provider/ProviderWizard.test.ts @@ -220,7 +220,7 @@ describe("ProviderWizard", () => { }) it("does not submit an untouched expression default", async () => { - const expression = "${PROVIDER_FOLDER}/config.json" + const expression = ["$", "{PROVIDER_FOLDER}/config.json"].join("") providerOptions.mockResolvedValue({ CONFIG: { name: "CONFIG", default: expression }, }) @@ -243,7 +243,7 @@ describe("ProviderWizard", () => { }) it("submits an explicitly edited expression default", async () => { - const expression = "${PROVIDER_FOLDER}/config.json" + const expression = ["$", "{PROVIDER_FOLDER}/config.json"].join("") providerOptions.mockResolvedValue({ CONFIG: { name: "CONFIG", default: expression }, }) diff --git a/pkg/compose/helper.go b/pkg/compose/helper.go index 8666f2f5a..88d2437f4 100644 --- a/pkg/compose/helper.go +++ b/pkg/compose/helper.go @@ -89,6 +89,37 @@ type ComposeHelper struct { invocationEnvironment map[string]string } +func NewComposeHelper(dockerHelper *docker.DockerHelper) (*ComposeHelper, error) { + dockerCmd := dockerHelper.DockerCommand + if dockerCmd == "" { + dockerCmd = "docker" + } + + type tryFunc func() (*ComposeHelper, error) + + var detectors []tryFunc + if dockerHelper.GetRuntime().Name() == docker.RuntimePodman { + detectors = []tryFunc{ + func() (*ComposeHelper, error) { return tryComposeSubcommand(dockerCmd) }, + func() (*ComposeHelper, error) { return tryDockerComposeV2(dockerCmd) }, + } + } else { + detectors = []tryFunc{ + func() (*ComposeHelper, error) { return tryDockerComposeV2(dockerCmd) }, + func() (*ComposeHelper, error) { return tryComposeSubcommand("podman") }, + } + } + + for _, detect := range detectors { + if helper, err := detect(); err == nil { + helper.Docker = dockerHelper + return helper, nil + } + } + + return nil, fmt.Errorf("docker compose or podman compose not installed") +} + // WithInvocationEnvironment leaves the shared provider helper unchanged. func (h *ComposeHelper) WithInvocationEnvironment(env map[string]string) *ComposeHelper { clone := *h @@ -121,42 +152,6 @@ func checkWSLgEnvFiles(candidates map[string]string) composecli.ProjectOptionsFn } } -// NewComposeHelper creates a new ComposeHelper instance after detecting whether -// Docker Compose V2 or Podman Compose is installed. The detection order depends -// on the container runtime: Podman runtimes try podman compose first, while -// Docker/nerdctl runtimes try docker compose V2 first. It returns an error if -// none are found. -func NewComposeHelper(dockerHelper *docker.DockerHelper) (*ComposeHelper, error) { - dockerCmd := dockerHelper.DockerCommand - if dockerCmd == "" { - dockerCmd = "docker" - } - - type tryFunc func() (*ComposeHelper, error) - - var detectors []tryFunc - if dockerHelper.GetRuntime().Name() == docker.RuntimePodman { - detectors = []tryFunc{ - func() (*ComposeHelper, error) { return tryComposeSubcommand(dockerCmd) }, - func() (*ComposeHelper, error) { return tryDockerComposeV2(dockerCmd) }, - } - } else { - detectors = []tryFunc{ - func() (*ComposeHelper, error) { return tryDockerComposeV2(dockerCmd) }, - func() (*ComposeHelper, error) { return tryComposeSubcommand("podman") }, - } - } - - for _, detect := range detectors { - if helper, err := detect(); err == nil { - helper.Docker = dockerHelper - return helper, nil - } - } - - return nil, fmt.Errorf("docker compose or podman compose not installed") -} - // tryDockerComposeV2 checks if Docker Compose V2 is available and returns a ComposeHelper if so. // The Compose V2 plugin can be installed on linux using // diff --git a/pkg/devcontainer/compose.go b/pkg/devcontainer/compose.go index 988f78163..fca65309e 100644 --- a/pkg/devcontainer/compose.go +++ b/pkg/devcontainer/compose.go @@ -363,16 +363,13 @@ func (r *runner) ensureComposeContainer( if err := r.checkWSLgReuse(containerDetails, options.Recreate); err != nil { return nil, err } - effectiveOptions, forceSecretRuntimeRefresh, err := r.composeSecretRuntimeOptions( + effectiveOptions, forceSecretRuntimeRefresh, err := r.effectiveComposeRuntimeOptions( containerDetails, options, ) if err != nil { return nil, err } - if forceSecretRuntimeRefresh { - log.Info("workspace secrets require secure runtime mounts") - } if existing, ok := r.reuseExistingCompose(ctx, params, containerDetails, effectiveOptions); ok { return existing, nil @@ -422,6 +419,23 @@ func (r *runner) reuseExistingCompose( }) } +func (r *runner) effectiveComposeRuntimeOptions( + containerDetails *config.ContainerDetails, + options UpOptions, +) (UpOptions, bool, error) { + effectiveOptions, forceSecretRuntimeRefresh, err := r.composeSecretRuntimeOptions( + containerDetails, + options, + ) + if err != nil { + return UpOptions{}, false, err + } + if forceSecretRuntimeRefresh { + log.Info("workspace secrets require secure runtime mounts") + } + return effectiveOptions, forceSecretRuntimeRefresh, nil +} + func (r *runner) composeSecretRuntimeOptions( details *config.ContainerDetails, options UpOptions, @@ -1112,20 +1126,11 @@ func (r *runner) buildComposeOverrideArgs( if override.path != "" { composeGlobalArgs = append(composeGlobalArgs, "-f", override.path) - invocation := composeInvocation{ - Version: 1, - Directory: start.project.WorkingDir, - Args: composeGlobalArgs, - } - if replay := r.wslgReplay(); replay != nil { - invocation.Version = 2 - invocation.WSLg = replay - } - if err := recordComposeInvocation(composeInvocationRecord{ - Path: override.path, - Service: start.parsedConfig.Config.Service, - Invocation: invocation, - }); err != nil { + if err := r.recordComposeOverrideInvocation( + start, + override.path, + composeGlobalArgs, + ); err != nil { return composeOverrideArgs{}, err } } @@ -1133,6 +1138,27 @@ func (r *runner) buildComposeOverrideArgs( return composeOverrideArgs{args: composeGlobalArgs, contract: override.contract}, nil } +func (r *runner) recordComposeOverrideInvocation( + start *startContainerParams, + overridePath string, + args []string, +) error { + invocation := composeInvocation{ + Version: 1, + Directory: start.project.WorkingDir, + Args: args, + } + if replay := r.wslgReplay(); replay != nil { + invocation.Version = 2 + invocation.WSLg = replay + } + return recordComposeInvocation(composeInvocationRecord{ + Path: overridePath, + Service: start.parsedConfig.Config.Service, + Invocation: invocation, + }) +} + func resolveComposeRuntimeImage( ctx context.Context, params *composeOverrideParams, @@ -1188,30 +1214,51 @@ func (r *runner) marshalComposeContract( }) } -func (r *runner) generateComposeUpOverride( +type composeOverrideMetadata struct { + mergedConfig *config.MergedDevContainerConfig + metadataValue []byte + hasOverlay bool +} + +func resolveComposeOverrideMetadata( ctx context.Context, - params *composeOverrideParams, + start *startContainerParams, extendResult composeExtendResult, - imageDetails *config.ImageDetails, -) (composeOverrideFile, error) { - start := params.startParams - +) (composeOverrideMetadata, error) { overlay, err := overlayForParsedConfig( ctx, start.parsedConfig, start.options.ExtraDevContainerPath, ) if err != nil { - return composeOverrideFile{}, err + return composeOverrideMetadata{}, err } parsed := *start.parsedConfig parsed.Overlay = overlay mergedConfig, err := mergeImageMetadataConfig(ctx, &parsed, extendResult.imageMetadata, "") if err != nil { - return composeOverrideFile{}, err + return composeOverrideMetadata{}, err } rawMetadata := overlayRawMetadata(extendResult.imageMetadata.Raw, overlay) metadataValue, err := metadata.MarshalImageMetadata(rawMetadata) + if err != nil { + return composeOverrideMetadata{}, err + } + return composeOverrideMetadata{ + mergedConfig: mergedConfig, + metadataValue: metadataValue, + hasOverlay: overlay != nil, + }, nil +} + +func (r *runner) generateComposeUpOverride( + ctx context.Context, + params *composeOverrideParams, + extendResult composeExtendResult, + imageDetails *config.ImageDetails, +) (composeOverrideFile, error) { + start := params.startParams + meta, err := resolveComposeOverrideMetadata(ctx, start, extendResult) if err != nil { return composeOverrideFile{}, err } @@ -1222,26 +1269,25 @@ func (r *runner) generateComposeUpOverride( contract, err := r.marshalComposeContract( imageDetails, - string(metadataValue), + string(meta.metadataValue), featureSignature, - overlay != nil, + meta.hasOverlay, ) if err != nil { return composeOverrideFile{}, err } - additionalLabels, err := r.wslgLabels() + additionalLabels, err := r.resolveComposeOverrideLabels( + imageDetails, + meta.metadataValue, + contract, + structuralSignature(start.parsedConfig.Config), + ) if err != nil { return composeOverrideFile{}, err } - if additionalLabels == nil { - additionalLabels = map[string]string{} - } - maps.Copy(additionalLabels, composeOverrideLabels( - imageDetails, metadataValue, contract, structuralSignature(start.parsedConfig.Config), - )) overrideComposeUpFilePath, err := r.extendedDockerComposeUp(&composeUpParams{ parsedConfig: start.parsedConfig, - mergedConfig: mergedConfig, + mergedConfig: meta.mergedConfig, composeHelper: start.composeHelper, composeService: params.composeService, originalImageName: params.originalImageName, @@ -1258,6 +1304,24 @@ func (r *runner) generateComposeUpOverride( return composeOverrideFile{path: overrideComposeUpFilePath, contract: string(contract)}, nil } +func (r *runner) resolveComposeOverrideLabels( + imageDetails *config.ImageDetails, + metadataValue, contract []byte, + structure string, +) (map[string]string, error) { + additionalLabels, err := r.wslgLabels() + if err != nil { + return nil, err + } + if additionalLabels == nil { + additionalLabels = map[string]string{} + } + maps.Copy(additionalLabels, composeOverrideLabels( + imageDetails, metadataValue, contract, structure, + )) + return additionalLabels, nil +} + // overlayRawMetadata persists only the established runtime metadata fields of // the overlay, excluding feature options, host initialization, and // source-selection inputs. diff --git a/pkg/devcontainer/config_overlay_lifecycle.go b/pkg/devcontainer/config_overlay_lifecycle.go index 2f83b5edb..35868209f 100644 --- a/pkg/devcontainer/config_overlay_lifecycle.go +++ b/pkg/devcontainer/config_overlay_lifecycle.go @@ -363,10 +363,21 @@ func validateComposeInvocationHeader(invocation composeInvocation) error { len(invocation.Args)%2 != 0 { return fmt.Errorf("invalid persisted Compose invocation") } - if !utf8.ValidString(invocation.Directory) || - strings.ContainsAny(invocation.Directory, "\x00\r\n") { + if err := validateInvocationDirectory(invocation.Directory); err != nil { + return err + } + return validateInvocationWSLg(invocation) +} + +func validateInvocationDirectory(dir string) error { + if !utf8.ValidString(dir) || + strings.ContainsAny(dir, "\x00\r\n") { return fmt.Errorf("invalid persisted Compose directory") } + return nil +} + +func validateInvocationWSLg(invocation composeInvocation) error { if invocation.Version == 2 { return validateWSLgReplay(invocation.WSLg) } diff --git a/pkg/devcontainer/prebuild.go b/pkg/devcontainer/prebuild.go index 579164f77..7492233e9 100644 --- a/pkg/devcontainer/prebuild.go +++ b/pkg/devcontainer/prebuild.go @@ -38,34 +38,18 @@ func (r *runner) Build(ctx context.Context, options provider.BuildOptions) (stri prebuildImage := r.determinePrebuildImage(options, prebuildRepo, buildInfo) - if buildInfo.ImageName == prebuildImage { - return buildInfo.ImageName, nil - } - - // should we push? - if options.SkipPush { - return prebuildImage, nil - } - - // if push happened during build, skip the separate push workflow - // When PushDuringBuild is enabled, BuildKit already pushed the image directly to the - // registry during the build process. Skip the tag/push workflow below to avoid - // redundant operations. The image is already in the registry. - if options.PushDuringBuild { - return prebuildImage, nil - } - - if err := r.pushPrebuildImage(ctx, pushPrebuildImageParams{ - imagePublisher: r.imageBackend, - substitutedConfig: substitutedConfig, - buildInfo: buildInfo, - prebuildImage: prebuildImage, - options: options, - prebuildRepo: prebuildRepo, - }); err != nil { - return "", err + if shouldPushPrebuild(options, buildInfo, prebuildImage) { + if err := r.pushPrebuildImage(ctx, pushPrebuildImageParams{ + imagePublisher: r.imageBackend, + substitutedConfig: substitutedConfig, + buildInfo: buildInfo, + prebuildImage: prebuildImage, + options: options, + prebuildRepo: prebuildRepo, + }); err != nil { + return "", err + } } - return prebuildImage, nil } @@ -106,6 +90,14 @@ func (r *runner) determinePrebuildImage( } } +func shouldPushPrebuild( + options provider.BuildOptions, + buildInfo *config.BuildInfo, + prebuildImage string, +) bool { + return buildInfo.ImageName != prebuildImage && !options.SkipPush && !options.PushDuringBuild +} + type pushPrebuildImageParams struct { imagePublisher driver.ImagePublisher substitutedConfig *config.SubstitutedConfig diff --git a/pkg/devcontainer/run.go b/pkg/devcontainer/run.go index aba3bf56e..bcd74b929 100644 --- a/pkg/devcontainer/run.go +++ b/pkg/devcontainer/run.go @@ -181,19 +181,13 @@ func (r *runner) Up( } defer cleanupBuildInformation(substitutedConfig.Config) defaultSelection := overlayConfigKind(substitutedConfig.Config) == overlayDefaultSelection - if !defaultSelection { - if err := r.checkOverlayRecreation(ctx, substitutedConfig, options); err != nil { - return nil, err - } - } - - if defaultSelection { - // The full overlay check runs after initializeCommand; fail a stale - // WSLg container first so the host hook does not run twice. - err = r.checkWSLgReuseBeforeInitialize(ctx, substitutedConfig.Config, options) - if err != nil { - return nil, err - } + if err := r.checkSelectionBeforeInitialize( + ctx, + substitutedConfig, + options, + defaultSelection, + ); err != nil { + return nil, err } if err := r.runInitializePhase(ctx, reporter, substitutedConfig, options); err != nil { return nil, err @@ -207,20 +201,7 @@ func (r *runner) Up( options: options, timeout: timeout, } - - var result *config.Result - err = status.Run( - ctx, - reporter, - status.Operation{Phase: status.PhasePreparingDevContainer}, - func(ctx context.Context) error { - var dispatchErr error - result, dispatchErr = r.dispatchByConfigKind(ctx, substitutedConfig, params) - return dispatchErr - }, - ) - r.annotateUpResult(result, selectionWarning) - return result, err + return r.runContainerWithStatus(ctx, params, selectionWarning) } func (r *runner) Command(ctx context.Context, params CommandParams) error { @@ -252,6 +233,26 @@ func (r *runner) Logs(ctx context.Context, writer io.Writer) error { return r.driver.GetDevContainerLogs(ctx, r.id, writer, writer) } +func (r *runner) runContainerWithStatus( + ctx context.Context, + params *runContainerParams, + selectionWarning string, +) (*config.Result, error) { + var result *config.Result + err := status.Run( + ctx, + r.reporter, + status.Operation{Phase: status.PhasePreparingDevContainer}, + func(ctx context.Context) error { + var dispatchErr error + result, dispatchErr = r.dispatchByConfigKind(ctx, params.parsedConfig, params) + return dispatchErr + }, + ) + r.annotateUpResult(result, selectionWarning) + return result, err +} + func (r *runner) annotateUpResult(result *config.Result, selectionWarning string) { if result == nil { return @@ -262,6 +263,18 @@ func (r *runner) annotateUpResult(result *config.Result, selectionWarning string } } +func (r *runner) checkSelectionBeforeInitialize( + ctx context.Context, + substitutedConfig *config.SubstitutedConfig, + options UpOptions, + defaultSelection bool, +) error { + if !defaultSelection { + return r.checkOverlayRecreation(ctx, substitutedConfig, options) + } + return r.checkWSLgReuseBeforeInitialize(ctx, substitutedConfig.Config, options) +} + func (r *runner) afterInitialize( ctx context.Context, substitutedConfig *config.SubstitutedConfig,