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

Replace DOS line endings with LF #49

Merged
merged 2 commits into from
Nov 10, 2023

Conversation

xknown
Copy link
Contributor

@xknown xknown commented Nov 10, 2023

DOS line endings are not currently stripped from the wp-config files, which results in extra lines being added to the modified file.

Fixes wp-cli/config-command#160
Related: wp-cli/wp-cli#5859

DOS line endings are not currently stripped from the wp-config files,
which results in extra lines being added to the modified file.

Fixes wp-cli/config-command#160
@xknown xknown requested a review from a team as a code owner November 10, 2023 08:12
@swissspidy swissspidy added this to the 1.3.5 milestone Nov 10, 2023
@swissspidy swissspidy self-requested a review November 10, 2023 09:41
@schlessera
Copy link
Member

@xknown This is not something that can be solved with a simple str_replace(), I'm afraid.

What the code does with your changes seems worse than before.

Imagine the scenario where you have two regular blank lines in the standard notation for servers:

\n\r\n\r

The logic with this PR will first replace the middle part of that sequence, breaking the two individual blank lines. Then, as it completes, the final result will be this:

\n\n\n\n

So, for the more common configurations, it will end up turning two blank lines into four blank lines.

To solve this properly, we would need to switch away from str_replace() and use a regular expression that considers entire groups in one go and detects the patterns.

@xknown
Copy link
Contributor Author

xknown commented Nov 10, 2023

@xknown This is not something that can be solved with a simple str_replace(), I'm afraid.

What the code does with your changes seems worse than before.

Imagine the scenario where you have two regular blank lines in the standard notation for servers:

\n\r\n\r

The logic with this PR will first replace the middle part of that sequence, breaking the two individual blank lines. Then, as it completes, the final result will be this:

\n\n\n\n

That's not really the case though, because the str_replace will first replace\n\r (or \r\n) by a single \n. However, I agree that using a regular expression might probably be better to only replace those characters where it makes most sense.

@schlessera
Copy link
Member

@xknown Indeed, you're right, my bad. I tested this in 3v4l.org and it seems to reliably work with all constellations.

Comment on lines 12 to 16
"// this is a demo\r\n",
"define( 'DB_NAME', '' );\n",
"define( 'DB_HOST', '' );\r\n",
"define( 'DB_USER', '' );\n\r",
"define( 'DB_COLLATE', '');\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add double blank lines in multiple constellations here as well just to fully demonstrate it works without splitting the combinations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added them in dd6f0f9, however I think there's another bug that happens when the provided config file has a multiline string containing any of the replaced characters. In theory, we shouldn't be touching them. To fix it properly, we might require a proper PHP parser though, not sure if it already exists somewhere in wp-cli?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we don't have a PHP Parser around, we tried to avoid adding dependencies to this to make sure it doesn't randomly break from conflicts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

however I think there's another bug that happens when the provided config file has a multiline string containing any of the replaced characters.

Can you open a separate issue for this?

@schlessera schlessera merged commit 202aa80 into wp-cli:main Nov 10, 2023
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants