Skip to content

Commit 834bf15

Browse files
aledbfclaude
andcommitted
fix(docker): keep the stage name of a continued final FROM line
TS ensureDockerfileHasFinalStageName reads only the physical last FROM line, so `FROM \` followed by `alpine AS base` becomes `FROM \ AS dev`, a Dockerfile no builder accepts. Ask BuildKit for that stage's name instead; every other FROM line keeps TS's handling. The ts-oracle golden records TS's answer as a divergence case, and docs/DIVERGENCES.md explains it. A `# escape=` directive now follows TS too, also recorded in the golden. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 2b48688 commit 834bf15

4 files changed

Lines changed: 73 additions & 0 deletions

File tree

‎docs/DIVERGENCES.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ touches a compared surface, reflected in the parity matrix.
2020
`stop`/`down` (pause, or stop+remove, a workspace's container — Compose-aware; the
2121
upstream CLI leaves teardown to the editor), and the automatic credential bridge that
2222
hands the CLI's resolved auth to `docker build`.
23+
- **A final `FROM` line continued with a backslash keeps its stage name.** TS
24+
`ensureDockerfileHasFinalStageName` reads only the physical line, so for
25+
`FROM \` followed by `alpine AS base` it writes `FROM \ AS dev`, a Dockerfile
26+
no builder accepts. This CLI asks BuildKit for that stage's name and leaves the
27+
Dockerfile as it is (`TestEnsureFinalStageNameReadsAContinuedFrom`,
28+
`internal/docker/testdata/ts-oracle/dockerfile.json`). Every other FROM line is
29+
handled exactly as TS handles it.
2330
- **`--override-config` deep-merges** the override onto the base config, whereas TS
2431
replaces the config wholesale (`readDocument(overrideConfigFile ?? configFile)`). With
2532
no readable base, the override stands alone — identical to TS. This lets an orchestrator

‎internal/docker/dockerfile.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,22 @@ var (
196196
// returned only when a name was added; otherwise modifiedContent is empty. A
197197
// Dockerfile without a FROM line, or whose last FROM line does not parse, is
198198
// TS's error.
199+
//
200+
// One deliberate divergence (docs/DIVERGENCES.md): when the last FROM line is
201+
// continued with a backslash, TS writes " AS <defaultName>" after the
202+
// backslash, which no builder accepts; this reads the stage's name with
203+
// BuildKit instead when it has one.
199204
func EnsureFinalStageName(content, defaultName string) (stageName string, modifiedContent string, err error) {
200205
lines := findFromLines.FindAllStringIndex(content, -1)
201206
if len(lines) == 0 {
202207
return "", "", errors.New("Error parsing Dockerfile: Dockerfile contains no FROM instructions")
203208
}
204209
lineStart, lineEnd := lines[len(lines)-1][0], lines[len(lines)-1][1]
210+
if strings.HasSuffix(strings.TrimRight(content[lineStart:lineEnd], " \t"), `\`) {
211+
if name := finalStageName(content); name != "" {
212+
return name, "", nil
213+
}
214+
}
205215
m := fromLineLabel.FindStringSubmatchIndex(content[lineStart:lineEnd])
206216
if m == nil {
207217
return "", "", errors.New("Error parsing Dockerfile: failed to parse final FROM line")
@@ -212,3 +222,17 @@ func EnsureFinalStageName(content, defaultName string) (stageName string, modifi
212222
pos := lineStart + m[1]
213223
return defaultName, content[:pos] + " AS " + defaultName + content[pos:], nil
214224
}
225+
226+
// finalStageName is the name BuildKit reads for the last stage, "" when it has
227+
// none or the Dockerfile does not parse.
228+
func finalStageName(content string) string {
229+
res, err := parser.Parse(strings.NewReader(content))
230+
if err != nil {
231+
return ""
232+
}
233+
stages, _, err := instructions.Parse(res.AST, nil)
234+
if err != nil || len(stages) == 0 {
235+
return ""
236+
}
237+
return stages[len(stages)-1].Name
238+
}

‎internal/docker/dockerfile_held_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ import (
44
"testing"
55
)
66

7+
// A FROM continued onto the next line is one instruction: BuildKit's parser
8+
// joins it, so the stage name on the continuation line is found and the
9+
// Dockerfile is left alone. The TS regex (dockerfileUtils.ts
10+
// ensureDockerfileHasFinalStageName) reads only the physical "FROM \" line
11+
// and writes " AS dev" after the backslash; EnsureFinalStageName asks BuildKit
12+
// for exactly this case (docs/DIVERGENCES.md).
13+
func TestEnsureFinalStageNameReadsAContinuedFrom(t *testing.T) {
14+
for _, tc := range []struct{ content, want string }{
15+
{"FROM \\\n alpine:3.20 AS base\n", "base"},
16+
{"FROM alpine AS build\nRUN true\nFROM \\\n build AS final\n", "final"},
17+
} {
18+
name, modified, err := EnsureFinalStageName(tc.content, "dev")
19+
if err != nil || name != tc.want || modified != "" {
20+
t.Errorf("EnsureFinalStageName(%q) = (%q, %q, %v), want (%q, \"\", nil)", tc.content, name, modified, err, tc.want)
21+
}
22+
}
23+
}
24+
725
// When BuildKit rejects the file (a trailing comment after the image), the
826
// label goes right after the image of the last FROM, at the same byte offset
927
// TS computes from the match index (dockerfileUtils.ts: `dockerfile.slice(0,

‎internal/docker/testdata/ts-oracle/dockerfile.json‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,30 @@
885885
"ref=x,type=registry,mode=inline"
886886
],
887887
"result": false
888+
},
889+
{
890+
"name": "continued FROM line",
891+
"fn": "ensureDockerfileHasFinalStageName",
892+
"args": [
893+
"FROM \\\n alpine:3.20 AS base\n",
894+
"dev"
895+
],
896+
"divergence": "docs/DIVERGENCES.md: a FROM line continued with a backslash keeps its stage name (TS writes \" AS <name>\" after the backslash, which no builder accepts)",
897+
"result": {
898+
"lastStageName": "dev",
899+
"modifiedDockerfile": "FROM \\ AS dev\n alpine:3.20 AS base\n"
900+
}
901+
},
902+
{
903+
"name": "escape directive",
904+
"fn": "findBaseImage",
905+
"args": [
906+
"# escape=`\nARG x\nFROM reg`$x/img\n",
907+
{},
908+
null,
909+
{}
910+
],
911+
"result": "reg`/img"
888912
}
889913
]
890914
}

0 commit comments

Comments
 (0)