WIP: Multiline shell - #116
Open
m-ildefons wants to merge 1 commit into
Open
Conversation
This is experimental. Parse shell commands into a list of lines,
separated at escaped line breaks, which can be re-assembled to a shell
script.
This would later allow to re-construct the correct line within the
Dockerfile where an error happens, because the line in the shell script
would indicate the correct position relative to the first escaped line
break in the Dockerfile.
E.g.:
```dockerfile
RUN foo \ # line 13
bar && [ -e error] # line 14
```
would parse into
Run (RunArgs (ArgumentsLines ["foo", " bar && [ -e error]"]) RunFlags ...)
form this the shell script could be reconstructed as:
```shell
foo \ # line 1
bar && [ -e error] # line 2
```
With this script, the commands can be checked e.g. with Shellcheck,
which will find an error on line 2.
This error can now be correctly associated with the line in the
dockerfile, beause the output of Shellcheck would contain the
line-number relative to the RUN instruction.
In this case, the error line would be calculated as
$line_of_RUN_instruction + $relative_line_in_shell - 1, i.e.:
13 + 2 - 1 = 14
This would yield the correct line where the error happens.
Signed-off-by: Moritz Röhrich <moritz@ildefons.de>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What problem does this PR address
Improve how line-breaks are represented in the AST to allow Hadolint to correctly calculate the position of Shellcheck-warnings.
related-to: hadolint/hadolint#891
related-to: hadolint/hadolint#285
related-to: hadolint/hadolint#251
How is this PR supposed to work
Parse shell commands into a list of lines, separated at escaped line breaks, which can be re-assembled to a shell script.
This would later allow to re-construct the correct line within the Dockerfile where an error happens, because the line in the shell script would indicate the correct position relative to the line of the instruction in the Dockerfile.
E.g.:
would parse into
form this the shell script could be reconstructed as:
With this script, the commands can be checked e.g. with Shellcheck, which will find an error on line 2.
This error can now be correctly associated with the line in the dockerfile, beause the output of Shellcheck would contain the line-number relative to the RUN instruction.
In this case, the error line would be calculated as
$line_of_RUN_instruction + $relative_line_in_shell - 1, i.e.:
13 + 2 - 1 = 14
This would yield the correct line where the error happens.
How to test this PR
There are unit-tests included to make sure this change produces the expected AST. However to reap the benefits of this change, there are also some changes required in Hadolint as well.
To test that, a custom build of Hadolint with those changes must be made:
hadolintandlanguage-dockerrepositoriesE.g. This Dockerfile should show Shellcheck warnings on all lines with
$warningNormal Hadolint - Warnings only in the line where the Docker instruction is rooted
Custom Hadolint build with this change - Warnings at the line where the problem is