Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/spec-node/dockerfileUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ export function ensureDockerfileHasFinalStageName(dockerfile: string, defaultLas

// Find the last line that starts with "FROM" (possibly preceeded by white-space)
const fromLines = [...dockerfile.matchAll(findFromLines)];
if (fromLines.length === 0) {
throw new Error('Error parsing Dockerfile: Dockerfile contains no FROM instructions');
}

const lastFromLineMatch = fromLines[fromLines.length - 1];
const lastFromLine = lastFromLineMatch.groups?.line as string;

Expand Down
12 changes: 11 additions & 1 deletion src/test/dockerfileUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert } from 'chai';
import { assert, expect } from 'chai';
import { imageMetadataLabel, internalGetImageBuildInfoFromDockerfile } from '../spec-node/imageMetadata';
import { ensureDockerfileHasFinalStageName, extractDockerfile, findBaseImage, findUserStatement, supportsBuildContexts } from '../spec-node/dockerfileUtils';
import { ImageDetails } from '../spec-shutdown/dockerUtils';
Expand Down Expand Up @@ -143,6 +143,16 @@ RUN another command
});
});
});

describe('without any from stage (invalid Dockerfile)', () => {
it('should throw a descriptive error', () => {
const dockerfile = `
RUN some command
`;
expect(() => ensureDockerfileHasFinalStageName(dockerfile, 'placeholder')).to.throw('Error parsing Dockerfile: Dockerfile contains no FROM instructions');
});
});

});

describe('getImageBuildInfo', () => {
Expand Down