Skip to content

[DI] Use GlobResource for non-tracked directories #23870

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

Conversation

vudaltsov
Copy link
Contributor

Q A
Branch? 3.3
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

I noticed that some of my excluded directories are still tracked by the container and changes to files inside them make it recompile. This brought me to the $trackContents || is_dir($path) line introduced in #21505.

@nicolas-grekas
Copy link
Member

I think the existing logic is correct in its intent to track only for new and deleted files, but not for changes.
BUT, most IDE annihilate that effort by creating a temporary file when saving a file.
If we merge this PR, we will introduce a new bug: deleted/new files won't be tracked anymore.
This means we should find a better way to track new/removed files only.

@vudaltsov
Copy link
Contributor Author

vudaltsov commented Aug 11, 2017

Then I probably misunderstood the mechanics.

Here's how to reproduce my issue:

symfony --quiet demo test
cd test
# run symfony to warm up cache
bin/console --quiet about
# see container modification time
date -r var/cache/dev/appDevDebugProjectContainer.php
# this is to see later the difference in seconds
sleep 2
# create a twig file
touch app/Resources/views/test.html.twig
# run command again and I expect here the container to be not recompiled
bin/console --quiet about
# it actually recompiles, because the modification time is now different
date -r var/cache/dev/appDevDebugProjectContainer.php

This happens because of this call in TwigExtension which creates a FileResource for the app/Resources/views directory although $trackContents = false.

Am I right that TwigBundle keeps track of all bundle directories to recompile container in case these directories appear or get removed?

The actual problem is that dx becomes worse when you are waiting for n seconds after just changing a template...

@vudaltsov
Copy link
Contributor Author

vudaltsov commented Aug 11, 2017

With current code there's no possibility to prevent directory tracking even by doing ContainerBuilder::fileExists('/path/to/dir', false). Is this valid?

@vudaltsov
Copy link
Contributor Author

vudaltsov commented Aug 12, 2017

@nicolas-grekas , I've checked, with my changes new and deleted files are still tracked correctly.
But the Twig issue disappears

@jvasseur
Copy link
Contributor

@vudaltsov the problem with your patch is that removing the app/Ressources/views will non longer trigger a recompile of the container leading to a fatal error because twig throw an exception when adding an inexistent path to the FileSystem loader.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 13, 2017

@vudaltsov can you try this?

--- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php
+++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php
@@ -407,9 +407,13 @@ class ContainerBuilder extends Container implements TaggedContainerInterface
             return $exists;
         }
 
-        if ($trackContents && is_dir($path)) {
-            $this->addResource(new DirectoryResource($path, is_string($trackContents) ? $trackContents : null));
-        } elseif ($trackContents || is_dir($path)) {
+        if (is_dir($path)) {
+            if ($trackContents) {
+                $this->addResource(new DirectoryResource($path, is_string($trackContents) ? $trackContents : null));
+            } else {
+                $this->addResource(new GlobResource($path, '/*', false));
+            }
+        } elseif ($trackContents) {
             $this->addResource(new FileResource($path));
         }

If it works, you can take the patch for your PR (and add a test case if possible.)

@vudaltsov
Copy link
Contributor Author

@nicolas-grekas, this makes things better. Now editing existing templates doesn't make the container recompile. New templates still do. Which makes sense because GlobResource notices the change in the file structure.

Is it possible to improve it even more? As @jvasseur pointed out, we only need to track for directory's existence in case of TwigExtension.

I'll try to experiment a little with this next week.

@nicolas-grekas nicolas-grekas force-pushed the di-container-builder-track-contents branch from 8d0a48b to e9ea24f Compare August 22, 2017 11:10
@nicolas-grekas nicolas-grekas changed the title [DI] Fix for unintentionally tracked directories [DI] Use GlobResource for non-tracked directories Aug 22, 2017
@nicolas-grekas nicolas-grekas force-pushed the di-container-builder-track-contents branch from e9ea24f to 048eb18 Compare August 22, 2017 11:12
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.

@vudaltsov I fixed resource tracking for TwigBundle to be a straight file existence check.

@vudaltsov
Copy link
Contributor Author

@nicolas-grekas , awesome news! Thank you. I din't have time to look at this yet.

@nicolas-grekas
Copy link
Member

Thank you @vudaltsov.

@nicolas-grekas nicolas-grekas merged commit 048eb18 into symfony:3.3 Aug 22, 2017
nicolas-grekas added a commit that referenced this pull request Aug 22, 2017
This PR was merged into the 3.3 branch.

Discussion
----------

[DI] Use GlobResource for non-tracked directories

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

I noticed that some of my excluded directories are still tracked by the container and changes to files inside them make it recompile. This brought me to the `$trackContents || is_dir($path)` line introduced in #21505.

Commits
-------

048eb18 [DI] Use GlobResource for non-tracked directories
@fabpot fabpot mentioned this pull request Aug 28, 2017
@vudaltsov vudaltsov deleted the di-container-builder-track-contents branch December 14, 2018 10:26
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.

4 participants