laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.29k stars 10.94k forks source link

Console commands, after interacting with the user, the code is incorrectly encoded. #35779

Closed maiconmva closed 3 years ago

maiconmva commented 3 years ago

Description:

In console commands, after interacting with the user, the code is incorrectly encoded.

As can be seen in the image below, with a very simple example, a message with special characters is displayed, just to show that it works, after that, we ask any question (confirmation fails too), and after that the encoding is lost. The console's answers come with the wrong encoding, but that wouldn't be a problem, the problem is that after the question, a database import is performed, and the imported data is scrambled.

I know its a windows problem, because on linux this error doesnt happend. as a standard shell application, I'm using git bash, but I've already tested with windows cmd and power shell

image

rodrigopedra commented 3 years ago

Might be something related to Mingw64, Laragon, Visual Studio Code terminal emulation or Windows.

I just tested in Linux and it works as expected.

Test code:

<?php // ./routes/console.php

use Illuminate\Support\Facades\Artisan;

Artisan::command('app:test', function () {
    $user = $this->ask('áêìöu');
    $this->info($user);
    $this->info('áêìöu');
    dd('áêìöu');
});

Output:

$ php artisan app:test

 áêìöu:
 > áêìöu

áêìöu
áêìöu
"áêìöu"
maiconmva commented 3 years ago

I know It Works on Linux, but its Common issue i think.., many people uses laravel on windows

rodrigopedra commented 3 years ago

But did you try directly on the windows terminal (Command Prompt/PowerShell), not inside a virtual environment such as Laragon or Mingwin?

Install PHP directly on windows and test with a clean installation to see if the error happens outside of these emulation layers. If you want to emulate a *nix environment try using WSL2.

At least, did you test on an official Laravel developement environment, such as Homestead?

It would be hard to support every development or deployment environment combination when multiple configuration factors can interfere in the outcome.

rodrigopedra commented 3 years ago

I did some testing on a virtual machine I have with windows and it fails.

To fix it you can set your php.ini's output_encoding variable to "UTF-8". By default it is empty.

output_encoding = "UTF-8"

Before setting output_encoding variable:

image

After setting output_encoding variable:

image

To find where your php.ini file is located run this command:

php -i | findstr "Configuration"

And check the path in the Loaded Configuration File line.

If you can't change the php.ini file, you can set at runtime by calling ini_set(...)

ini_set('output_encoding', 'UTF-8');

For example, in my sample command, it would become:

<?php // ./routes/console.php

use Illuminate\Support\Facades\Artisan;

Artisan::command('app:test', function () {
    ini_set('output_encoding', 'UTF-8');

    $user = $this->ask('áêìöu');
    $this->info($user);
    $this->info('áêìöu');
    dd('áêìöu');
});

If you want this to be run for every file you can add it to your AppServiceProvider file inside its boot(...) method.

Hope this helps.

driesvints commented 3 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.