Skip to content

Implement a YAML linter hook #358

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

Merged
merged 29 commits into from
Oct 18, 2021
Merged

Conversation

ericLemanissier
Copy link
Contributor

@ericLemanissier ericLemanissier commented Oct 6, 2021

fixes #308

@ericLemanissier ericLemanissier force-pushed the yamllinter branch 2 times, most recently from db44a1f to dec9d6d Compare October 6, 2021 21:33
@ericLemanissier ericLemanissier marked this pull request as ready for review October 6, 2021 22:37
@ericLemanissier
Copy link
Contributor Author

@danimtb
Copy link
Member

danimtb commented Oct 7, 2021

the linter can be very useful. The main downside is that we don't have a way to install external dependencies for hooks...

Anyway, if this is going to check the conandata.yml only I'd probably change the name to explicit this or make the hook more generic to check all YAML files exported alongside the recipe.

Another question: Should this be part of the conan-center hook or better keep it as a different hook for wider use rather than ConanCenter only?

@ericLemanissier
Copy link
Contributor Author

the linter can be very useful. The main downside is that we don't have a way to install external dependencies for hooks...

I just added some code that made it explicit what has to be done if yamllint is not installed.

Another question: Should this be part of the conan-center hook or better keep it as a different hook for wider use rather than ConanCenter only?

As it has an external requirement, I think we cannot include it in conan-center hooks. That said we can still run the hook in Conan-center-index CI, as demonstrated in ericLemanissier/cocorepo#5

Anyway, if this is going to check the conandata.yml only I'd probably change the name to explicit this or make the hook more generic to check all YAML files exported alongside the recipe.

ok, I guess that would make the check a post_export instead of pre_export

@danimtb danimtb changed the title imlement yaml linter hook Implement a YAML linter hook Oct 7, 2021
@ericLemanissier
Copy link
Contributor Author

@danimtb it's ready for review

@uilianries
Copy link
Member

@ericLemanissier Is yaml lint recursive? If yes, I would suggest looking one level above to check config.yml too

@ericLemanissier
Copy link
Contributor Author

ericLemanissier commented Oct 8, 2021

How do we know If it is cci's structure ? We could end up scanning the full disk.

EDIT: I'll simply scan ../config.yml if it exists

@uilianries
Copy link
Member

EDIT: I'll simply scan ../config.yml if it exists

Smart!

@uilianries
Copy link
Member

Well, it will be funny once merged. Not in Conan Center by default, but still has a big potential:

[HOOK - yaml_linter.py] pre_export(): Lint yaml '/home/uilian/Development/conan/conan-center-index/recipes/zlib/1.2.11/conanfile.py'
[HOOK - yaml_linter.py] pre_export(): /home/uilian/Development/conan/conan-center-index/recipes/zlib/1.2.11/conandata.yml:4:10: [warning] wrong indentation: expected 6 but found 9 (indentation)
[HOOK - yaml_linter.py] pre_export(): /home/uilian/Development/conan/conan-center-index/recipes/zlib/1.2.11/conandata.yml:5:10: [warning] wrong indentation: expected 6 but found 9 (indentation)
[HOOK - yaml_linter.py] pre_export(): /home/uilian/Development/conan/conan-center-index/recipes/zlib/1.2.11/conandata.yml:6:8: [warning] wrong indentation: expected 4 but found 7 (indentation)
[HOOK - yaml_linter.py] pre_export(): YAML Linter detected '0' errors

Co-authored-by: Uilian Ries <[email protected]>
@ericLemanissier
Copy link
Contributor Author

These are warnings which can be disabled if you prefer. That said, this hook is not part of conan-center hooks, so it will never be reported by c3i. This is why I intend to make it run in a dedicated github action,: nobody will look at the log, except if the status is failed.

uilianries
uilianries previously approved these changes Oct 11, 2021
Copy link
Contributor

@jgsogo jgsogo left a comment

Choose a reason for hiding this comment

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

We can add more hooks other that conan-center to CCI (in fact I think some of those check should be extracted from conan-center into their own hook). And we will, probably this will be one of them.

Besides that, I have some concerns with this idiom (not first time here):

try:
    import yamllint
except ImportError as e:
    sys.stderr.write("Install yamllint to use 'yaml_linter' hook: 'pip install yamllint'")
    sys.exit(1)

I think Conan client will exit with the sys.exit, it is unconditional, no exception to catch. I would prefer just an error: "Hook XXX cannot be loaded because of ...". I'm not sure if this is something already working if we let the ImportError to propagate, or it's better right now to capture the exception and noop if the hook cannot run:

try:
   import yamllint
   
   def pre_export(...):
      ...
      
except ImportError as e:
    sys.stderr.write("...")

Maybe, for some other plugins it makes sense to raise and stop Conan execution, but for most of them an error while loading the hook should be enough. wdyt?

@ericLemanissier
Copy link
Contributor Author

I agree !

jgsogo
jgsogo previously approved these changes Oct 14, 2021
Copy link
Contributor

@jgsogo jgsogo left a comment

Choose a reason for hiding this comment

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

Feel free to add an entry to README.md

Copy link
Member

@uilianries uilianries left a comment

Choose a reason for hiding this comment

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

Great job, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

yaml linter
4 participants