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

env: Unable to boot on Windows OS #50814

Closed
t-hamano opened this issue May 20, 2023 · 4 comments · Fixed by #50895
Closed

env: Unable to boot on Windows OS #50814

t-hamano opened this issue May 20, 2023 · 4 comments · Fixed by #50895
Assignees
Labels
[Package] Env /packages/env

Comments

@t-hamano
Copy link
Contributor

Description

It appears that version 7.0.0 of the env package is no longer able to start on Windows OS.

Here are all the logs after running the command:

Log
npm run wp-env start

> test@ wp-env D:\Desktop\test
> wp-env "start"

‼ Warning: could not find a .wp-env.json configuration file and could not determine if 'D:\Desktop\test' is a WordPress installation, a plugin, or a theme.
× Error while running docker-compose command.
#1 [internal] load build definition from WordPress.Dockerfile
#1 transferring dockerfile: 1.91kB 0.0s done
#1 DONE 0.1s

#2 [internal] load .dockerignore
#2 transferring context: 2B 0.0s done
#2 DONE 0.1s

#3 [internal] load metadata for docker.io/library/wordpress:latest
#3 DONE 2.5s

#4 [ 1/17] FROM docker.io/library/wordpress@sha256:f3b8d54fe9b80e88255121a46933ca1961fb9f3f39e6981a668cdb7f019a5bbd
#4 DONE 0.0s

#5 [ 9/17] RUN apt-get -qy install sudo
#5 CACHED

#6 [11/17] RUN echo 'upload_max_filesize = 1G' >> /usr/local/etc/php/php.ini
#6 CACHED

#7 [ 6/17] RUN useradd -m -u 0 -g 0 {username} || true
#7 CACHED

#8 [ 7/17] RUN apt-get -qy update
#8 CACHED

#9 [ 2/17] RUN sed -i 's|deb.debian.org/debian stretch|archive.debian.org/debian stretch|g' /etc/apt/sources.list
#9 CACHED

#10 [ 8/17] RUN apt-get -qy install autoconf            dpkg-dev                file            g++             gcc             libc-dev                make            pkg-config              re2c && touch /usr/local/etc/php/php.ini
#10 CACHED

#11 [ 5/17] RUN groupadd -g 0 {username} || true
#11 CACHED

#12 [ 3/17] RUN sed -i 's|security.debian.org/debian-security stretch|archive.debian.org/debian-security stretch|g' /etc/apt/sources.list
#12 CACHED

#13 [15/17] RUN php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer
#13 CACHED

#14 [12/17] RUN echo 'post_max_size = 1G' >> /usr/local/etc/php/php.ini
#14 CACHED

#15 [13/17] RUN curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php
#15 CACHED

#16 [14/17] RUN export COMPOSER_HASH=`curl -sS https://composer.github.io/installer.sig` && php -r "if (hash_file('SHA384', '/tmp/composer-setup.php') === '$COMPOSER_HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('/tmp/composer-setup.php'); } echo PHP_EOL;"
#16 CACHED

#17 [10/17] RUN echo "{username} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
#17 CACHED

#18 [ 4/17] RUN sed -i '/stretch-updates/d' /etc/apt/sources.list
#18 CACHED

#19 [16/17] RUN rm /tmp/composer-setup.php
#19 CACHED

#20 [17/17] RUN composer global require --dev yoast/phpunit-polyfills:"^1.0"
#20 ERROR: process "/bin/sh -c composer global require --dev yoast/phpunit-polyfills:\"^1.0\"" did not complete successfully: unable to find user {username}: no matching entries in passwd file  
------
 > [17/17] RUN composer global require --dev yoast/phpunit-polyfills:"^1.0":
------
failed to solve: process "/bin/sh -c composer global require --dev yoast/phpunit-polyfills:\"^1.0\"" did not complete successfully: unable to find user {username}: no matching entries in passwd file
npm ERR! code ELIFECYCLE
npm ERR! errno 17
npm ERR! test@ wp-env: `wp-env "start"`
npm ERR! Exit status 17
npm ERR!
npm ERR! Failed at the test@ wp-env script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\{username}\AppData\Roaming\npm-cache\_logs\2023-05-20T13_27_15_180Z-debug.log

