Skip to content

Commit

Permalink
Fix #106 Allow diagnostics to be ignored with a comment
Browse files Browse the repository at this point in the history
Most diagnostics can now be ignored with by embedding a comment right
before the source line of the error.

Signed-off-by: Remy Suen <[email protected]>
  • Loading branch information
rcjsuen committed Sep 9, 2023
1 parent dc5a2be commit 36e9102
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]
### Added
- support parsing the new `--start-interval` flag for HEALTHCHECK instructions ([#115](https://github.com/rcjsuen/dockerfile-utils/issues/115))
- allow some diagnostics to be ignored if a `# dockerfile-utils: ignore` comment precedes the originating line of the error ([#106](https://github.com/rcjsuen/dockerfile-utils/issues/106))

## [0.11.0] - 2022-08-23
### Added
Expand Down
31 changes: 31 additions & 0 deletions VALIDATOR.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,37 @@ The output on the command line will not include any whitespaces.
```

## Supported Validation Checks
If the validator is flagging something you want it to ignore, you can put a `# dockerfile-utils: ignore` comment right before the originating line of the error. Note that not all errors can be ignored this way. Fatal errors (missing `FROM` instruction for example) or errors related to parser directives (since having a comment would make something _not_ a parser directive anymore) cannot be ignored.

```Dockerfile
FROM alpine
# dockerfile-utils: ignore
UNRECOGNIZED argument
```

The originating line is generally the instruction itself. So if you have a multi-line instruction you must put it before the instruction instead of before the error the line is on.

**Correct:**
```Dockerfile
FROM alpine
# dockerfile-utils: ignore
HEALTHCHECK \
--interval=30s \
--typo=example \
CMD [ "executable" ]
```
**Incorrect:**
```Dockerfile
FROM alpine
HEALTHCHECK \
--interval=30s \
# dockerfile-utils: ignore
--typo=example \
CMD [ "executable" ]
```

If you feel an error cannot be ignored but you feel it is a non-fatal error, please let us know by opening an issue.


### General
#### Instructions
Expand Down
25 changes: 22 additions & 3 deletions src/dockerValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class Validator {

validate(document: TextDocument): Diagnostic[] {
this.document = document;
let problems: Diagnostic[] = [];
let problems: DockerfileDiagnostic[] = [];
let dockerfile = DockerfileParser.parse(document.getText());
this.checkDirectives(dockerfile, problems);
let instructions = dockerfile.getInstructions();
Expand Down Expand Up @@ -281,6 +281,25 @@ export class Validator {
for (let instruction of dockerfile.getOnbuildTriggers()) {
this.validateInstruction(document, escapeChar, instruction, instruction.getKeyword(), true, problems);
}

const ignoredLines = [];
for (const comment of dockerfile.getComments()) {
if (comment.getContent() === "dockerfile-utils: ignore") {
ignoredLines.push(comment.getRange().start.line);
}
}

problemsCheck: for (let i = 0; i < problems.length; i++) {
if (problems[i].instructionLine !== null) {
for (const ignoredLine of ignoredLines) {
if (ignoredLine + 1 === problems[i].instructionLine) {
problems.splice(i, 1);
i--;
continue problemsCheck;
}
}
}
}
return problems;
}

Expand Down Expand Up @@ -1663,7 +1682,7 @@ export class Validator {
return Validator.createError(instructionLine, start, end, Validator.getDiagnosticMessage_InstructionRequiresOneOrThreeArguments(), ValidationCode.ARGUMENT_REQUIRES_ONE_OR_THREE);
}

private static createNoSourceImage(start: Position, end: Position): Diagnostic {
private static createNoSourceImage(start: Position, end: Position): DockerfileDiagnostic {
return Validator.createError(null, start, end, Validator.getDiagnosticMessage_NoSourceImage(), ValidationCode.NO_SOURCE_IMAGE);
}

Expand Down Expand Up @@ -1691,7 +1710,7 @@ export class Validator {
return Validator.createError(instructionLine, start, end, Validator.getDiagnosticMessage_InstructionUnknown(instruction), ValidationCode.UNKNOWN_INSTRUCTION);
}

private static createError(instructionLine: uinteger | null, start: Position, end: Position, description: string, code?: ValidationCode, tags?: DiagnosticTag[]): Diagnostic {
private static createError(instructionLine: uinteger | null, start: Position, end: Position, description: string, code?: ValidationCode, tags?: DiagnosticTag[]): DockerfileDiagnostic {
return Validator.createDiagnostic(DiagnosticSeverity.Error, instructionLine, start, end, description, code, tags);
}

Expand Down
12 changes: 12 additions & 0 deletions test/dockerValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4563,4 +4563,16 @@ describe("Docker Validator Tests", function() {
testEscape("WORKDIR", "/or", "ion");
});
});

describe("embedded ignore rules", () => {
it("can ignore following diagnostic", () => {
const diagnostics = validateDockerfile("FROM alpine\n# dockerfile-utils: ignore\nUNRECOGNIZED argument");
assert.strictEqual(diagnostics.length, 0);
});

it("can ignore multiple diagnostics", () => {
const diagnostics = validateDockerfile("FROM alpine\n# dockerfile-utils: ignore\nUNRECOGNIZED argument\n# dockerfile-utils: ignore\nUNRECOGNIZED argument");
assert.strictEqual(diagnostics.length, 0);
});
});
});

0 comments on commit 36e9102

Please sign in to comment.