Problem/Motivation
Since Symfony's YAML parser has some issues https://github.com/symfony/symfony/issues/16234 that allow for invalid yaml to be parsed and in an attempt to assuage some fears in #1920902: Add a Drupal Yaml wrapper so we can default to PECL Yaml component if it is available.
We should add a YAML linting pass to core and contrib so we can ensure YAML is able to be parsed correctly in all our implementations and is able to be parsed by external tools.
Steps to reproduce
Proposed resolution
Add eslint-plugin-yml to our precommit script to ensure YAML is always well formed.
Running core/scripts/dev/commit-code-check.sh
from the top level of the checkout will check any changed YAML files.
Running yarn lint:yaml
from the core directory will check all YAML files in core.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
How to test the new eslint for yaml files
From the core directory do:
node ./node_modules/eslint/bin/eslint.js --ext .yml .
or run
yarn lint:yaml
Comment | File | Size | Author |
---|
Issue fork drupal-2591827
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
stevectorComment #3
elachlan CreditAttribution: elachlan commentedComment #4
elachlan CreditAttribution: elachlan commentedComment #5
elachlan CreditAttribution: elachlan commentedComment #6
elachlan CreditAttribution: elachlan commentedComment #7
elachlan CreditAttribution: elachlan commentedComment #8
elachlan CreditAttribution: elachlan commentedComment #9
elachlan CreditAttribution: elachlan commentedComment #10
elachlan CreditAttribution: elachlan commentedI suggest yamlJS because it doesn't have a dependency on grunt, like grunt-yamllint does.
Comment #11
elachlan CreditAttribution: elachlan commentedsorry, double post.
Comment #12
elachlan CreditAttribution: elachlan commentedComment #13
YesCT CreditAttribution: YesCT commentedComment #14
Mile23Addressing blockers for this issue: #2654650: [meta] Jobs for linting and coding standards
Comment #15
Mile23The symfony issue linked above seems to have been fixed with deprecation for symfony 2.8: https://github.com/symfony/symfony/pull/16285
Drupal 8's symfony dependency is 2.7.*, so this is an argument in favor of bumping up to 2.8.* #2611816: Update to symfony 2.8
We should still do YAML linting/CS here, though.
Comment #16
MixologicComment #17
elachlan CreditAttribution: elachlan commented@Mixologic - This is the final code style check I believe.
Comment #18
elachlan CreditAttribution: elachlan commentedAdjust title.
Comment #19
MixologicWe can't really do much here until core has decided what the standard is and what tools we should use to enforce that standard. Our maybe wet know that already?
Comment #20
andypostPECL extension yml sometimes wrong as well
Comment #21
andypostComment #22
elachlan CreditAttribution: elachlan commentedDo we already test if the YAML is parsable?
Comment #23
quietone CreditAttribution: quietone at Acro Commerce commentedThe symfony issue quoted in the IS has been fixed.
Comment #24
catchThis can be added directly in core now without requiring changes to DrupalCI.
Comment #25
Wim Leers#24: What changed?
Comment #26
neclimdulCurious as well. Happy to make a patch but not sure what needs to be done to make it happen.
Comment #27
catchSorry that would have been useful information to add - since #3178845: Run same checks as committers do on DrupalCI.
Comment #28
longwaveQuick stab at this using https://www.npmjs.com/package/yaml-lint
PHP CodeSniffer claims to check YAML files but it doesn't seem to do a very good job, maybe we should remove it from the equation if we have something better now.
Comment #29
neclimdulDid a quick check and edited a file to reproduce the common problem we where running into and it caught it so this works for me.
Specifically this would cause the problem where Symfony would use the later value for the key and Pecl would use the first and we'd end up with brokenness. I expect the core team or maybe tests have been really great at catching these because I haven't seen them in a while but this looks like a great lint for catching errors with yaml.
Going to RTBC because the approach makes sense to me and correctly linted problems.
Comment #30
catchThis looks good.
It won't help with issues like #3112452: Fix indentation consistency in core's yaml files. though where the YAML is valid but confusing - so I think we should open a follow-up to see if we can add formatting checks as well as the raw linting.
Comment #31
longwaveI wonder if it's worth exploring eslint-plugin-yml as an alternative as that seems to be able to handle coding style as well. We already use a number of eslint plugins.
https://www.npmjs.com/package/eslint-plugin-yml
Comment #32
catchBack to NR for that. If we can combine linting and coding style in eslint that'd be worth doing I think.
Comment #33
longwaveThis looks like it might be a winner. Running eslint-plugin-yml with the strictest set of options across core reports many errors, mostly because we do not use consistent quoting. But we can switch all the rules off (including Prettier, which implements some YAML rules) and start with the "recommended" set, and get 9 errors:
As an example the two errors in core.services.yml are where we have a "tags" key but nothing inside it:
eslint-plugin-yml also only brings with it a single child dependency as we already have the rest of ESLint installed.
Comment #34
longwaveThe attached patch builds on #33:
This only leaves system.authorize.yml, which as far as I can see is no longer used in core, apart from a migration that writes to it? Probably worth ignoring this here and exploring in a followup issue?
Comment #35
longwaveAdded a patch in #2715145: Remove system.authorize config to deal with system.authorize.yml.
Comment #36
neclimdulSo figured I'd put this through the paces locally and see what happened. Everything is pretty straight forward and seemed to work but I was a little bit interested in how
node ./node_modules/eslint/bin/eslint.js "$TOP_LEVEL/$FILE"
would behave outside the core directory. So I modifiedsites/development.services.yml
and got this weird error.Not sure how its connected or why it would only seem to affect files outside of core. Especially since eslint-plugin-prettier is clearly installed but thought I'd provide the feedback.
Also... it passed even though the file was not valid so marking NW. Passing may actually just be a weird byproduct of the error and how node chose to return from the error though.
Comment #37
longwaveAddressed #36 with an additional option when running eslint. Also added --quiet to match the other calls to eslint in
commit-code-check.sh
.Comment #38
neclimdulEverything looks great! The real interdiff attached :-D So easy to mess that up when you're making a quick change.
Side note: I accidentally had a big folder of config data sitting in the directory when I ran it this time and it did expose something interesting. With a lot of yaml changes this _really_ slows down the script. Maybe not a deal breaker but wanted to share.
Comment #39
longwaveWhoops, thanks for fixing that. Another reason I should embrace the Gitlab workflow for all issues!
Comment #40
longwaveI also think we should drop YAML linting from PHPCS if we have a better tool doing it, PHPCS isn't doing a very good job anyway - let's open a followup for that if this is committed.
Comment #41
alexpottWe have a couple of new (i guess) errors to fix.
Comment #43
longwaveWhile working on this I found a major bug in the recent change to user.mail.yml: #3212034: Account emails are missing newlines due to malformed YAML
Comment #44
longwaveuser.mail.yml was fixed in the linked issue.
system.authorize.yml will be deleted in #2715145: Remove system.authorize config but we can fix the syntax here temporarily.
Rerolled #37 and fixed the above.
Comment #45
longwaveNeeds rerolling.
Comment #47
longwaveRerolled as a merge request.
Comment #48
daffie CreditAttribution: daffie commentedCould we update the IS with:
I think we also should add a CR for the added test functionality.
Comment #49
longwaveUpdated IS.
Comment #50
longwaveAdded CR: https://www.drupal.org/node/3226497
Comment #51
quietone CreditAttribution: quietone commentedThe doc page should be updated as well, I think. https://www.drupal.org/docs/develop/development-tools/running-core-devel...
When I read the CR, which is fine, I thought it would be helpful to link to the doc page, so I added it. Took it straight from https://www.drupal.org/node/3185278.
Comment #52
daffie CreditAttribution: daffie commentedUpdated the IS with how to run eslint for yaml files.
Comment #53
daffie CreditAttribution: daffie commentedI found out that after installing the MR and running the command from the IS that the changes for core/core.services.yml are needed to pass the new eslint for yaml files.
I think we need a followup for when there is a change to core/package.json that all files are tested to see if they pass. Just like we did for PHPCS in #3224583: The testbot does not run PHPCS on all files when core/phpcs.xml.dist is changed.
Changing the status to needs work for the unresolved threads.
Comment #54
daffie CreditAttribution: daffie commentedComment #55
longwaveUpgraded eslint-plugin-yml, responded to the other comments.
Comment #56
daffie CreditAttribution: daffie commentedJust one open unresolved thread.
Comment #57
longwaveNeed to check here that drupalci actually runs as expected - the default validate_codebase steps should be skipped and nightwatchjs should run as normal.
Comment #58
longwaveThe validate_codebase step is missing entirely from the Jenkins output (as it does nothing, this is fine) and the Nightwatch tests ran as expected so to me this is an OK change to make. We probably should follow our own best practices and validate all our YAML except when we are explicitly testing invalid YAML.
Comment #59
daffie CreditAttribution: daffie commentedThe new yarn package for eslint-plugin-yml has been added in the MR.
The file core/scripts/dev/commit-code-check.sh has been updated for the package.
When I run the command to test for failures with the new plugin, I get a number of vialures which are fixed in the MR (core/drupalci.yml and core/core.services.yml).
The IS is in order and there is an CR.
After the MR has landed the page https://www.drupal.org/drupalorg/docs/drupal-ci/customizing-drupalci-tes... needs updating.
For me it is RTBC.
Comment #61
catchCommitted b85fb24 and pushed to 9.3.x. Thanks!
Comment #62
Wim LeersPublished change record. Noticed this in an unexpectedly broken contrib build…