I deleted all Docker containers, images, volumes, and networks before running the command. With env version 6.0.0, it can be started normally.

The following are possible error locations in the logs:

#20 [17/17] RUN composer global require --dev yoast/phpunit-polyfills:"^1.0"
#20 ERROR: process "/bin/sh -c composer global require --dev yoast/phpunit-polyfills:\"^1.0\"" did not complete successfully: unable to find user {username}: no matching entries in passwd file  
------
 > [17/17] RUN composer global require --dev yoast/phpunit-polyfills:"^1.0":
------
failed to solve: process "/bin/sh -c composer global require --dev yoast/PHPUnit-polyfills:\"^1.0\"" did not complete successfully: unable to find user {username}: no matching entries in passwd file

{username} shows the actual Windows OS login user. If the following commands are executed directly, the installation completed successfully:

composer global require --dev yoast/phpunit-polyfills:"^1.0"

I think this issue might be related to #50408, where the command causing the error was added.

Step-by-step reproduction instructions

  • Create a package.json like the following:
{
  "name": "test",
  "scripts": {
    "wp-env": "wp-env"
  },
  "dependencies": {
    "@wordpress/env": "^7.0.0"
  }
}
  • npm i
  • npm run wp-env start

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@t-hamano t-hamano added the [Package] Env /packages/env label May 20, 2023
@t-hamano
Copy link
Contributor Author

I expected the problem to be a failure to add users in the Docker container. So I made the following changes:

diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js
index 4a573c97aa..a86b40896e 100644
--- a/packages/env/lib/init-config.js
+++ b/packages/env/lib/init-config.js
@@ -133,8 +133,8 @@ ARG HOST_USERNAME
 ARG HOST_UID
 ARG HOST_GID
 # When the IDs are already in use we can still safely move on.
-RUN groupadd -g $HOST_GID $HOST_USERNAME || true
-RUN useradd -m -u $HOST_UID -g $HOST_GID $HOST_USERNAME || true
+RUN groupadd -g $HOST_GID $HOST_USERNAME
+RUN useradd -m -u $HOST_UID -g $HOST_GID $HOST_USERNAME

 # Install any dependencies we need in the container.
 ${ installDependencies( 'wordpress', env, config ) }`;
@@ -167,7 +167,7 @@ RUN adduser -h /home/$HOST_USERNAME -G $( getent group $HOST_GID | cut -d: -f1 )

 # Install any dependencies we need in the container.
 ${ installDependencies( 'cli', env, config ) }
-
+
 # Switch back to the original user now that we're done.
 USER www-data

When I run npm run wp-env start, I see that it fails to add groups and users as shown below. This problem does not occur on WSL2 (Linux).

#8 [ 5/17] RUN groupadd -g 0 tetsuaki.hamano
#8 0.286 groupadd: GID '0' already exists
#8 ERROR: process "/bin/sh -c groupadd -g $HOST_GID $HOST_USERNAME" did not complete successfully: exit code: 4
------
 > [ 5/17] RUN groupadd -g 0 tetsuaki.hamano:
#8 0.286 groupadd: GID '0' already exists
------
failed to solve: process "/bin/sh -c groupadd -g $HOST_GID $HOST_USERNAME" did not complete successfully: exit code: 4

Next, I thought the problem might be that HOST_UID and HOST_GID are zero and already exist. So I hard-coded each with a fixed value:

diff --git a/packages/env/lib/init-config.js b/packages/env/lib/init-config.js
index 4a573c97aa..efbed4486a 100644
--- a/packages/env/lib/init-config.js
+++ b/packages/env/lib/init-config.js
@@ -133,8 +133,8 @@ ARG HOST_USERNAME
 ARG HOST_UID
 ARG HOST_GID
 # When the IDs are already in use we can still safely move on.
-RUN groupadd -g $HOST_GID $HOST_USERNAME || true
-RUN useradd -m -u $HOST_UID -g $HOST_GID $HOST_USERNAME || true
+RUN groupadd -g 9999 $HOST_USERNAME || true
+RUN useradd -m -u 9999 -g 9999 $HOST_USERNAME || true

 # Install any dependencies we need in the container.
 ${ installDependencies( 'wordpress', env, config ) }`;
