Dotenv not found

Hi,

We’re currently building a new theme with Sage 10, but for some reason, we’re unable to use wp cli with it.

We’re working within a fresh bedrock install and I’ve tried the tips added to this thread (PHP Fatal error - Dotenv not found - cannot use WP CLI because of it. Any ideas?) but with no luck, unfortunately.

The strange thing is that when we delete the theme folder, wp cli works again, but there don’t seem to be any dotenv dependencies within Sage?

Bedrock composer file:

{
  "name": "roots/bedrock",
  "type": "project",
  "license": "MIT",
  "description": "WordPress boilerplate with Composer, easier configuration, and an improved folder structure",
  "homepage": "https://roots.io/bedrock/",
  "authors": [
    {
      "name": "Scott Walkinshaw",
      "email": "scott.walkinshaw@gmail.com",
      "homepage": "https://github.com/swalkinshaw"
    },
    {
      "name": "Ben Word",
      "email": "ben@benword.com",
      "homepage": "https://github.com/retlehs"
    }
  ],
  "keywords": [
    "bedrock", "composer", "roots", "wordpress", "wp", "wp-config"
  ],
  "support": {
    "issues": "https://github.com/roots/bedrock/issues",
    "forum": "https://discourse.roots.io/category/bedrock"
  },
  "repositories": [
    {
      "type": "composer",
      "url": "https://wpackagist.org",
      "only": ["wpackagist-plugin/*", "wpackagist-theme/*"]
    },
    {
      "type": "vcs",
      "url": "git@github.com:Imaginaire/im-cpt.git"
    },
    {
      "type": "vcs",
      "url": "git@github.com:Imaginaire/im-clean.git"
    },
    {
      "type": "vcs",
      "url": "https://github.com/wp-media/wp-rocket.git"
    },
    {
      "type": "vcs",
      "url": "git@github.com:Imaginaire/im-logo.git"
    },
    {
      "type": "vcs",
      "url": "git@github.com:Imaginaire/im-blocks.git"
    },
    {
			"type": "vcs",
			"url": "git@github.com:theimaginaire/acf-private.git"
		},
    {
      "type": "vcs",
      "url": "git@github.com:Imaginaire/gravity-forms.git"
    }
  ],
  "require": {
    "php": ">=7.4",
    "composer/installers": "^2.1",
    "vlucas/phpdotenv": "^5.4",
    "oscarotero/env": "^2.1",
    "roots/bedrock-autoloader": "^1.0",
    "roots/bedrock-disallow-indexing": "^2.0",
    "roots/wordpress": "5.9.3",
    "roots/wp-config": "1.0.0",
    "roots/wp-password-bcrypt": "1.1.0",
    "roots/acorn": "^2.0.1",
    "imaginaire/im-clean": "1.0.1",
    "wpackagist-plugin/sucuri-scanner": "dev-trunk",
    "wpackagist-plugin/seo-by-rank-math": "dev-trunk",
    "wpackagist-plugin/wp-mail-smtp": "dev-trunk",
    "wpackagist-plugin/redirection": "dev-trunk",
    "wpackagist-plugin/custom-post-type-ui": "dev-trunk",
    "wpackagist-plugin/custom-permalinks": "dev-trunk",
    "wpackagist-plugin/intuitive-custom-post-order": "dev-trunk",
    "wpackagist-plugin/page-or-post-clone": "dev-trunk",
    "elliotcondon/advanced-custom-fields-pro": "^5.12",
    "gravity-forms/gravity-forms": "^2.6"
  },
  "require-dev": {
    "squizlabs/php_codesniffer": "^3.6.2",
    "roave/security-advisories": "dev-latest"
  },
  "config": {
    "optimize-autoloader": true,
    "preferred-install": "dist",
    "allow-plugins": {
      "composer/installers": true,
      "roots/wordpress-core-installer": true
    }
  },
  "minimum-stability": "dev",
  "prefer-stable": true,
  "extra": {
    "installer-paths": {
      "web/app/mu-plugins/{$name}/": ["type:wordpress-muplugin"],
      "web/app/plugins/{$name}/": ["type:wordpress-plugin"],
      "web/app/themes/{$name}/": ["type:wordpress-theme"]
    },
    "wordpress-install-dir": "web/wp"
  },
  "scripts": {
    "post-root-package-install": [
      "php -r \"copy('.env.example', '.env');\""
    ],
    "test": [
      "phpcs"
    ]
  }
}

Any help would be massively appreciated!

I believe it is looking for a .env that you need to make. If you search in the stack, you should find a .env.example file.

Hi,

No, we have a .env file in place, so it’s not that.

Here is the error:

[13-May-2022 14:04:25 UTC] PHP Fatal error:  Uncaught Error: Class 'Dotenv\Environment\DotenvFactory' not found in /Users/seb/.wp-cli/packages/vendor/illuminate/support/helpers.php:646
Stack trace:
#0 /Users/seb/sites/im-edison/vendor/roots/acorn/config/broadcasting.php(18): env('BROADCAST_DRIVE...', 'null')
#1 /Users/seb/sites/im-edison/vendor/roots/acorn/src/Roots/Acorn/Bootstrap/LoadConfiguration.php(66): require('/Users/seb/site...')
#2 /Users/seb/sites/im-edison/vendor/roots/acorn/src/Roots/Acorn/Bootstrap/LoadConfiguration.php(37): Roots\Acorn\Bootstrap\LoadConfiguration->loadConfigurationFiles(Object(Roots\Acorn\Application), Object(Illuminate\Config\Repository))
#3 /Users/seb/sites/im-edison/vendor/roots/acorn/src/Illuminate/Foundation/Application.php(237): Roots\Acorn\Bootstrap\LoadConfiguration->bootstrap(Object(Roots\Acorn\Application))
#4 /Users/seb/sites/im-edison/vendor/roots/acorn/src/Roots/Acorn/Bootloader.php(208): Illuminate\Foundation\Application->bootstrapWith(Array)
#5 /Users/seb/sites/im-edison/vendor/roots/acorn/src/Roots/Acorn/Bo in /Users/seb/.wp-cli/packages/vendor/illuminate/support/helpers.php on line 646

