Skip to content

Commit ff669e5

Browse files
committed
fix: preserve dockerfile indentation and multiline
1 parent 66f7a50 commit ff669e5

13 files changed

+168
-26
lines changed

src/core/generateIncluded.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module.exports = function generateIncluded({
1717
.join(" ")
1818

1919
return (
20-
"\n" +
2120
`# DOCKERFILE-X:START ${attrsStr}` +
2221
"\n" +
2322
includedContent +

src/lib/parseDockerfile.js

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,67 @@ module.exports = function parseDockerfile(content, options = {}) {
99
includeComments: true,
1010
...parseOptions,
1111
})
12-
for (const instruction of instructions) {
12+
const lines = content.split(/\r?\n/)
13+
for (let i = 0; i < instructions.length; i++) {
14+
const instruction = instructions[i]
15+
16+
// Determine start line (0-based) from available fields; fallback to 0
17+
const startIdx =
18+
(instruction.lineno && instruction.lineno - 1) ||
19+
(instruction.startLine && instruction.startLine - 1) ||
20+
(instruction.startline && instruction.startline - 1) ||
21+
(instruction.line && instruction.line - 1) ||
22+
0
23+
24+
// Determine next instruction start (for fallback end bound)
25+
const next = instructions[i + 1]
26+
const nextStartIdx = next
27+
? (next.lineno && next.lineno - 1) ||
28+
(next.startLine && next.startLine - 1) ||
29+
(next.startline && next.startline - 1) ||
30+
(next.line && next.line - 1) ||
31+
lines.length
32+
: lines.length
33+
34+
// End index: prefer docker-file-parser's lineno (last line of instruction), else before next instruction
35+
const endIdx =
36+
instruction.lineno && Number.isInteger(instruction.lineno)
37+
? instruction.lineno - 1
38+
: Math.max(startIdx, nextStartIdx - 1)
39+
40+
// Recover the true start by scanning upward from endIdx until the header line (instruction keyword) is found
41+
const startsWithName = (line, name) =>
42+
new RegExp(`^\\s*${name}\\b`, "i").test(line)
43+
44+
let startIdxScan = endIdx
45+
if (instruction.name !== "COMMENT") {
46+
while (
47+
startIdxScan >= 0 &&
48+
!startsWithName(lines[startIdxScan] || "", instruction.name)
49+
) {
50+
startIdxScan--
51+
}
52+
if (startIdxScan < 0) {
53+
startIdxScan = startIdx
54+
}
55+
}
56+
57+
const defaultRaw = lines.slice(startIdxScan, endIdx + 1).join("\n")
58+
1359
if (instruction.name === "COMMENT") {
1460
if (removeSyntaxComments && syntaxCommentRegex.test(instruction.args)) {
1561
instruction.raw = ""
1662
} else {
17-
instruction.raw = instruction.args
63+
// Preserve exact original comment formatting (including indentation)
64+
instruction.raw = defaultRaw
1865
}
19-
}
20-
if (instruction.name === "FROM") {
66+
} else if (instruction.name === "FROM") {
67+
// Normalize "AS" casing but keep args semantics
2168
instruction.args = instruction.args.replace(/(\s+)as(\s+)/gi, "$1AS$2")
2269
instruction.raw = generateRawInstruction(instruction)
70+
} else {
71+
// Preserve exact original formatting for all other instructions (e.g., RUN with continuations/indentation)
72+
instruction.raw = defaultRaw
2373
}
2474
}
2575
return instructions

test/__snapshots__/COPY/copyFrom.expected.dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ ENV HOME=/home/ubuntu
1111
RUN chmod 0777 /home/ubuntu
1212
RUN mkdir /app && chown 1000:1000 /app
1313
# DOCKERFILE-X:END file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" includeType="include"
14-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends curl ca-certificates wget git && rm -rf /var/lib/apt/lists/*
14+
RUN apt-get update && \
15+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
16+
curl \
17+
ca-certificates \
18+
wget \
19+
git \
20+
&& rm -rf /var/lib/apt/lists/*
1521
# DOCKERFILE-X:END file="./downloader.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
1622
FROM nodee5203c--downlo550515 AS nodee5203c--build-node
1723
# renovate: datasource=node depName=node versioning=node
1824
ARG NODE_VERSION=20.3.0
1925
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
20-
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/ && mv /opt/$NODE_PACKAGE /opt/node
26+
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz \
27+
| tar -xzC /opt/ \
28+
&& mv /opt/$NODE_PACKAGE /opt/node
2129
# DOCKERFILE-X:START file="./ubuntu.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
2230
ARG UBUNTU_VERSION=22.04
2331
FROM ubuntu:$UBUNTU_VERSION AS nodee5203c--ubuntu9e4275--final-stage

test/__snapshots__/COPY/copyFromStage.expected.dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ ENV HOME=/home/ubuntu
1111
RUN chmod 0777 /home/ubuntu
1212
RUN mkdir /app && chown 1000:1000 /app
1313
# DOCKERFILE-X:END file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" includeType="include"
14-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends curl ca-certificates wget git && rm -rf /var/lib/apt/lists/*
14+
RUN apt-get update && \
15+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
16+
curl \
17+
ca-certificates \
18+
wget \
19+
git \
20+
&& rm -rf /var/lib/apt/lists/*
1521
# DOCKERFILE-X:END file="./downloader.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
1622
FROM nodee5203c--downlo550515 AS nodee5203c--build-node
1723
FROM nodee5203c--build-node AS nodee5203c
1824
# renovate: datasource=node depName=node versioning=node
1925
ARG NODE_VERSION=20.3.0
2026
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
21-
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/ && mv /opt/$NODE_PACKAGE /opt/node
27+
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz \
28+
| tar -xzC /opt/ \
29+
&& mv /opt/$NODE_PACKAGE /opt/node
2230
# DOCKERFILE-X:START file="./ubuntu.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
2331
ARG UBUNTU_VERSION=22.04
2432
FROM ubuntu:$UBUNTU_VERSION AS nodee5203c--ubuntu9e4275--final-stage

test/__snapshots__/FROM/from.expected.dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,17 @@ ENV HOME=/home/ubuntu
99
RUN chmod 0777 /home/ubuntu
1010
RUN mkdir /app && chown 1000:1000 /app
1111
# DOCKERFILE-X:END file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" includeType="include"
12-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends curl ca-certificates wget git && rm -rf /var/lib/apt/lists/*
12+
RUN apt-get update && \
13+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
14+
curl \
15+
ca-certificates \
16+
wget \
17+
git \
18+
&& rm -rf /var/lib/apt/lists/*
1319
# DOCKERFILE-X:END file="./inc/downloader.dockerfile" includedBy="from.dockerfile" includeType="from"
1420
FROM downlo550515 AS final-stage
1521
ARG NODE_VERSION=20.3.0
1622
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
17-
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/ && mv /opt/$NODE_PACKAGE /opt/node
23+
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz \
24+
| tar -xzC /opt/ \
25+
&& mv /opt/$NODE_PACKAGE /opt/node

test/__snapshots__/FROM/fromIncludeArg.expected.dockerfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ ARG UBUNTU_VERSION=22.04
22
FROM debian AS node
33
ARG NODE_VERSION=20.3.0
44
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
5-
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/ && mv /opt/$NODE_PACKAGE /opt/node
5+
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz \
6+
| tar -xzC /opt/ \
7+
&& mv /opt/$NODE_PACKAGE /opt/node
68
# DOCKERFILE-X:START file="./inc/ubuntu.dockerfile" includedBy="fromIncludeArg.dockerfile" includeType="from"
79
ARG UBUNTU_VERSION=22.04
810
FROM ubuntu:$UBUNTU_VERSION AS ubuntu9e4275--final-stage

test/__snapshots__/FROM/fromStageAlias.expected.dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ ENV HOME=/home/ubuntu
99
RUN chmod 0777 /home/ubuntu
1010
RUN mkdir /app && chown 1000:1000 /app
1111
# DOCKERFILE-X:END file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" includeType="include"
12-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends curl ca-certificates wget git && rm -rf /var/lib/apt/lists/*
12+
RUN apt-get update && \
13+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
14+
curl \
15+
ca-certificates \
16+
wget \
17+
git \
18+
&& rm -rf /var/lib/apt/lists/*
1319
# DOCKERFILE-X:END file="./inc/downloader.dockerfile" includedBy="fromStageAlias.dockerfile" includeType="from"
1420
FROM downlo550515 AS build-node
1521
ARG NODE_VERSION=20.3.0
1622
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
17-
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/ && mv /opt/$NODE_PACKAGE /opt/node
23+
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz \
24+
| tar -xzC /opt/ \
25+
&& mv /opt/$NODE_PACKAGE /opt/node
1826
FROM build-node AS build-node-extra
1927
RUN npm install -g yarn

test/__snapshots__/FROM/fromWithTarget.expected.dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ ENV HOME=/home/ubuntu
1111
RUN chmod 0777 /home/ubuntu
1212
RUN mkdir /app && chown 1000:1000 /app
1313
# DOCKERFILE-X:END file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" includeType="include"
14-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends curl ca-certificates wget git && rm -rf /var/lib/apt/lists/*
14+
RUN apt-get update && \
15+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
16+
curl \
17+
ca-certificates \
18+
wget \
19+
git \
20+
&& rm -rf /var/lib/apt/lists/*
1521
# DOCKERFILE-X:END file="./downloader.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
1622
FROM nodee5203c--downlo550515 AS nodee5203c--build-node
1723
FROM nodee5203c--build-node AS nodee5203c
1824
# renovate: datasource=node depName=node versioning=node
1925
ARG NODE_VERSION=20.3.0
2026
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
21-
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/ && mv /opt/$NODE_PACKAGE /opt/node
27+
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz \
28+
| tar -xzC /opt/ \
29+
&& mv /opt/$NODE_PACKAGE /opt/node
2230
# DOCKERFILE-X:START file="./ubuntu.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
2331
ARG UBUNTU_VERSION=22.04
2432
FROM ubuntu:$UBUNTU_VERSION AS nodee5203c--ubuntu9e4275--final-stage

test/__snapshots__/FROM/fromWithTargetStageAlias.expected.dockerfile

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ ENV HOME=/home/ubuntu
1111
RUN chmod 0777 /home/ubuntu
1212
RUN mkdir /app && chown 1000:1000 /app
1313
# DOCKERFILE-X:END file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" includeType="include"
14-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends curl ca-certificates wget git && rm -rf /var/lib/apt/lists/*
14+
RUN apt-get update && \
15+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
16+
curl \
17+
ca-certificates \
18+
wget \
19+
git \
20+
&& rm -rf /var/lib/apt/lists/*
1521
# DOCKERFILE-X:END file="./downloader.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
1622
FROM nodee5203c--downlo550515 AS nodee5203c--build-node
1723
FROM nodee5203c--build-node AS nodee5203c
1824
# renovate: datasource=node depName=node versioning=node
1925
ARG NODE_VERSION=20.3.0
2026
ARG NODE_PACKAGE=node-v$NODE_VERSION-linux-x64
21-
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz | tar -xzC /opt/ && mv /opt/$NODE_PACKAGE /opt/node
27+
RUN curl https://nodejs.org/dist/v$NODE_VERSION/$NODE_PACKAGE.tar.gz \
28+
| tar -xzC /opt/ \
29+
&& mv /opt/$NODE_PACKAGE /opt/node
2230
# DOCKERFILE-X:START file="./ubuntu.dockerfile" includedBy="inc/node.dockerfile" includeType="from"
2331
ARG UBUNTU_VERSION=22.04
2432
FROM ubuntu:$UBUNTU_VERSION AS nodee5203c--ubuntu9e4275--final-stage

test/__snapshots__/INCLUDE/include.expected.dockerfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,11 @@ ENV HOME=/home/ubuntu
88
RUN chmod 0777 /home/ubuntu
99
RUN mkdir /app && chown 1000:1000 /app
1010
# DOCKERFILE-X:END file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" includeType="include"
11-
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends curl ca-certificates wget git && rm -rf /var/lib/apt/lists/*
11+
RUN apt-get update && \
12+
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
13+
curl \
14+
ca-certificates \
15+
wget \
16+
git \
17+
&& rm -rf /var/lib/apt/lists/*
1218
# DOCKERFILE-X:END file="inc/downloader.dockerfile" includedBy="include.dockerfile" includeType="include"

0 commit comments

Comments
 (0)