@@ -162,12 +162,12 @@ ARG HOST_USERNAME
 ARG HOST_UID
 ARG HOST_GID
 # When the IDs are already in use we can still safely move on.
-RUN addgroup -g $HOST_GID $HOST_USERNAME || true
-RUN adduser -h /home/$HOST_USERNAME -G $( getent group $HOST_GID | cut -d: -f1 ) -u $HOST_UID $HOST_USERNAME || true
+RUN addgroup -g 9999 $HOST_USERNAME || true
+RUN adduser -h /home/$HOST_USERNAME -G $( getent group 9999 | cut -d: -f1 ) -u 9999 $HOST_USERNAME || true

 # Install any dependencies we need in the container.
 ${ installDependencies( 'cli', env, config ) }
-
+
 # Switch back to the original user now that we're done.
 USER www-data

The error then changed to the following

npm run wp-env start

> gutenberg@15.8.1 wp-env D:\_local\WordPress\gutenberg\app\public\wp-content\plugins\gutenberg
> wp-env "start"

× Error while running docker-compose command.
 Container 9309c35b6b008e96c6c014f1a88dcbb4-mysql-1  Running
 Container 9309c35b6b008e96c6c014f1a88dcbb4-wordpress-1  Created
 Container 9309c35b6b008e96c6c014f1a88dcbb4-wordpress-1  Starting
 Container 9309c35b6b008e96c6c014f1a88dcbb4-wordpress-1  Started
Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under.

If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.

If you'd like to continue as root, please run this again, adding this flag:  --allow-root

If you'd like to run it as the user that this site is under, you can run the following to become the respective user:

    sudo -u USER -i -- wp <command>


npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gutenberg@15.8.1 wp-env: `wp-env "start"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gutenberg@15.8.1 wp-env script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\tetsuaki.hamano\AppData\Roaming\npm-cache\_logs\2023-05-20T14_38_40_952Z-debug.log
@t-hamano
Copy link
Contributor Author

@ObliviousHarmony @noahtallen

I think UID and GID are for Unix file systems; SID is used for Windows. I expect this is why HOST_GID and HOST_UID are zero in the Windows OS.

Is there any problem with hard-coding HOST_GID and HOST_UID with arbitrary numbers?

@ObliviousHarmony
Copy link
Contributor

Thanks @t-hamano,

The uid and gid are set to 0 (root) on Windows, because, as you noted, these concepts don't actually exist. The user add and group add instructions have || true because we don't really care whether or not they are successful. Docker's --user option lets us use the uid:gid format (0:0) and so it never mattered whether or not the user was actually created. All that matters is that we're in the container with the right IDs.

This worked fine in the original PR updating permissions, but, we're now switching to the user in order to install Composer. While it's true that you've encountered this problem because of the deliberate root UID, technically, this would actually happen whenever the UID already exists in the container. Looking at the documentation, it seems that the USER instruction does support the uid:gid format, so, we should change the instruction USER $HOST_USERNAME to USER $HOST_UID:$HOST_GID.

The caveat here is that composer gets really mad when you try to run it as root. We can solve this with the COMPOSER_ALLOW_SUPERUSER environment variable though.

Is there any problem with hard-coding HOST_GID and HOST_UID with arbitrary numbers?

I don't know! I went with root because it felt like this would be the most permissive and better reflect the way permissions are mapped on Windows. Given that the above should be able to fully resolve this issue, do you think continuing to use root would be ideal?

@ObliviousHarmony
Copy link
Contributor

Assigning an arbitrary user might be the best course of action on Windows 😄 I realized this was a bad idea five seconds after I tried it because literally everything gets mad if you run it as root. Apache very aggressively refuses to start and I imagine even if that was worked around there is still a myriad of problems to navigate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Env /packages/env
3 participants