Skip to content

[Config] Fixing GlobResource when inside phar archive #26845

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

Merged
merged 1 commit into from
Jun 20, 2018

Conversation

vworldat
Copy link
Contributor

@vworldat vworldat commented Apr 6, 2018

Q A
Branch? master
Bug fix? yes
New feature? no
BC breaks? yes if old broken behavior counts as stable
Deprecations? no
Tests pass? no tests yet
Fixed tickets
License MIT
Doc PR N/A

When packaging an Sf4 application as a PHAR archive using globs at various locations (Kernel, services.yaml) most glob files are not found because the glob() PHP method does not support PHAR streams.

Using the regex fallback instead when operating inside PHAR archives fixes the behavior for me.

Examples:

src/Kernel.php::configureContainer():

$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');

Expected behavior: config/services.yaml inside PHAR archive is found and parsed
Actual behavior: the file will not be loaded

config/services.yaml (hard-coded in Kernel without using glob pattern)

    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'

Expected behavior: service classes in src/ will be found and auto-wired
Actual behavior: services are not auto-wired because the class files are not found

@c33s
Copy link

c33s commented Apr 6, 2018

is it really a bc break? looks like a bug fix for me

@ogizanagi
Copy link
Contributor

ogizanagi commented Apr 6, 2018

Actually, this should not be an issue if you warmup the prod cache (and thus build the container) before building your phar (and bundling var/cache/prod in it). The container would be built once and won't need to discover files again, so this is never hit.

As I answered @c33s on Slack, I use the following https://github.com/humbug/box config with no issue to bundle a Symfony app in a phar:

{
  "files": ["config/bundles.php"],
  "finder": [
    { "in": "src" },
    { "in": "var/cache/prod" },
    {
      "name": ["*.php"],
      "exclude": ["Tests", "Tester", "Test", "test", "tests"],
      "in": "vendor"
    }
  ],
  "main": "bin/phar-app",
  "output": "legacy-importer.phar",
  "compression": "GZ",
  "stub": true
}

where bin/phar-app is just a dedicated entrypoint setting the APP_ENV env var and configuring a single command application.

@vworldat
Copy link
Contributor Author

vworldat commented Apr 6, 2018

@ogizanagi you are of course right, if pre-warming the cache is an option this can be used to work around the issue. Nevertheless I think the current behavior is not intended and leads to confusion as well as bad DX. And once someone might want to run a packaged app in debug mode for whatever reason, the pre-warmed cache may not be sufficient anymore.

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Apr 7, 2018
@nicolas-grekas
Copy link
Member

Could you add a test case please?

@c33s
Copy link

c33s commented Apr 10, 2018

example.zip
@vworldat phar fixture file for the tests containing

enc/puppet-enc/build/example.phar
.phar
.phar/signature.bin
.phar/stub.php
bin
bin/console
config
config/packages
config/packages/dev
config/packages/dev/monolog.yaml
config/packages/dev/routing.yaml
config/packages/prod
config/packages/prod/monolog.yaml
config/packages/test
config/packages/test/framework.yaml
config/packages/test/monolog.yaml
config/packages/framework.yaml
config/packages/routing.yaml
config/bundles.php
config/routes.yaml
config/services.yaml
composer.json

@theofidry
Copy link
Contributor

theofidry commented Apr 28, 2018

Could it also be that the application is executed in the wrong environment? I.e. when booting inside the PHAR, it detects that the dumped container is outdated and tries to dump it again (which will fail)

@nicolas-grekas
Copy link
Member

@vworldat would you mind adding a test base, borrowing the fixtures phar from @c33s please?

@nicolas-grekas nicolas-grekas changed the base branch from master to 3.4 June 19, 2018 14:01
When packaging an Sf4 application as a PHAR archive using globs at various locations (`Kernel`, `services.yaml`) most glob files are not found because the `glob()` PHP method [does not support PHAR streams](https://stackoverflow.com/questions/8203188/unexpected-problems-with-php-phar).

Using the regex fallback instead when operating inside PHAR archives fixes the behavior for me.
@nicolas-grekas
Copy link
Member

Thank you @vworldat.

@nicolas-grekas nicolas-grekas merged commit e336ebe into symfony:3.4 Jun 20, 2018
nicolas-grekas added a commit that referenced this pull request Jun 20, 2018
…rldat)

This PR was merged into the 3.4 branch.

Discussion
----------

[Config] Fixing GlobResource when inside phar archive

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | yes if old broken behavior counts as stable
| Deprecations? | no
| Tests pass?   | no tests yet
| Fixed tickets |
| License       | MIT
| Doc PR        | N/A

When packaging an Sf4 application as a PHAR archive using globs at various locations (`Kernel`, `services.yaml`) most glob files are not found because the `glob()` PHP method [does not support PHAR streams](https://stackoverflow.com/questions/8203188/unexpected-problems-with-php-phar).

Using the regex fallback instead when operating inside PHAR archives fixes the behavior for me.

## Examples:

`src/Kernel.php::configureContainer()`:

```php
$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
```

Expected behavior: `config/services.yaml` inside PHAR archive is found and parsed
Actual behavior: the file will not be loaded

`config/services.yaml` (hard-coded in Kernel without using glob pattern)

```yaml
    App\:
        resource: '../src/*'
        exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
```

Expected behavior: service classes in `src/` will be found and auto-wired
Actual behavior: services are not auto-wired because the class files are not found

Commits
-------

e336ebe Fixing GlobResource when inside phar archive
This was referenced Jun 25, 2018
nicolas-grekas added a commit that referenced this pull request May 17, 2022
…source (nicolas-grekas)

This PR was merged into the 4.4 branch.

Discussion
----------

[Config] Fix looking for single files in phars with GlobResource

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

`Finder::in()` only accepts directories as arguments, but `GlobResource` can feed it with files when looking inside a phar file (see #26845) or when using `/**/` in the pattern.

This PR fixes it.

Commits
-------

d4ea072 [Config] Fix looking for single files in phars with GlobResource
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.

6 participants