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

Prettier is removing the doctype from handlebar files #11223

Open
paulsmithkc opened this issue Jul 18, 2021 · 12 comments
Open

Prettier is removing the doctype from handlebar files #11223

paulsmithkc opened this issue Jul 18, 2021 · 12 comments
Labels
lang:handlebars Issues affecting Handlebars (Glimmer) scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency type:bug Issues identifying ugly output, or a defect in the program

Comments

@paulsmithkc
Copy link

paulsmithkc commented Jul 18, 2021

Environments:

  • Prettier Version: 2.3.2
  • Running Prettier via: vscode extension
  • Runtime: Node.js v14.16.0
  • Operating System: Windows
  • Prettier plugins (if any): none

Steps to reproduce:

Original code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
</html>

Formatted code:

<html lang='en'>
  <head>
    <meta charset='UTF-8' />
    <meta http-equiv='X-UA-Compatible' content='IE=edge' />
    <meta name='viewport' content='width=device-width, initial-scale=1.0' />
    <title>Document</title>
  </head>
  <body>

  </body>
</html>

Expected behavior:

The <!DOCTYPE html> should be preserved unchanged.

Actual behavior:

The <!DOCTYPE html> is removed.

@sosukesuzuki
Copy link
Member

I cannot reproduce this in playground. Can you reproduce in playground?

Prettier 2.3.2
Playground link

--parser html

Input:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  
</body>
</html>

Output:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
</html>

@sosukesuzuki sosukesuzuki added lang:html Issues affecting HTML (and SVG but not JSX) status:awaiting response Issues that require answers to questions from maintainers before action can be taken labels Jul 18, 2021
@no-response
Copy link

no-response bot commented Aug 1, 2021

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@no-response no-response bot closed this as completed Aug 1, 2021
@paulsmithkc
Copy link
Author

Sorry. This happened when formatting a .handlebars file. The file was located at /views/layouts/main.handlebars.

On further testing, it seems to be a problem .handlebars files, and not .html files. Still a critical bug.

@no-response no-response bot removed the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Aug 4, 2021
@no-response no-response bot reopened this Aug 4, 2021
@fisker
Copy link
Member

fisker commented Aug 4, 2021

Thanks!

Prettier 2.3.2
Playground link

--parser glimmer

Input:

<!DOCTYPE html>
<html lang="en">
</html>

Output:

<html lang="en">
</html>

It seems a known issue in parser. glimmerjs/glimmer-vm#870

@fisker fisker added lang:handlebars Issues affecting Handlebars (Glimmer) scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency type:bug Issues identifying ugly output, or a defect in the program and removed lang:html Issues affecting HTML (and SVG but not JSX) labels Aug 4, 2021
louh added a commit to streetmix/streetmix that referenced this issue Aug 21, 2021
…from commit hook

There is a huge bug with the `glimmer` library (which prettier relies on for parsing)
where the <!DOCTYPE html> tag will get dropped improperly when formatting. I'm replacing
the tags and keeping the formatted files here but removing the auto-format settings
from commit hooks.

See more: prettier/prettier#11223
louh added a commit to streetmix/streetmix that referenced this issue Aug 21, 2021
* chore(package): update husky@7

* chore(actions): optimize update-translations install

* chore(package): bump commitlint@13.1.0

* chore: remove extraneous npm messages

* chore(package): update lint-staged@11.1.2

* chore(package): bump prettier@2.3.2

* chore(templates): clean up with prettier

* chore(package): bump stylelint deps

* fix(view): plausible domain var

* fix(prettier): keep formatted hbs for now, but remove hbs formatting from commit hook

There is a huge bug with the `glimmer` library (which prettier relies on for parsing)
where the <!DOCTYPE html> tag will get dropped improperly when formatting. I'm replacing
the tags and keeping the formatted files here but removing the auto-format settings
from commit hooks.

See more: prettier/prettier#11223
@Officialstrike
Copy link

Seems still to be an issue after the official release of handlebars support and glimmerjs fixing the issue?

@mwyschan
Copy link

mwyschan commented Jul 3, 2022

According to glimmerjs/glimmer-vm#870, the issues have already been fixed in glimmerjs/glimmer-vm#1305. However, I'm experiencing the same issues with v2.7.1.

@Chikanka
Copy link

Chikanka commented Aug 8, 2022

I found this issue on https://ovio.org/projects and would love to contribute!

@fisker
Copy link
Member

fisker commented Aug 8, 2022

Go ahead!

@fisker
Copy link
Member

fisker commented Sep 8, 2022

I tried to work on this, but it seems the doctype info still missing in AST.

> require('@glimmer/syntax').preprocess('<!DOCTYPE html>')
{
  type: 'Template',
  body: [],
  blockParams: [],
  loc: SourceSpan {
    data: HbsSpan {
      source: [Source],
      hbsPositions: [Object],
      kind: 'HbsPosition',
      _charPosSpan: null,
      _providedHbsLoc: [Object]
    },
    isInvisible: false
  }
}

@paulsmithkc paulsmithkc changed the title Prettier is removing the doctype from HTML files Prettier is removing the doctype from handebar files Dec 16, 2022
@paulsmithkc paulsmithkc changed the title Prettier is removing the doctype from handebar files Prettier is removing the doctype from handlebar files Dec 16, 2022
@portfolioris
Copy link

This happens to .astro files too.

Eric-Arellano added a commit to ParkingReformNetwork/reform-map that referenced this issue May 11, 2023
However, due to prettier/prettier#11223, we
don't always run Prettier on the handlebars file. Only manually and we
add back `<!DOCTYPE html>` to the template.
@RohithCIS
Copy link

RohithCIS commented Jul 3, 2023

On, VS Code the following .prettierrc works

{
  "overrides": [
    {
      "files": "*.handlebars",
      "options": {
        "parser": "html"
      }
    }
  ]
}

@Andrew15-5
Copy link

On, VS Code the following .prettierrc works

Prettier does not depend on text editor.

I tested it on prettier v2.8.8 and it works. Thanks.

Here's my config in .prettierrc.toml:

overrides = [
  { files = "*.hbs", options = { parser = "html" } }
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:handlebars Issues affecting Handlebars (Glimmer) scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

No branches or pull requests

9 participants