From bee6f2a6bff85be7db9ad0218cb07ec29713876b Mon Sep 17 00:00:00 2001 From: devthejo Date: Sun, 24 Dec 2023 12:16:15 +0100 Subject: [PATCH] fix: include target --- src/core/loadDockerfile.js | 2 + src/core/postProcessDockerfile.js | 17 ++++++-- src/instruction/from.js | 2 +- src/instruction/fromParam.js | 2 +- src/instruction/include.js | 3 +- .../INCLUDE/include.expected.dockerfile | 5 +-- .../issues/include-target.expected.dockerfile | 17 ++++++++ test/fixs.js | 6 +++ test/fixtures/inc/python.dockerfile | 15 +++++++ test/fixtures/issue1.dockerfile | 39 +++++++++++++++++++ 10 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 test/__snapshots__/issues/include-target.expected.dockerfile create mode 100644 test/fixtures/inc/python.dockerfile create mode 100644 test/fixtures/issue1.dockerfile diff --git a/src/core/loadDockerfile.js b/src/core/loadDockerfile.js index 29d01ce..d743a55 100644 --- a/src/core/loadDockerfile.js +++ b/src/core/loadDockerfile.js @@ -19,6 +19,7 @@ async function loadDockerfile(filePath, fileContext = {}) { nestingLevel = 0, dockerContext, scope = [], + isRootInclude, } = fileContext const relativeFilePath = path.relative(dockerContext, filePath) @@ -77,6 +78,7 @@ async function loadDockerfile(filePath, fileContext = {}) { stageAlias: parentStageAlias, filePath, isRootFile, + isRootInclude, scope, relativeFilePath, }) diff --git a/src/core/postProcessDockerfile.js b/src/core/postProcessDockerfile.js index 26bd398..445525c 100644 --- a/src/core/postProcessDockerfile.js +++ b/src/core/postProcessDockerfile.js @@ -10,11 +10,19 @@ const scopeStages = require("./scopeStages") module.exports = function postProcessDockerfile( content, - { stageTarget, stageAlias, filePath, isRootFile, relativeFilePath, scope }, + { + stageTarget, + stageAlias, + filePath, + isRootFile, + isRootInclude, + relativeFilePath, + scope, + }, ) { const instructions = parseDockerfile(content) - if (!isRootFile) { + if (!isRootFile && !isRootInclude) { scopeStages(instructions, { relativeFilePath }) } @@ -46,7 +54,8 @@ module.exports = function postProcessDockerfile( if (matchTargetStage) { stageTargetFound = true } - const isLastTarget = !localStageTarget && currentFromCount === fromCount + const isLastTarget = + !isRootInclude && !localStageTarget && currentFromCount === fromCount if (stageAlias && (matchTargetStage || isLastTarget)) { result += `FROM ${stageName} AS ${stageAlias}` + "\n" } @@ -61,7 +70,7 @@ module.exports = function postProcessDockerfile( result = deduplicateStages(result) - if (isRootFile) { + if (isRootFile || isRootInclude) { result = sanitizeStageNames(result) } diff --git a/src/instruction/from.js b/src/instruction/from.js index 84a828d..ebd33b4 100644 --- a/src/instruction/from.js +++ b/src/instruction/from.js @@ -7,7 +7,7 @@ const loadDockerfile = require("../core/loadDockerfile") const generateIncluded = require("../core/generateIncluded") module.exports = ({ - nestingLevel, + nestingLevel = 0, dockerContext, filePath, relativeFilePath, diff --git a/src/instruction/fromParam.js b/src/instruction/fromParam.js index ddfbb88..4a33425 100644 --- a/src/instruction/fromParam.js +++ b/src/instruction/fromParam.js @@ -7,7 +7,7 @@ const loadDockerfile = require("../core/loadDockerfile") const generateIncluded = require("../core/generateIncluded") module.exports = ({ - nestingLevel, + nestingLevel = 0, dockerContext, filePath, relativeFilePath, diff --git a/src/instruction/include.js b/src/instruction/include.js index 8c53cc4..3a08d68 100644 --- a/src/instruction/include.js +++ b/src/instruction/include.js @@ -5,7 +5,7 @@ const loadDockerfile = require("../core/loadDockerfile") const generateIncluded = require("../core/generateIncluded") module.exports = ({ - nestingLevel, + nestingLevel = 0, dockerContext, filePath, relativeFilePath, @@ -31,6 +31,7 @@ module.exports = ({ parentStageTarget: null, parentStageAlias: stageAlias, nestingLevel: nestingLevel + 1, + isRootInclude: nestingLevel === 0, }) const result = [] diff --git a/test/__snapshots__/INCLUDE/include.expected.dockerfile b/test/__snapshots__/INCLUDE/include.expected.dockerfile index a3bd96f..a5bc729 100644 --- a/test/__snapshots__/INCLUDE/include.expected.dockerfile +++ b/test/__snapshots__/INCLUDE/include.expected.dockerfile @@ -1,9 +1,8 @@ # DOCKERFILE-X:START file="inc/downloader.dockerfile" includedBy="include.dockerfile" # DOCKERFILE-X:START file="ubuntu.dockerfile" includedBy="inc/downloader.dockerfile" ARG UBUNTU_VERSION=22.04 -FROM ubuntu:$UBUNTU_VERSION AS downlo550515--ubuntu9e4275--final-stage -FROM downlo550515--ubuntu9e4275--final-stage AS downlo550515--ubuntu9e4275 -FROM downlo550515--ubuntu9e4275 AS downlo550515 +FROM ubuntu:$UBUNTU_VERSION AS ubuntu9e4275--final-stage +FROM ubuntu9e4275--final-stage AS ubuntu9e4275 RUN groupadd -g 1000 ubuntu && useradd -rm -d /home/ubuntu -s /bin/bash -g ubuntu -G sudo -u 1000 ubuntu ENV HOME=/home/ubuntu RUN chmod 0777 /home/ubuntu diff --git a/test/__snapshots__/issues/include-target.expected.dockerfile b/test/__snapshots__/issues/include-target.expected.dockerfile new file mode 100644 index 0000000..127cf6c --- /dev/null +++ b/test/__snapshots__/issues/include-target.expected.dockerfile @@ -0,0 +1,17 @@ +# See https://codeberg.org/devthefuture/dockerfile-x. +ARG JAVA_VERSION=17 +# Even if no JDK is required for rtx, use an Ubuntu base image that incudes the JDK and which is also used by other +# stages in order to reduce the total number of base images used. +FROM eclipse-temurin:$JAVA_VERSION-jdk-jammy AS build +# See https://docs.docker.com/build/cache/#use-the-dedicated-run-cache +RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y --no-install-recommends ca-certificates curl gnupg && install -dm 755 /etc/apt/keyrings && curl https://rtx.jdx.dev/gpg-key.pub | gpg --dearmor > /etc/apt/keyrings/rtx-archive-keyring.gpg && echo "deb [signed-by=/etc/apt/keyrings/rtx-archive-keyring.gpg arch=amd64] https://rtx.jdx.dev/deb stable main" > /etc/apt/sources.list.d/rtx.list && apt-get update && apt-get install -y --no-install-recommends rtx +RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y --no-install-recommends build-essential git +# Ease debugging of rtx errors in derived images. +ENV RTX_VERBOSE=1 +# Make the tool versions available to derived images. +COPY .tool-versions . +# DOCKERFILE-X:START file="inc/python.dockerfile" includedBy="issue1.dockerfile" +FROM build AS python-build +RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install -y --no-install-recommends libbz2-dev libffi-dev liblzma-dev libncurses-dev libreadline-dev libssl-dev libz-dev +RUN rtx install python +# DOCKERFILE-X:END file="inc/python.dockerfile" includedBy="issue1.dockerfile" diff --git a/test/fixs.js b/test/fixs.js index da95317..5c0c255 100644 --- a/test/fixs.js +++ b/test/fixs.js @@ -11,3 +11,9 @@ describe("dedup", () => { await testFixture("dedup") }) }) + +describe("issues", () => { + it("include-target", async () => { + await testFixture("issue1") + }) +}) diff --git a/test/fixtures/inc/python.dockerfile b/test/fixtures/inc/python.dockerfile new file mode 100644 index 0000000..09353fb --- /dev/null +++ b/test/fixtures/inc/python.dockerfile @@ -0,0 +1,15 @@ +FROM build AS python-build + +RUN --mount=type=cache,target=/var/cache/apt \ + # Install tools required for building. + apt-get update && \ + apt-get install -y --no-install-recommends \ + libbz2-dev \ + libffi-dev \ + liblzma-dev \ + libncurses-dev \ + libreadline-dev \ + libssl-dev \ + libz-dev + +RUN rtx install python \ No newline at end of file diff --git a/test/fixtures/issue1.dockerfile b/test/fixtures/issue1.dockerfile new file mode 100644 index 0000000..c268a90 --- /dev/null +++ b/test/fixtures/issue1.dockerfile @@ -0,0 +1,39 @@ +# syntax=devthefuture/dockerfile-x +# See https://codeberg.org/devthefuture/dockerfile-x. + +ARG JAVA_VERSION=17 + +# Even if no JDK is required for rtx, use an Ubuntu base image that incudes the JDK and which is also used by other +# stages in order to reduce the total number of base images used. +FROM eclipse-temurin:$JAVA_VERSION-jdk-jammy AS build + +# See https://docs.docker.com/build/cache/#use-the-dedicated-run-cache +RUN --mount=type=cache,target=/var/cache/apt \ + # Install tools required to install rtx. + apt-get update && \ + apt-get install -y --no-install-recommends \ + ca-certificates \ + curl \ + gnupg \ + && \ + # Install rtx (see https://github.com/jdx/rtx#installation). + install -dm 755 /etc/apt/keyrings && \ + curl https://rtx.jdx.dev/gpg-key.pub | gpg --dearmor > /etc/apt/keyrings/rtx-archive-keyring.gpg && \ + echo "deb [signed-by=/etc/apt/keyrings/rtx-archive-keyring.gpg arch=amd64] https://rtx.jdx.dev/deb stable main" > /etc/apt/sources.list.d/rtx.list && \ + apt-get update && \ + apt-get install -y --no-install-recommends rtx + +RUN --mount=type=cache,target=/var/cache/apt \ + # Install tools required to run rtx. + apt-get update && \ + apt-get install -y --no-install-recommends \ + build-essential \ + git + +# Ease debugging of rtx errors in derived images. +ENV RTX_VERBOSE=1 + +# Make the tool versions available to derived images. +COPY .tool-versions . + +INCLUDE inc/python.dockerfile \ No newline at end of file