wp-cli/config-command

WP-CLI detecting wp-config.php in parent folder could cause command to fail

Opened this issue · 8 comments

The case:

Let's say I'm having a WordPress installation which is not working for some reason or WordPress installation leftovers in public_html/. Meanwhile I want to install and configure WordPress in public_html/some-random-subfolder using WP-CLI. I download and place the files in the subfolder, prepare DB and try to

wp config create

The action fails with:

wp config create --dbname=test --dbuser=test --dbpass=test --dbhost=127.0.0.1
Error: The 'wp-config.php' file already exists.

Here is what I have in the wp-config.php in the parent folder:

cat ../wp-config.php 
<?php

echo "some broken or custom wp-config.php file";
require_once(__DIR__.'/wp-config-CUSTOM.php');

Additionally, all other WP-CLI commands except wp --info are also failing:

wp core
Error: Strange wp-config.php file: wp-settings.php is not loaded directly.

If there's no wp-config.php in the parent dir - it works:

ll ../wp-config*
ls: cannot access '../wp-config*': No such file or directory

wp config create --dbname=test --dbuser=test --dbpass=test --dbhost=127.0.0.1
ERROR 1045 (28000): Access denied for user 'test'@'localhost' (using password: YES)

(DB error above is normal, there's no such DB but the point is that I don't receive the wp-config.php related error)

If there is a valid WordPress application with a proper wp-config.php - it works as well.

So, the issue is if there is invalid wp-config/leftovers in the parent directory - this prevents me to configure a new, separate, independent WordPress app using WP-CLI in a sub-folder.

I've tested that on different 2 different stacks - LAMP and LEMP - the behaviour is the same.

Maybe it would be a good idea commands like "config", "core", etc. which could be related to installing and configuring separate application to be independent from the anything else than the files in the current folder?

Have you tried it using many sub-folders, like, for one wordpress app you use public_html/app1 which contains it's own wp-config.php in there, i.e, public_html/app1/wp-config.php and the other app can have a subfolder like public_html/app2 making it public_html/app2/wp-config.php

@ivanyord I tried to replicate this just now but couldn't. When I tried creating a new config file in a subfolder like you described above, it just created it normally, even though the parent folder already had one (whether that was broken or not).

You should check whether you were maybe loading a global config file that was pointing WP-CLI to a specific folder with a WordPress install.

Hi, I think I'm hitting the same issue.

Steps to reproduce:

~ docker run --rm -it php:7.4 /bin/bash
# cd /var/www/html
# curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# chmod +x wp-cli.phar
# mv wp-cli.phar /usr/local/bin/wp
# touch wp-config.php
# mkdir foo
# cd foo
# wp core download --allow-root
# wp config create --dbname=foo --dbuser=bar --allow-root
Error: The 'wp-config.php' file already exists.

If I leave "/var/www/html/foo" empty and create another folder within it the command again works as expected

This has happened to us today with WP-CLI 2.4.0

I think the config create command should indeed only check for existence of a config in the spot where it intends to write one, not in any parent folders.

Also, as a work-around for now, the current check is ignored if you use the --force flag.

Hi @ivanyord @schlessera @jkahrs @n3storm @anirudhdggl, I tried to reproduce the issue mentioned, but not able to reproduce it. Here is the approach I used to replicate the same.

Approach,

  1. Created the WordPress directory, use wp core download to download the WordPress.
  2. Use wp config create .. to set up the config file. Works correctly.
  3. Created the subfolder test-wordpress-config, and run wp core download inside it.
  4. Again run wp config create and this time also config file created successfully.

Let me know if I have tested this correct. Also, may be the issue seems to be too old, hence a newer version might have resolved this. Can you please try to replicate this issue with newer version?

Here is the wp --info content from which I tried to reproduce this,

Screenshot 2023-05-24 at 2 42 14 PM

Thanks.

Does wp cli create any hidden files with metadata when executing wp core download that may help wp cli recognise each wp installation?

In my case both wordpress where not downloaded and installed using wp cli.

jkahrs commented

Hi @ivanyord @schlessera @jkahrs @n3storm @anirudhdggl, I tried to reproduce the issue mentioned, but not able to reproduce it. Here is the approach I used to replicate the same.

Approach,

1. Created the WordPress directory, use `wp core download` to download the WordPress.

2. Use `wp config create ..` to set up the config file. Works correctly.

3. Created the subfolder `test-wordpress-config`, and run `wp core download` inside it.

4. Again run `wp config create` and this time also config file created successfully.

Let me know if I have tested this correct. Also, may be the issue seems to be too old, hence a newer version might have resolved this. Can you please try to replicate this issue with newer version?

Here is the wp --info content from which I tried to reproduce this,
Screenshot 2023-05-24 at 2 42 14 PM

Thanks.

Hi, for me the original steps from my listing still reproduce the issue. As a workaround I am currently using --force as mentioned by @schlessera