Skip to content

Conversation

ed-fruty
Copy link

Q A
Branch? 4.0
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR symfony/symfony-docs#

I don't think it's a big bug, but it's a case when symfony wasn't work properly.
When you have a specific env name, you can't even install symfony correctly.
Steps to reproduce:

export APP_ENV=dev-server
composer create-project symfony/skeleton symfony_will_crash

And you will get an error [KO] [KO] Script cache:clear returned with error code 255 !! PHP Fatal error: Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Undefined constant 'ContainerCPr692s\srcDev' in /home/project/symfony_will_crash/var/cache/dev-server/srcDev-serverDebugProjectContainer.php:5.

Full installation log will be like this:

Installing symfony/skeleton (v4.0.5)
  - Installing symfony/skeleton (v4.0.5): Loading from cache
Created project in symfony_will_crash
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 21 installs, 0 updates, 0 removals
  - Installing symfony/flex (v1.0.66): Loading from cache
  - Installing symfony/polyfill-mbstring (v1.6.0): Loading from cache
  - Installing symfony/console (v4.0.3): Loading from cache
  - Installing symfony/routing (v4.0.3): Loading from cache
  - Installing symfony/http-foundation (v4.0.3): Loading from cache
  - Installing symfony/yaml (v4.0.3): Loading from cache
  - Installing symfony/framework-bundle (v4.0.3): Loading from cache
  - Installing symfony/http-kernel (v4.0.3): Loading from cache
  - Installing symfony/event-dispatcher (v4.0.3): Loading from cache
  - Installing psr/log (1.0.2): Loading from cache
  - Installing symfony/debug (v4.0.3): Loading from cache
  - Installing symfony/finder (v4.0.3): Loading from cache
  - Installing symfony/filesystem (v4.0.3): Loading from cache
  - Installing psr/container (1.0.0): Loading from cache
  - Installing symfony/dependency-injection (v4.0.3): Loading from cache
  - Installing symfony/config (v4.0.3): Loading from cache
  - Installing psr/simple-cache (1.0.0): Loading from cache
  - Installing psr/cache (1.0.1): Loading from cache
  - Installing symfony/cache (v4.0.3): Loading from cache
  - Installing symfony/dotenv (v4.0.3): Loading from cache
Writing lock file
Generating autoload files
Symfony operations: 4 recipes (2129a2681f34fe264097ab9136e6141a)
  - Configuring symfony/flex (>=1.0): From github.com/symfony/recipes:master
  - Configuring symfony/framework-bundle (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/console (>=3.3): From github.com/symfony/recipes:master
  - Configuring symfony/routing (>=4.0): From github.com/symfony/recipes:master
Executing script cache:clear [KO]
 [KO]
Script cache:clear returned with error code 255
!!  PHP Fatal error:  Uncaught Symfony\Component\Debug\Exception\FatalThrowableError: Undefined constant 'ContainerCPr692s\srcDev' in /home/project/symfony_will_crash/var/cache/dev-server/srcDev-serverDebugProjectContainer.php:5
!!  Stack trace:
...

The main thing is compiler tries to create container with filename and classname uses our $APP_ENV and in this case it will be srcDev-serverDebugProjectContainer which is not valid for php syntax.

One of suggestions is always to use normalized environment name. In our case it will be devserver.
Another one is throw an exception if env is not valid.
What do think ?

@ed-fruty ed-fruty changed the base branch from master to 4.0 January 24, 2018 14:27
@ed-fruty
Copy link
Author

Regex can be changed for something like /[^\w]+/i or other.

@nicolas-grekas
Copy link
Member

Thanks for the report. Envs have implicit requirements, good idea to enforce them.
Adding a new protected method is a no-go as we don't want this to be extensible.
But normalizing inline when setting the env /OR/ throwing an InvalidArgumentException is legit IMHO.
I'm not sure about the proposed regexp, how did you design it?

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Jan 25, 2018
@dunglas
Copy link
Member

dunglas commented Jan 26, 2018

Good catch, the format should be enforced, this kind of issues is a pain to debug, especially for newcomers. I suggest to throw an exception specifying the valid characters instead of normalizing (the normalization introduces some complexity, and probably some more WTF later).

@ed-fruty
Copy link
Author

ed-fruty commented Jan 27, 2018

@dunglas, @nicolas-grekas I suggest to normalize environment value instead of exception.
I reviewed kernel code again and found almost the same normalization for kernel name getter.

So, it would be strangeous behaviour when in one place would be normalization and in another one - exception.

Can I extract normalization for this two places in separate private method and then update this PR ?

@nicolas-grekas
Copy link
Member

@ed-fruty go on my side for normalization, but with a BC-safe regular expression:
$this->environment = preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', $environment);
the normalization in place for the name should not be changed, and thus no need for any private method, just this line in the constructor is enough IMHO.

@nicolas-grekas
Copy link
Member

wait, actually this patch would be better:

--- a/src/Symfony/Component/HttpKernel/Kernel.php
+++ b/src/Symfony/Component/HttpKernel/Kernel.php
@@ -480,7 +480,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
      */
     protected function getContainerClass()
     {
-        return $this->name.ucfirst($this->environment).($this->debug ? 'Debug' : '').'ProjectContainer';
+        return $this->name.ucfirst(preg_replace('/[^a-zA-Z0-9_\x7f-\xff]/', '', $this->environment)).($this->debug ? 'Debug' : '').'ProjectContainer';
     }
 
     /**

@ed-fruty
Copy link
Author

ed-fruty commented Jan 30, 2018

Updated PR.
As I understood checks were failed due to http connection errors.

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

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

Looks good to me at the fix side. Tests need to be implemented, and this PR should be retargetted/rebased at branch 2.7, where the issue also exists. As PHP 5.3 is the minimum version there, PHP 7 syntax cannot be used.

@@ -523,6 +523,44 @@ public function testProjectDirExtension()
$this->assertSame('foo', $kernel->getContainer()->getParameter('kernel.project_dir'));
}

/**
* @param $env
* @param $expected
Copy link
Member

Choose a reason for hiding this comment

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

pleas remove both @param

$kernel = new class($env, true) extends Kernel {
public function registerContainerConfiguration(LoaderInterface $loader)
{
// TODO: Implement registerContainerConfiguration() method.
Copy link
Member

Choose a reason for hiding this comment

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

TODO :) (same below)

@Tobion
Copy link
Contributor

Tobion commented Feb 1, 2018

The environment is also used for the cache folder. So certain special characters will make this fail as well, esp. on Windows. So that sohuld be fixed as well if we want make env chars failure tolerant.

@ed-fruty ed-fruty changed the base branch from 4.0 to 2.7 February 2, 2018 13:59
@ed-fruty ed-fruty changed the base branch from 2.7 to 4.0 February 2, 2018 14:27
@ed-fruty
Copy link
Author

ed-fruty commented Feb 2, 2018

I create my branch from 4.0 and now I can't rebase it to 2.7. I getting errors in git, related to unresolved conflicts...
Can I create another one branch based on 2.7 with my fixes and create another PR ?

@nicolas-grekas
Copy link
Member

Can I create another one branch based on 2.7 with my fixes and create another PR ?

sure, if you're more comfortable this way no pb!

@nicolas-grekas nicolas-grekas changed the base branch from 4.0 to 2.7 March 19, 2018 20:27
@nicolas-grekas nicolas-grekas changed the base branch from 2.7 to 4.0 March 19, 2018 20:34
@nicolas-grekas
Copy link
Member

Closing as this needs to be submitted on 2.7 (and tests be updated to work on PHP 5.3)
@ed-fruty waiting for your PR :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants