Skip to content

Commit 7f540d8

Browse files
Fixed wp-env start On Windows (#50895)
1 parent 87c08e6 commit 7f540d8

File tree

7 files changed

+34
-15
lines changed

7 files changed

+34
-15
lines changed

packages/env/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
- Support using double dashes in `wp-env run ...` to pass arguments that would otherwise be consumed by `wp-env`. For example, while normally `--help` would provide the `wp-env` help text, if you use `npx wp-env run cli php -- --help` you will see the PHP help text.
1818
- Validate whether or not config options exist to prevent accidentally including ones that don't.
1919

20+
### Bug fix
21+
22+
- Support Windows without requiring the use of WSL.
23+
2024
## 7.0.0 (2023-05-10)
2125

2226
### Breaking Change

packages/env/lib/commands/run.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ function spawnCommandDirectly( config, container, command, envCwd, spinner ) {
6767
// to interact with the files mounted from the host.
6868
const hostUser = getHostUser();
6969

70-
// We need to pass absolute paths to the container.
71-
envCwd = path.resolve(
70+
// Since Docker requires absolute paths, we should resolve the input to a POSIX path.
71+
// This is needed because Windows resolves relative paths from the C: drive.
72+
envCwd = path.posix.resolve(
7273
// Not all containers have the same starting working directory.
7374
container === 'mysql' || container === 'tests-mysql'
7475
? '/'

packages/env/lib/config/parse-config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const DEFAULT_ENVIRONMENT_CONFIG = {
8787
testsPort: 8889,
8888
mappings: {},
8989
config: {
90+
FS_METHOD: 'direct',
9091
WP_DEBUG: true,
9192
SCRIPT_DEBUG: true,
9293
WP_ENVIRONMENT_TYPE: 'local',
@@ -233,7 +234,10 @@ async function getDefaultConfig(
233234
env: {
234235
development: {},
235236
tests: {
236-
config: { WP_DEBUG: false, SCRIPT_DEBUG: false },
237+
config: {
238+
WP_DEBUG: false,
239+
SCRIPT_DEBUG: false,
240+
},
237241
},
238242
},
239243
};

packages/env/lib/config/test/__snapshots__/config-integration.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ exports[`Config Integration should load local and override configuration files 1
88
"env": {
99
"development": {
1010
"config": {
11+
"FS_METHOD": "direct",
1112
"SCRIPT_DEBUG": true,
1213
"WP_DEBUG": true,
1314
"WP_ENVIRONMENT_TYPE": "local",
@@ -35,6 +36,7 @@ exports[`Config Integration should load local and override configuration files 1
3536
},
3637
"tests": {
3738
"config": {
39+
"FS_METHOD": "direct",
3840
"SCRIPT_DEBUG": false,
3941
"WP_DEBUG": false,
4042
"WP_ENVIRONMENT_TYPE": "local",
@@ -79,6 +81,7 @@ exports[`Config Integration should load local configuration file 1`] = `
7981
"env": {
8082
"development": {
8183
"config": {
84+
"FS_METHOD": "direct",
8285
"SCRIPT_DEBUG": true,
8386
"WP_DEBUG": true,
8487
"WP_ENVIRONMENT_TYPE": "local",
@@ -106,6 +109,7 @@ exports[`Config Integration should load local configuration file 1`] = `
106109
},
107110
"tests": {
108111
"config": {
112+
"FS_METHOD": "direct",
109113
"SCRIPT_DEBUG": false,
110114
"WP_DEBUG": false,
111115
"WP_ENVIRONMENT_TYPE": "local",
@@ -150,6 +154,7 @@ exports[`Config Integration should use default configuration 1`] = `
150154
"env": {
151155
"development": {
152156
"config": {
157+
"FS_METHOD": "direct",
153158
"SCRIPT_DEBUG": true,
154159
"WP_DEBUG": true,
155160
"WP_ENVIRONMENT_TYPE": "local",
@@ -177,6 +182,7 @@ exports[`Config Integration should use default configuration 1`] = `
177182
},
178183
"tests": {
179184
"config": {
185+
"FS_METHOD": "direct",
180186
"SCRIPT_DEBUG": false,
181187
"WP_DEBUG": false,
182188
"WP_ENVIRONMENT_TYPE": "local",
@@ -221,6 +227,7 @@ exports[`Config Integration should use environment variables over local and over
221227
"env": {
222228
"development": {
223229
"config": {
230+
"FS_METHOD": "direct",
224231
"SCRIPT_DEBUG": true,
225232
"WP_DEBUG": true,
226233
"WP_ENVIRONMENT_TYPE": "local",
@@ -249,6 +256,7 @@ exports[`Config Integration should use environment variables over local and over
249256
},
250257
"tests": {
251258
"config": {
259+
"FS_METHOD": "direct",
252260
"SCRIPT_DEBUG": false,
253261
"WP_DEBUG": false,
254262
"WP_ENVIRONMENT_TYPE": "local",

packages/env/lib/config/test/parse-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const DEFAULT_CONFIG = {
3535
pluginSources: [],
3636
themeSources: [],
3737
config: {
38+
FS_METHOD: 'direct',
3839
WP_DEBUG: true,
3940
SCRIPT_DEBUG: true,
4041
WP_ENVIRONMENT_TYPE: 'local',

packages/env/lib/get-host-user.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ module.exports = function getHostUser() {
1313
const hostUser = os.userInfo();
1414

1515
// On Windows the uid and gid will be -1. Since there isn't a great way to handle this,
16-
// we're just going to say that the host user is root. On Windows you'll likely be
17-
// using WSL to run commands inside the container, which has a uid and gid. If
18-
// you aren't, you'll be mounting directories from Windows, to a Linux
19-
// VM (Docker Desktop uses one), to the guest OS. I assume that
20-
// when dealing with this configuration that file ownership
21-
// has the same kind of magic handling that macOS uses.
22-
const uid = ( hostUser.uid === -1 ? 0 : hostUser.uid ).toString();
23-
const gid = ( hostUser.gid === -1 ? 0 : hostUser.gid ).toString();
16+
// we're just going to assign them to 1000. Docker Desktop already takes care of
17+
// permission-related issues using magic, so this should be fine.
18+
const uid = ( hostUser.uid === -1 ? 1000 : hostUser.uid ).toString();
19+
const gid = ( hostUser.gid === -1 ? 1000 : hostUser.gid ).toString();
2420

2521
return {
2622
name: hostUser.username,

packages/env/lib/init-config.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,15 +202,20 @@ RUN apt-get -qy install $PHPIZE_DEPS && touch /usr/local/etc/php/php.ini
202202
203203
# Set up sudo so they can have root access.
204204
RUN apt-get -qy install sudo
205-
RUN echo "$HOST_USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers`;
205+
RUN echo "#$HOST_UID ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers`;
206206
break;
207207
}
208208
case 'cli': {
209209
dockerFileContent += `
210+
# Make sure we're working with the latest packages.
210211
RUN apk update
212+
213+
# Install some basic PHP dependencies.
211214
RUN apk --no-cache add $PHPIZE_DEPS && touch /usr/local/etc/php/php.ini
215+
216+
# Set up sudo so they can have root access.
212217
RUN apk --no-cache add sudo linux-headers
213-
RUN echo "$HOST_USERNAME ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers`;
218+
RUN echo "#$HOST_UID ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers`;
214219
break;
215220
}
216221
default: {
@@ -238,8 +243,8 @@ RUN rm /tmp/composer-setup.php`;
238243
// Install any Composer packages we might need globally.
239244
// Make sure to do this as the user and ensure the binaries are available in the $PATH.
240245
dockerFileContent += `
241-
USER $HOST_USERNAME
242-
ENV PATH="\${PATH}:/home/$HOST_USERNAME/.composer/vendor/bin"
246+
USER $HOST_UID:$HOST_GID
247+
ENV PATH="\${PATH}:~/.composer/vendor/bin"
243248
RUN composer global require --dev yoast/phpunit-polyfills:"^1.0"
244249
USER root`;
245250

0 commit comments

Comments
 (0)