Skip to content

Commit 31afe9d

Browse files
hiroTamadaclaude
andcommitted
fix: track inter-stage aliases in Dockerfile FROM rewriting
rewriteDockerfileFROMs was missing stage name tracking, which could cause it to rewrite inter-stage FROM references (e.g. FROM builder) to point at the local registry instead of the prior build stage. Also skips ARG variable references (${...}) that can't be resolved. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 952b215 commit 31afe9d

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

lib/builds/builder_agent/dockerfile_rewrite_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,40 @@ COPY binary /`,
139139
RUN echo hello`,
140140
expectedCount: 0,
141141
expected: `FROM ` + registryURL + `/onkernel/nodejs22-base:0.1.1
142+
RUN echo hello`,
143+
},
144+
{
145+
name: "inter-stage FROM reference is not rewritten",
146+
dockerfile: `FROM onkernel/nodejs22-base:0.1.1 AS builder
147+
RUN npm install
148+
FROM builder
149+
COPY --from=builder /app /app`,
150+
expectedCount: 1,
151+
expected: `FROM ` + registryURL + `/onkernel/nodejs22-base:0.1.1 AS builder
152+
RUN npm install
153+
FROM builder
154+
COPY --from=builder /app /app`,
155+
},
156+
{
157+
name: "inter-stage reference case insensitive",
158+
dockerfile: `FROM onkernel/nodejs22-base:0.1.1 AS Builder
159+
RUN npm install
160+
FROM builder
161+
COPY --from=builder /app /app`,
162+
expectedCount: 1,
163+
expected: `FROM ` + registryURL + `/onkernel/nodejs22-base:0.1.1 AS Builder
164+
RUN npm install
165+
FROM builder
166+
COPY --from=builder /app /app`,
167+
},
168+
{
169+
name: "variable reference FROM is not rewritten",
170+
dockerfile: `ARG BASE_IMAGE=onkernel/nodejs22-base:0.1.1
171+
FROM ${BASE_IMAGE}
172+
RUN echo hello`,
173+
expectedCount: 0,
174+
expected: `ARG BASE_IMAGE=onkernel/nodejs22-base:0.1.1
175+
FROM ${BASE_IMAGE}
142176
RUN echo hello`,
143177
},
144178
}

lib/builds/builder_agent/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ func rewriteDockerfileFROMs(dockerfilePath, registryURL string, insecure bool, r
937937

938938
lines := strings.Split(string(content), "\n")
939939
rewriteCount := 0
940+
stageNames := make(map[string]bool)
940941

941942
for i, line := range lines {
942943
trimmed := strings.TrimSpace(line)
@@ -971,6 +972,14 @@ func rewriteDockerfileFROMs(dockerfilePath, registryURL string, insecure bool, r
971972

972973
imageRef := parts[imageIdx]
973974

975+
// Record AS alias if present
976+
for j := imageIdx + 1; j < len(parts)-1; j++ {
977+
if strings.EqualFold(parts[j], "AS") {
978+
stageNames[strings.ToLower(parts[j+1])] = true
979+
break
980+
}
981+
}
982+
974983
// Skip if already referencing the local registry
975984
if strings.HasPrefix(imageRef, registryURL+"/") {
976985
continue
@@ -981,6 +990,16 @@ func rewriteDockerfileFROMs(dockerfilePath, registryURL string, insecure bool, r
981990
continue
982991
}
983992

993+
// Skip inter-stage references (e.g. FROM builder)
994+
if stageNames[strings.ToLower(imageRef)] {
995+
continue
996+
}
997+
998+
// Skip variable references that can't be resolved
999+
if strings.Contains(imageRef, "${") {
1000+
continue
1001+
}
1002+
9841003
// Normalize the image reference
9851004
// Docker Hub images can be referenced as:
9861005
// - "nginx" (library image)

0 commit comments

Comments
 (0)