8000 kernel.project_dir (getProjectDir()) silently returns incorrect path without composer.json · Issue #23950 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

kernel.project_dir (getProjectDir()) silently returns incorrect path without composer.json #23950

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

Closed
spaivaras opened this issue Aug 22, 2017 · 8 comments

Comments

@spaivaras
Copy link
Q A
Bug report? yes
Feature request? no
BC Break report? no
Symfony version 3.3.x

Currently in symfony 3.3.x parameter kernel.project_dir silently returns kernel_dir if composer.json is not found at all. IMHO it shouldn't be related with composer.json as in prod environment, composer is typically not needed at all, so files could be discarded from build artifacts. So even an error or a warning would be better here compared to option that you can get 2 different paths depending on that you have or not have a 3rd party file.

even cache:clear fails with missing files errors, if project_dir is used without composer.json file

PHP Fatal error:  Uncaught Symfony\Component\Config\Exception\FileLocatorFileNotFoundException: The file "/var/www/releases/20170822132058/app/vendor/willdurand/geocoder/Dumper" does not exist. in /var/www/releases/20170822132058/vendor/symfony/config/FileLocator.php:46
111
@stof
Copy link
Member
stof commented Aug 22, 2017

Well, if you don't want it to depend on the presence of the composer.json, you can overwrite the method in your own kernel.
Harcoding the nesting level of your own kernel class inside your project structure in Symfony itself cannot work (the kernel can be in different places). Finding the composer.json file made it easy to build a generic implementation working for all people not removing the composer.json.
However, inside your own AppKernel, implementing a project-specific retrieval not bound to the composer.json is dead simple (it will be return dirname(__DIR__); if you use the structure of the standard edition)

@nicolas-grekas
Copy link
Member

I agree with @stof (btw, you're not the only one cleaning composer.json, and this solution was provided before and was OK.)

@xabbuh
Copy link
Member
xabbuh commented Aug 22, 2017

I agree with @stof and @nicolas-grekas. Thus, closing here as there is an easy way to override the project dir config if needed.

@xabbuh xabbuh closed this as completed Aug 22, 2017
@mvrhov
Copy link
mvrhov commented Aug 23, 2017

So why is the projectDir variable private then? Why do I have to re-declare it in my kernel?

@chalasr
Copy link
Member
chalasr commented Aug 23, 2017

@mvrhov the kernel.project_dir parameter uses the return value of Kernel::getProjectDir(), override this method to set a custom path.

@mvrhov
Copy link
mvrhov commented Aug 23, 2017

rootDir variable is protected.. so I can set it once and next time just return the cached value.
For projectDir I have to redeclare the variable in a child to do the same. That's why I'm asking.

@stof
Copy link
Member
stof commented Aug 23, 2017

@mvrhov rootDir being protected is a legacy reason. We cannot make it private due to the change being a BC break. but the new property can be private without issue.
The support way to customize this has always be to overwrite the method (even for rootDir, the doc talked about overriding the getter)

@chs2
Copy link
Contributor
chs2 commented Mar 18, 2019

Hi,

I just hit the same problem on production and I wondered, now that ".env" is required by public/index.php, if it couldn't be added to the test in Kernel::getProjectDir() with "composer.json" ?

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Kernel.php#L347

while (!file_exists($dir.'/composer.json') && !file_exists($dir.'/.env')) {
/* … */
}

symfony-splitter pushed a commit to symfony/http-kernel that referenced this issue Mar 23, 2019
This PR was merged into the 4.3-dev branch.

Discussion
----------

Allow user to set the project dir

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |  <!-- symfony/symfony-docs#... required for new features -->

Currently, the project directory is defined by the location of the composer.json file.

That file is not required in production, which therefore [breaks the method getProjectDir](symfony/symfony#23950) (who sends back null).
The offered solution, while working, requires the developer to implement it, and uses inheritance override, while a more aesthetic solution could be used.

This does not fix the behaviour, but allows the developer to pass the project dir as a parameter.

While this solution does not include BC break or anything, it is important to notice that it includes
**an optional parameter**.

[Object instantiation in the framework bundle recipe](https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.2/public/index.php#L23) could be updated as follow (in another PR):

```php
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
```

```php
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG'], dirname(__DIR__));
```

Commits
-------

c40017d Allow user to set the project dir
fabpot added a commit that referenced this issue Mar 23, 2019
This PR was merged into the 4.3-dev branch.

Discussion
----------

Allow user to set the project dir

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |  <!-- symfony/symfony-docs#... required for new features -->

Currently, the project directory is defined by the location of the composer.json file.

That file is not required in production, which therefore [breaks the method getProjectDir](#23950) (who sends back null).
The offered solution, while working, requires the developer to implement it, and uses inheritance override, while a more aesthetic solution could be used.

This does not fix the behaviour, but allows the developer to pass the project dir as a parameter.

While this solution does not include BC break or anything, it is important to notice that it includes
**an optional parameter**.

[Object instantiation in the framework bundle recipe](https://github.com/symfony/recipes/blob/master/symfony/framework-bundle/4.2/public/index.php#L23) could be updated as follow (in another PR):

```php
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']);
```

```php
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG'], dirname(__DIR__));
```

Commits
-------

c40017d Allow user to set the project dir
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
0