Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(build): drop Dockerfile comments before joining lines in the gate

Yudhi Armyndharise0dfd844dbec
diff --git a/scripts/check-dockerignore.mjs b/scripts/check-dockerignore.mjs
index a5b9cb851..162211171 100644
--- a/scripts/check-dockerignore.mjs
+++ b/scripts/check-dockerignore.mjs
@@ -170,7 +170,13 @@ const errors = [];
// instead of restating them: a hand-written list lost eight of the patchers, and a rule dropping one of
// them then broke the image build rather than this check. A copy from another stage, a flag, the
// destination, a remote ADD source and `.` (the whole context) are not inputs.
-const dockerfile = readFileSync(new URL('Dockerfile', root), 'utf8').replace(/\\\n/g, ' ');
+// Comment lines go first, as in BuildKit: a comment is never continued, so one that ends in a backslash must
+// not swallow the instruction below it, and one inside a continued instruction drops out of it.
+const dockerfile = readFileSync(new URL('Dockerfile', root), 'utf8')
+ .split('\n')
+ .filter(line => !/^\s*#/.test(line))
+ .join('\n')
+ .replace(/\\\n/g, ' ');
const wildcard = /[*?[]/;
function copyInputs(source) {
if (/^[a-z][a-z0-9+.-]*:\/\/|^git@/i.test(source)) return [];
diff --git a/scripts/check-dockerignore.spec.mjs b/scripts/check-dockerignore.spec.mjs
index 3e2ce4ebb..d90e306b0 100644
--- a/scripts/check-dockerignore.spec.mjs
+++ b/scripts/check-dockerignore.spec.mjs
@@ -103,6 +103,29 @@ test('a rule spelled ./data/ keeps data/ out of the context as data/ does', () =
assert.equal(run.status, 0, run.out);
});
+test('an instruction continued over several lines is read whole', () => {
+ const run = runGate({
+ Dockerfile: 'COPY control.txt \\\n scripts/new.js \\\n ./scripts/\n',
+ '.dockerignore': `${REAL_DOCKERIGNORE}\nscripts/new.js\n`,
+ });
+ assert.equal(run.status, 1, run.out);
+ assert.match(run.out, /rejects 'scripts\/new\.js'/);
+});
+
+test('a comment ends with its line, whatever it ends in, and is not part of a continued instruction', () => {
+ // BuildKit does not continue a comment, so the COPY below one that ends in a backslash is an instruction
+ // of its own, and a comment between the lines of a continued instruction is dropped.
+ const dockerfiles = [
+ 'COPY control.txt ./\n# a comment ending in a backslash \\\nCOPY scripts/new.js ./scripts/\n',
+ 'COPY control.txt \\\n # a comment inside the instruction\n scripts/new.js ./scripts/\n',
+ ];
+ for (const dockerfile of dockerfiles) {
+ const run = runGate({ Dockerfile: dockerfile, '.dockerignore': `${REAL_DOCKERIGNORE}\nscripts/new.js\n` });
+ assert.equal(run.status, 1, `${JSON.stringify(dockerfile)}: ${run.out}`);
+ assert.match(run.out, /rejects 'scripts\/new\.js'/, `${JSON.stringify(dockerfile)}: ${run.out}`);
+ }
+});
+
test('an indented or lower-case instruction is read like any other', () => {
for (const line of [
' COPY ./scripts/new.js ./scripts/',