GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — core: fix $BUILDPLATFORM resolving to the target platform in dockerBuild (cross-platform cache regression) by sipsma · Pull Request #13571 · dagger/dagger

Conversation

@sipsma

@sipsma sipsma commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Problem

Users upgrading Dagger from 0.20.8 to 0.21.x reported losing layer caching that used to hit — most visibly when building a Dockerfile for a non-native platform.

Their setup: a large multi-stage Dockerfile built for both linux/arm64 and linux/amd64, on an engine running linux/amd64. Most stages are pinned to the build host with FROM --platform=$BUILDPLATFORM so they run natively (no emulation), and only the final release stages are built for the target platform. On 0.20.8 the build took ~56s; on 0.21.6 the same build took ~1m52s.

The core symptom: in 0.21.x the amd64 and arm64 builds no longer shared cache for the $BUILDPLATFORM-pinned stages, even though those stages should be identical across target archs (they all target the same build platform). The reporters found that explicitly passing a BUILDPLATFORM build arg set to the native platform restored the caching, and noticed that without it, an amd64-target build reported x86_64 where they expected the build-host arch.

Root cause

The predefined BUILDPLATFORM (and BUILDOS/BUILDARCH/BUILDVARIANT) build args stopped resolving to the engine's native/build-host platform and instead tracked the target platform.

In the dagql-native Dockerfile build path, (*Container).Build in core/container.go constructs dockerfile2llb.ConvertOpt with TargetPlatform set but leaves BuildPlatforms (a field on the embedded dockerui.Config) unset. buildPlatformOpt in internal/buildkit/frontend/dockerfile/dockerfile2llb/platform.go then falls back to using the target platform as the build platform:

if opt.TargetPlatform != nil && opt.BuildPlatforms == nil {
    buildPlatforms = []ocispecs.Platform{*opt.TargetPlatform}
}

so getPlatformArgs seeds BUILDPLATFORM = platforms.Format(buildPlatforms[0]) = the target platform. That means BUILDPLATFORM == TARGETPLATFORM, which (a) emulates $BUILDPLATFORM stages that should run natively on the host, and (b) diverges the resulting cache key per target platform, so the "shared" build-host stages of an amd64 vs arm64 build no longer dedupe.

This is a regression from #11856 ("migrate all caching to dagql"), which switched off the BuildKit solver. Previously these builds went through BuildKit's dockerui frontend, whose Client.init (internal/buildkit/frontend/dockerui/config.go) always sets BuildPlatforms to the worker's native platform. The dagql-native path builds ConvertOpt directly and never replicated that.

Fix

Set BuildPlatforms to the engine's native platform in the ConvertOpt, matching what the dockerui frontend does:

BuildPlatforms: []specs.Platform{query.Platform().Spec()},

query.Platform() is the engine's default/host platform (platforms.Normalize(platforms.DefaultSpec())). With BuildPlatforms populated, BUILDPLATFORM resolves to the native platform again regardless of the target, while TARGETPLATFORM/TARGETOS/etc. continue to reflect the requested target. Users who explicitly set a BUILDPLATFORM build arg still override it (unchanged behavior).

The change is scoped to the Dockerfile build path; it's the only place in core that constructs dockerfile2llb.ConvertOpt. Dockerfiles that don't reference $BUILDPLATFORM are unaffected (the predefined args only materialize into the build when referenced), so this doesn't gratuitously invalidate existing caches.

Test

Adds TestDockerBuildBuildPlatformArg in core/integration/dockerfile_test.go, which builds a small Dockerfile using FROM --platform=$BUILDPLATFORM for a non-native target and asserts:

  1. BUILDPLATFORM resolves to the engine's native platform (not the target);
  2. the $BUILDPLATFORM stage is identical across two different target platforms (i.e. it builds for the native arch, so the DAGs/cache keys don't diverge);
  3. explicitly passing a BUILDPLATFORM build arg (the reported workaround) also yields native behavior.

This test fails on main before the fix and passes with it.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@sipsma
core: resolve $BUILDPLATFORM to the engine's native platform in docke…
1494395
…rBuild

The dagql-native Dockerfile build path built dockerfile2llb.ConvertOpt with
TargetPlatform set but BuildPlatforms left unset. When BuildPlatforms is nil,
buildPlatformOpt falls back to using the target platform as the build platform,
so the predefined BUILDPLATFORM/BUILDOS/BUILDARCH build args tracked the target
platform instead of the engine's native/host platform.

That regressed multi-platform Dockerfile builds that use
FROM --platform=$BUILDPLATFORM to keep stages on the build host: those stages
were emulated to the target arch and, because their value diverged per target,
they no longer shared cache across target platforms.

Set BuildPlatforms to the engine's native platform, matching what BuildKit's
dockerui frontend does. Regression from dagger#11856 (the buildkit-solver -> dagql
migration).

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma sipsma changed the title core: fix \$BUILDPLATFORM resolving to the target platform in dockerBuild (cross-platform cache regression) core: fix $BUILDPLATFORM resolving to the target platform in dockerBuild (cross-platform cache regression) Jul 2, 2026
@grouville
grouville self-requested a review July 13, 2026 13:25
grouville
grouville approved these changes Jul 13, 2026
View reviewed changes

@grouville grouville left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clear, thanks 🙏

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
tiborvass
tiborvass approved these changes Jul 13, 2026
View reviewed changes
@tiborvass
tiborvass merged commit 7c4d2d1 into dagger:main Jul 13, 2026
81 checks passed

Uh oh!

There was an error while loading. Please reload this page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tiborvass tiborvass tiborvass approved these changes
@grouville grouville grouville approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@sipsma @tiborvass @grouville
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.