I found this in a github discussion.

Thanks, but that’s from a really old thread (2019) and isn’t applicable to this problem. Still having the issue. For clarity, these are the composer requires:

"php": ">=7.4",
    "composer/installers": "^2.1",
    "vlucas/phpdotenv": "^5.4",
    "oscarotero/env": "^2.1",
    "roots/bedrock-autoloader": "^1.0",
    "roots/bedrock-disallow-indexing": "^2.0",
    "roots/wordpress": "5.9.3",
    "roots/wp-config": "1.0.0",
    "roots/wp-password-bcrypt": "1.1.0",
    "roots/acorn": "^2.0.6",

In that case I would (assuming you are already using VCS/git for that Bedrock site/project) create a new, empty Bedrock project (composer create-project roots/bedrock) and then overwrite the existing Bedrock project, merge everything and you should have the latest dependencies and support files.

For the sake of clarity, can you explain exactly what happens? i.e.:

  • What command are you trying to run?
  • What is the error/failure output? Is it the same for all commands?
  • How are you invoking WP-CLI, and from what directory?

Even trying to run ‘wp’ to get the help screen fails with the error below (this is the same error whatever wp command we run – i.e. wp plugin activate etc)

We’re invoking wp cli from the project folder (roots/bedrock setup with a roots/sage theme). WP cli works fine outside of the project directory.

Fatal error: Uncaught Error: Class 'Dotenv\Environment\DotenvFactory' not found in /Users/seb/.wp-cli/packages/vendor/illuminate/support/helpers.php:646
Stack trace:
#0 /Users/seb/sites/imaginaire3/vendor/roots/acorn/config/broadcasting.php(18): env('BROADCAST_DRIVE...', 'null')
#1 /Users/seb/sites/imaginaire3/vendor/roots/acorn/src/Roots/Acorn/Bootstrap/LoadConfiguration.php(66): require('/Users/seb/site...')
#2 /Users/seb/sites/imaginaire3/vendor/roots/acorn/src/Roots/Acorn/Bootstrap/LoadConfiguration.php(37): Roots\Acorn\Bootstrap\LoadConfiguration->loadConfigurationFiles(Object(Roots\Acorn\Application), Object(Illuminate\Config\Repository))
#3 /Users/seb/sites/imaginaire3/vendor/roots/acorn/src/Illuminate/Foundation/Application.php(237): Roots\Acorn\Bootstrap\LoadConfiguration->bootstrap(Object(Roots\Acorn\Application))
#4 /Users/seb/sites/imaginaire3/vendor/roots/acorn/src/Roots/Acorn/Bootloader.php(208): Illuminate\Foundation\Application->bootstrapWith(Array)
#5 /Users/seb/sites/imaginaire3/vendor/roots/acorn/src/Ro in /Users/seb/.wp-cli/packages/vendor/illuminate/support/helpers.php on line 646

Thanks but I tried this, works fine right until we drop the theme into the installation – it’s really weird as the theme shouldn’t affect it and doesn’t use dotenv as a dependency or anything.

Well, according to the stack trace you posted it actually does.

So illuminate/support has a PHP file helpers.php that actually tries to use the class Dotenv\Environment\DotenvFactory.
When you go one level down the stacktrace you see that roots/acorn ( 2.0.6 ) broadcasting.php called the part from helpers.php:

And indeed, roots/acorn 2.0.6 has illuminate/support version range ^8.70 as dependency:
https://github.com/roots/acorn/blob/b6eeae53bcac7d321b06b9fb4316c67240e8b48b/composer.json#L52

illuminate/support on packagist.org: https://packagist.org/packages/illuminate/support#v8.70.1

illuminate/support v8.70.1 on GitHub:
https://github.com/illuminate/support/tree/v8.70.1

illuminate/support v8.70.1 helpers.php:
https://github.com/illuminate/support/blob/88939afba5b60d6bd53084ee0bd06c5e2534501f/helpers.php
And that’s strange: In the error line 646 is mentioned at where the non-existing Dotenv\Environment\DotenvFactory class is attempted to be used,
because the helpers.php of illuminate/support version v8.70.1 only has a total of 377 lines.

illuminate/support doesn’t require vlucas/phpdotenv but at least suggests that package within version range ^5.4.1:
https://github.com/illuminate/support/blob/3c1ab546c7de1f3176060b1d4d28feb100d0a37b/composer.json#L50

And the only version of illuminate/support I found that has at least 646 lines is v5.8.36:
https://github.com/illuminate/support/blob/df4af6a32908f1d89d74348624b57e3233eea247/helpers.php

So one version of illuminate/support is required (8.70.1), but another one is used (5.8.36 or similar in 5.8.x).

Very similar discussion here:
https://discourse.roots.io/t/php-fatal-error-dotenv-not-found-cannot-use-wp-cli-because-of-it-any-ideas/18844/2

Yeah, it’s just really strange as I tried creating a completely new bedrock instance (using composer create-project), wp cli worked, then when I try and activate a new Sage instance I receive a warning to install Acorn. As soon as I install acorn, the error returns?

Here’s the composer line requiring acorn – I’m wondering if there’s a known conflict between acorn and wp cli?

"roots/acorn": "^2.0"

You ever get anywhere with this. I have a working Sage10 theme on LocalWP but when i add it to my server I get this exact same error message.