diff --git a/frontend/dockerfile/dockerfile2llb/convert.go b/frontend/dockerfile/dockerfile2llb/convert.go index c084e3991ee3..09e9d1b3150d 100644 --- a/frontend/dockerfile/dockerfile2llb/convert.go +++ b/frontend/dockerfile/dockerfile2llb/convert.go @@ -801,10 +801,15 @@ func toDispatchState(ctx context.Context, dt []byte, opt ConvertOpt) (*dispatchS target.state = target.state.SetMarshalDefaults(defaults...) if !platformOpt.implicitTarget { + sameOsArch := platformOpt.targetPlatform.OS == target.image.OS && platformOpt.targetPlatform.Architecture == target.image.Architecture target.image.OS = platformOpt.targetPlatform.OS target.image.Architecture = platformOpt.targetPlatform.Architecture - target.image.Variant = platformOpt.targetPlatform.Variant - target.image.OSVersion = platformOpt.targetPlatform.OSVersion + if platformOpt.targetPlatform.Variant != "" || !sameOsArch { + target.image.Variant = platformOpt.targetPlatform.Variant + } + if platformOpt.targetPlatform.OSVersion != "" || !sameOsArch { + target.image.OSVersion = platformOpt.targetPlatform.OSVersion + } if platformOpt.targetPlatform.OSFeatures != nil { target.image.OSFeatures = append([]string{}, platformOpt.targetPlatform.OSFeatures...) } diff --git a/frontend/dockerfile/dockerfile_test.go b/frontend/dockerfile/dockerfile_test.go index c1195dbbb503..b78b6d3ae2ca 100644 --- a/frontend/dockerfile/dockerfile_test.go +++ b/frontend/dockerfile/dockerfile_test.go @@ -217,6 +217,7 @@ var allTests = integration.TestFuncs( testPowershellInDefaultPathOnWindows, testOCILayoutMultiname, testPlatformWithOSVersion, + testMaintainBaseOSVersion, ) // Tests that depend on the `security.*` entitlements @@ -9634,6 +9635,119 @@ EOF require.Equal(t, p2.OSVersion+"\n", string(dt)) } +func testMaintainBaseOSVersion(t *testing.T, sb integration.Sandbox) { + integration.SkipOnPlatform(t, "windows") + workers.CheckFeatureCompat(t, sb, workers.FeatureDirectPush) + + ctx := sb.Context() + + c, err := client.New(ctx, sb.Address()) + require.NoError(t, err) + defer c.Close() + + f := getFrontend(t, sb) + + p1 := ocispecs.Platform{ + OS: "windows", + OSVersion: "10.20.30", + Architecture: "amd64", + } + p1Str := platforms.FormatAll(p1) + + registry, err := sb.NewRegistry() + if errors.Is(err, integration.ErrRequirements) { + t.Skip(err.Error()) + } + require.NoError(t, err) + target := registry + "/buildkit/testplatformwithosversion-1:latest" + + dockerfile := []byte(` +FROM scratch +ARG TARGETPLATFORM +COPY <