ParseException: The YAML value does not appear to be valid UTF-8

Sometimes Symfony throws up the most peculiar of error messages.

I had a situation where I had two config (.yml) files, one environment overriding the other.

Now, what was particularly confusing about this situation was that I had copied and pasted the config (not very DRY) from the first file to the second.

This wasn’t a new project – both files had been around for a long time, but it was the first time I was using the second file properly.

The idea in this instance was the second config file would inherit all the first config files settings, but over rule the ones I had changed.

As soon as I did this, however, I got the following:

symfony yaml parsing error

code error text for Google’s benefit:

Whoops, looks like something went wrong.

1/1ContextErrorException: Warning: htmlspecialchars(): Invalid multibyte sequence in argument in /mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Debug/ExceptionHandler.php line 302
in /mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Debug/ExceptionHandler.php line 302
at ErrorHandler->handle('2', 'htmlspecialchars(): Invalid multibyte sequence in argument', '/mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Debug/ExceptionHandler.php', '302', array('args' => array(array('string', '')), 'result' => array(), 'key' => '0', 'item' => array('string', '')))
at htmlspecialchars('', '11', 'UTF-8') in /mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Debug/ExceptionHandler.php line 302
at ExceptionHandler->formatArgs(array(array('string', ''))) in /mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Debug/ExceptionHandler.php line 158
at ExceptionHandler->getContent(object(FlattenException)) in /mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Debug/ExceptionHandler.php line 117
at ExceptionHandler->createResponse(object(ParseException)) in /mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Debug/ExceptionHandler.php line 74
at ExceptionHandler->handle(object(ParseException))
Exception thrown when handling an exception (Symfony\Component\Debug\Exception\FlattenException: The YAML value does not appear to be valid UTF-8.)

1/1ParseException: The YAML value does not appear to be valid UTF-8.
in /mnt/project/my-project/vendor/symfony/symfony/src/Symfony/Component/Yaml/Parser.php line 59

A classic Symfony blow up.

Now, whilst this may be a Symfony project, it is not a Symfony problem!

The problem is the encoding of the file itself. This sounds like it might be a total pain to fix, but if you are using PHPStorm it’s actually very, very easy.

How to Fix ParseException: The YAML value does not appear to be valid UTF-8

Open the file in PHPStorm.

Take a look in the bottom right corner:


Notice – I am using Windows here, so the default file encoding type is what caught me out.

Click the text ‘windows-1252’ or whatever you have here that is not UTF-8.

Select UTF-8.

You may get a little pop up warning you that your file will be converted to UTF-8.

Take a back up if you aren’t sure, but otherwise, convert it.

Upload the file (or do nothing if using shared mounts in Vagrant), and success, we have captured the enemies intelligence.

Published by

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

2 thoughts on “ParseException: The YAML value does not appear to be valid UTF-8”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.