Prettier for YAML - SyntaxError: Nested mappings are not allowed in compact mappings

8.3k Views Asked by At

In my project there is a YAML file for NewRelic newrelic.yml that has a line with Ruby code for setting app_name. The existing config is working, and I need to edit the file, but when I try to do a yarn commit it fail with the following error, that to me looks like a Prettier error.

I have tried to add #prettier-ignore before the line, but it still causes the commit to fail.

node_modules/prettier/index.js:13588
      throw error;
      ^

SyntaxError: Nested mappings are not allowed in compact mappings (18:13)
  17 |   # Relic.  For more details, see https://docs.newrelic.com/docs/apm/new-relic-apm/maintenance/renaming-applications
> 18 |   app_name: <%= ENV["SERVER_ENV"] == 'staging' ? 'MyApp (Staging)' : 'MyApp' %>
     |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

My code editor is VSCode incase that is relevant.

So I'd like to either figure out why the # prettier-ignore is not working as the prettier docs indicate it should. Or figure out how to get things formatted to pass, which is the preferred option obviously.

1

There are 1 best solutions below

1
On

Seems like Prettier doesn't handle ERB in YAML files.

I would add all common configurations into one block and would then re-use that block with YAML aliases.

common: &default_settings
  license_key: 1234
  log_lever: info
  # ...

production:
  <<: *default_settings
  app_name: MyApp

staging:
  <<: *default_settings
  app_name: MyApp (Staging)