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

[Bug]: Endless loop on format #761

Closed
SoFizzticated opened this issue Aug 19, 2023 · 2 comments
Closed

[Bug]: Endless loop on format #761

SoFizzticated opened this issue Aug 19, 2023 · 2 comments
Assignees
Labels
bug Something isn't working Stale Stale label

Comments

@SoFizzticated
Copy link

SoFizzticated commented Aug 19, 2023

Description

(Trying from a fresh install to eliminate possible errors, tried both with ONLY the plugin and with the "Laravel Extension Pack")
Whenever I try to format on any blade file using laravel-blade-formatter, plugin goes into an endless loop and starts clogging any other actions. After the loop has started, any saves on other files get severely slower. The formatting itself never happens.
1

EDIT
Found out after further restarts that this is happening in one particular file, triggering formatting on the attached code seems to break the plugin until the next VSCode restart.
`

<?php
    // Build a calendar
    // First day of this month, then monday of that week
    $start = Carbon\Carbon::now()->startOfMonth()->startOfWeek();
    // Last day of this month, then sunday of that week
    $end = Carbon\Carbon::now()->endOfMonth()->endOfWeek();
    $periods = [];
    $period = [];
    $day = $start->copy();
    while ($day->lte($end)) {
        $period[] = $day->copy();
        if ($day->isSunday()) {
            $periods[] = $period;
            $period = [];
        }
        $day->addDay();
    }

    // Worker fetching
    // TODO: Doesn't exist yet, faked for now
    // Worker has [id, name, days => [date => [clockin, clockout], notes]]
    $workerNames = ['Name1', 'Name2', 'Name3', 'Name4'];
    $workers = [];
    for ($i = 0; $i < 4; $i++) {
        $worker = new stdClass();
        $worker->id = $i;
        $worker->name = $workerNames[$i];
        $worker->days = [];
        for ($j = 0; $j < rand(8, 10); $j++) {
            // Random day between 0 and 31, trailing 0 if < 10 $day=rand(1, 31);
            $day = $day < 10 ? '0' . $day : $day;
            $worker->days['2023-08-' . $day] = ['clockin' => '08:00', 'clockout' =>
            '16:00', 'notes' => ''];
        }
        $workers[] = $worker;
    }
?>

<div class="container">
    <div class="grid w-full grid-cols-8 overflow-x-auto break-words bg-gray-200">
        {{-- X axis = Days of the week, Y axis = row --}}
        @foreach ($periods as $period)
        {{-- Day headers --}}
        <div class=""></div>
        @foreach ($period as $day)
        <div class="text-center bg-green-100 border border-gray-300">{{ $day->format('d/m') }}</div>
        @endforeach

        {{-- Worker rows --}}
        @foreach ($workers as $worker)
        {{-- If the $worker has no $worker->days matching any day in $period, continue --}}
        @if (count(array_intersect_key($worker->days, array_flip(array_map(function ($day) { return
        $day->format('Y-m-d'); }, $period)))) == 0)
        @continue
        @endif
        <div class="border border-gray-300 bg-green-50">{{ $worker->name }}</div>
        @foreach ($period as $day)
        @if (isset($worker->days[$day->format('Y-m-d')]))
        <div
            class="relative flex flex-col font-mono text-center bg-white border border-gray-300 group md:flex-row md:place-content-between md:px-3">
            {{-- Button for editting an entry for this day and worker --}}
            <div
                class="box-border absolute left-0 right-0 z-10 hidden w-full h-full bg-yellow-100 cursor-pointer group-hover:block opacity-60">
                <button class="w-full h-full"
                    onclick="alert('Editant...\r\nHorari: 08:00 - 16:00\r\nNota:')">Editar</button>
            </div>

            {{-- Clockin --}}
            <span>
                {{ $worker->days[$day->format('Y-m-d')]['clockin'] }}
        </span>
        {{-- Clockout --}}
        <span>
            {{ $worker->days[$day->format('Y-m-d')]['clockout'] }}

        </span>
        {{-- Notes --}}
        @if ($worker->days[$day->format('Y-m-d')]['notes'] != '')
        <span class="hidden md:block">
            {{ $worker->days[$day->format('Y-m-d')]['notes'] }}
        </span>
        @endif
        </div>
        @else
        <div class="relative text-center bg-white border border-gray-300 group">
            {{-- Button for adding an entry for this day and worker --}}
            <div
                class="box-border absolute left-0 right-0 z-10 hidden w-full h-full bg-green-100 cursor-pointer group-hover:block opacity-60">
                <button class="w-full h-full" onclick="alert('Afegint entrada...')">+</button>
            </div>
        </div>
        @endif
        @endforeach
        @endforeach

        {{-- Row to add worker --}}
        <div>
            {{-- Worker select --}}
            <select class="w-full h-full p-0.5 select select-xs">
                <option value="0" selected disabled>Afegir treballador</option>
                @foreach ($workers as $worker)
                <option value="{{ $worker->id }}">{{ $worker->name }}</option>
                @endforeach
            </select>
        </div>
        @foreach ($period as $day)
        <div class="relative text-center bg-gray-100 border border-gray-300 group opacity-60">
            {{-- Button for adding an entry for this day and worker --}}
            <div
                class="box-border absolute left-0 right-0 z-10 hidden w-full h-full bg-green-100 cursor-pointer group-hover:block opacity-60">
                <button class="w-full h-full" onclick="alert('Afegint entrada...')">+</button>
            </div>
        </div>
        @endforeach

        {{-- Spacer --}}
        <div class="h-4 col-span-8"></div>
        @endforeach
    </div>
</div>
`

