Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better yaml parsing #1619

Merged
merged 4 commits into from May 16, 2024
Merged

better yaml parsing #1619

merged 4 commits into from May 16, 2024

Conversation

pd93
Copy link
Member

@pd93 pd93 commented Apr 25, 2024

This PR greatly improves the error reporting of YAML issues by surfacing the real parsing errors from yaml.v3 and providing a syntax highlighted snippet of the error and its location. It also provides a clickable link to the error location (if using a supported editor terminal like VSCode).

Syntax highlighting is provided through chroma and I have added a custom theme that looks good for terminals that only support 8 colours.

Some examples:

image

image

@andreynering andreynering marked this pull request as draft May 9, 2024 00:25
@pd93 pd93 marked this pull request as ready for review May 10, 2024 19:21
@pd93 pd93 requested a review from andreynering May 10, 2024 19:21
Copy link
Collaborator

@vmaerten vmaerten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! It's way more clear now 🎉

cmd/task/task.go Outdated Show resolved Hide resolved
@vmaerten
Copy link
Collaborator

(Nothing to see with this PR, but what is your taskd command ? 😄 )

@pd93
Copy link
Member Author

pd93 commented May 11, 2024

what is your taskd command ? 😄

Hey @vmaerten, it's just a function I have defined in my dotfiles that helps me out with development:

taskd () {
        pushd "$DEV/github.com/go-task/task" &> /dev/null
        go build -o "$HOME/bin/taskd" ./cmd/task
        res=$? 
        popd &> /dev/null
        if [ $res -ne 0 ]
        then
                echo "\x1b[31mFailed to build taskd\x1b[0m"
                return 1
        fi
        "$HOME/bin/taskd" "$@"
}

Essentially, it will build the task binary, place it in my ~/bin and then (if it succeeds) call it with any arguments I gave. It allows me to easily test checked out code without having to manually install development versions of task every time I make a change. Not doing this can get quite annoying as I regularly use a release version of task for work.

Sidenote: You might think "why not just use go run ... instead?". I used to do this, but the go binary obscures things like exit codes and can actually change behaviour in some subtle ways. It's best to test with a properly built binary.

Copy link
Member

@andreynering andreynering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the idea, this is a great QoL improvement!

I noticed that actual syntax errors still show the usual way. That's a possible improvement to be done in the future:

image

@andreynering andreynering added the area: parser Changes related to parsing Taskfiles. label May 16, 2024
@andreynering
Copy link
Member

I rebased to fix a conflict.

@andreynering andreynering enabled auto-merge (squash) May 16, 2024 01:21
@andreynering andreynering merged commit 8d138a5 into main May 16, 2024
13 checks passed
@andreynering andreynering deleted the better-yaml-parsing branch May 16, 2024 01:24
andreynering added a commit that referenced this pull request May 16, 2024
@pd93
Copy link
Member Author

pd93 commented May 16, 2024

I noticed that actual syntax errors still show the usual way. That's a possible improvement to be done in the future:

@andreynering I did look into this while doing this PR, but those errors are created by the YAML lexer rather than the parser. This makes it a bit more difficult to intercept the errors as we don't have access to the yaml.Node when the error first hits our code. However, maybe there is another way. Worth taking a look in the future.

This alternative library also exists which provides this kind of error handling out-of-the-box. However, it does not implement the same interfaces and would mean redoing all our unmarshalers which is a bit annoying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: parser Changes related to parsing Taskfiles.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants