microsoft / vscode-dotnettools

This is a feedback repository to capture issues logged for the C# Dev Kit and related extensions from Visual Studio Code
Other
228 stars 12 forks source link

[BUG] cshtml formatter keeps adding blank lines #963

Open ysiivan opened 7 months ago

ysiivan commented 7 months ago

Describe the Issue

In some cases when saving (or anything that triggers formatting, I guess) a *.cshtml file in VS Code, blank lines are being added in some places with each save. The user might not even notice that their file is growing. In my case, a fragment near the bottom of the page got blown so much that in a week or so the file grew by dozens of thousands of rows.

Appears to be a bug in the formatter that perhaps does not properly work with line endings.

Steps To Reproduce

VS Code on Windows working on code base with Unix line endings (LF). The issue was not observed in Linux or in Windows when the file has Windows line endings {CRLF). I have this near the end of one of my Razor pages:


    @if (Model.IsActive)
    {
        <button type="submit" class="btn btn-primary" asp-page-handler="Inactivate">Inactivate</button>
    }
    else
    {
        <button type="submit" class="btn btn-primary" asp-page-handler="Activate">Activate</button>
        <button type="submit" class="btn btn-primary" asp-page-handler="Save">Save</button>
    }
</form>

After one save, it looks like this

    @if (Model.IsActive)

    {
        <button type="submit" class="btn btn-primary" asp-page-handler="Inactivate">Inactivate</button>
    }

    else

    {
        <button type="submit" class="btn btn-primary" asp-page-handler="Activate">Activate</button>
        <button type="submit" class="btn btn-primary" asp-page-handler="Save">Save</button>
    }
</form>

After two saves:

    @if (Model.IsActive)

    {
        <button type="submit" class="btn btn-primary" asp-page-handler="Inactivate">Inactivate</button>
    }

    else

    {
        <button type="submit" class="btn btn-primary" asp-page-handler="Activate">Activate</button>
        <button type="submit" class="btn btn-primary" asp-page-handler="Save">Save</button>
    }
</form>

and so on

Expected Behavior

Blank lines should not be added. The formatter should work correctly with either LF or CRLF files regardless of the OS VS Code is being used on.

Environment Information

Windows 11

ryzngard commented 2 months ago

@jordi1215 is this now fixed? I remember you had something similar....

aktxyz commented 2 months ago

happens to me as well fyi ... and I like to auto format so I end up with a ton of empty lines usually after a @foreach() ...

any workarounds ? I have tried adding comments and poking at it but seems to add lines reliably !

c8j commented 2 weeks ago

Still happening for me as well (only using the C# extension, without devkit) on my windows machine only (since I set it up to also use LF to be consistent with my linux machine)