Expected Behavior

Code should be formatted as per laravel-blade-formatter's guidelines

Actual Behavior

Visual Studio Code breaks and gets stuck in an endless loop of being unable to format (nor save, if save on format was enabled)

Additional Context

version: 0.22.1
platform: Windows 10 (64-bit)

EDIT 2

Found out that a mix of the php blade tag, a comment with a hyphen, and the code attached seem to break the inner logic of the formatter which, when combined with other code, can escalate to an infinite loop? (since I'm failing at attaching code with formatting, posting an image too):

Code to reproduce

<x-app-layout> @php // TODO: A hyphen here ' breaks things $worker->days['2023-08-' . $day] = ['clockin' => '08:00', 'clockout' => '16:00', 'notes' => '']; @endphp </x-app-layout>

Expected result

<x-app-layout> @php // TODO: A hyphen here ' breaks things $worker->days['2023-08-' . $day] = ['clockin' => '08:00', 'clockout' => '16:00', 'notes' => '']; @endphp </x-app-layout>

Actual result

<x-app-layout> @php // TODO: A hyphen here ' breaks things $worker->days['2023-08-' . $day] = ['clockin' => '08:00', 'clockout' => '16:00', 'notes' => '']; @endphp </x-app-layout>

Hyphen in / Hyphen out

2
imagen

@SoFizzticated SoFizzticated added the bug Something isn't working label Aug 19, 2023
@shufo shufo self-assigned this Aug 19, 2023
@shufo
Copy link
Owner

shufo commented Sep 9, 2023

Thanks @SoFizzticated.

Problem 1

It seems deeply nested parentheses causes this infinite loop

@if (count(array_intersect_key($worker->days, array_flip(array_map(function ($day) { return
    $day->format('Y-m-d'); }, $period)))) == 0)
    @continue
@endif

You can work around this by extract condition from parentheses.

@php
  	$condition =
  	count(
      array_intersect_key(
          $worker->days,
          array_flip(
              array_map(function ($day) {
                  return $day->format('Y-m-d');
              }, $period),
          ),
      ),
  ) == 0;
@endphp

@if ($condition)
    @continue
@endif

Problem 2

It may not be the cause of the infinite loop, but some other bug. I'll find out what's going on here.
Thanks for your help!

image

Copy link
Contributor

github-actions bot commented Nov 9, 2023

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

@github-actions github-actions bot added the Stale Stale label label Nov 9, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Stale Stale label
Projects
None yet
Development

No branches or pull requests

2 participants