How to have Assetic + sass work with Symfony2 in Windows?

11,730

Solution 1

Please see my answer to the question How to use SCSS filter in Symfony2 under Windows?: https://stackoverflow.com/a/9658410/3765

It is a pretty comprehensive coverage of my time spent trying to get Assetic working on Windows.


Based on your example - you shouldn't need to add the cssrewrite filter. Here is how I use Compass from Twig:

{% stylesheets filter='compass' output='css/compiled/*.css' 'css/example.scss' %}
    <link rel="stylesheet" href="{{ asset_url }}">
{% endstylesheets %}

Solution 2

Just as an update to this question (or for anyone only using sass not compass) if anyone comes here looking for the answer. All that should be needed now is:

assetic:
    filters:
        cssrewrite: ~
        sass: 
          bin: "<pathtosass>"
          apply_to: "\.scss$"

parameters:
     assetic.ruby.bin: '<pathtoyourrubyhere>'

This is using PHP 5.4

Share:
11,730

Related videos on Youtube

mlarcher
Author by

mlarcher

Updated on June 04, 2022

Comments

  • mlarcher
    mlarcher almost 2 years

    I'm trying to get Assetic to run within Symfony 2.0.11 in order to use sass for css files.

    I've been fiddling around that issue for hours, and from what I gather the current assetic version (1.0.2) is screwed up and just can't work in windows.

    I tried updating assetic and asseticBundle to the latest Master version from github, but those require Symfony 2.1 which brings in a lot of changes and is not backward compatible (not to mention it's not officially released either), so that's not an option.

    This post symfony2 assetics yui compressor on windows (path syntax) gives some good insight on the matter, but unfortunately the changes on the assetic lib it recommends aren't enough to make it work properly in my environment.

    I found some interesting bits also in https://github.com/kriswallsmith/assetic/commit/5621cd449b0d85316e5872d672e7e900edc2246c and https://github.com/kriswallsmith/assetic/issues/25

    In the end, what I know is that I need to alter vendor/assetic/src/Assetic/Util/ProcessBuilder.php, maybe some other files in the package, and perhaps add some informations in my config.yml file (it seems adding the path to java or sass might help).

    What I need is for someone who was able to get a working configuration to give me pointers onto what I'm missing, or ideally the steps required to have that damn thing work.

    Anyone up to the task ?

    _____Update: Still digging, it's only been 3 days now :/

    New elements :

    1/ php5.3.8 may have an issue with proc_open and data > 2048 bytes as per https://bugs.php.net/bug.php?id=60120 This issue can have implications when getting symfony from the git repository ( https://github.com/symfony/symfony/issues/3216 ). issuing "git config --global core.autocrlf input" and re-fetching everything from git allowed me to use php5.3.10 without crash.

    2/ https://github.com/kriswallsmith/assetic/commit/5621cd449b0d85316e5872d672e7e900edc2246c looks like a decent basis. I've maded some minor modifications on the paths in Process.php (cf https://github.com/kriswallsmith/assetic/issues/92 ) but I'm not really sure that is relevant. Anyhow, the command line I get from Assetic at this point works in a shell and outputs the expected css.

    3/ sass.bat (located in Ruby193\bin) needs to have absolute path to ruby.exe in order to go a bit further (I learnt that the hard way, you only see the error message if you var_dump and kill the script at the right place!)

    Now, things seem a bit better, but still not working with sass (I think it'd work fine with some other filters) I managed to isolate the issue in Process.php (around line 172) at "$data = fread($pipe, 8192);" : The second time the script passes at this place, with $pipe pointing to the second resource, it never returns... and php gets stuck and has really hard times coming out (I need to kill/restart wamp at least two times to be able to make another test)

    I'm really not familiar with proc_open and streams, and I have difficulties understanding what the code is trying to do in there...

    I hope this can help the next one trying to have things work, and eventually help my case too. Still looking for some support on the matter!

    _____Update:

    Further testing made me realize that I could have "php app/console assetic:dump" work when the sass file was small enough. It seems to me that the actual css rules (excluding variable definitions and mixins) need to me smaller than 4096 bytes with php5.3.10, even less with php 5.3.8. That is pointing to the proc_open bug described in https://bugs.php.net/bug.php?id=60120 and https://bugs.php.net/bug.php?id=51800

    _____Update:

    I tried to install php5.4 to check if it was fixing the issue. It took me some time to realize the reason this version was not working on my computer is because there is no x64 build yet. I then installed the 32bits version of wamp, and got php5.4 working with it easily. End result : proc_open still hangs :( I'm beginning to be out of ideas here...


    Walkthrough:

    So, eventually I got this to work, and thought I'd try to sum the most important steps up for later viewers :

    1. Have Ruby 1.9.3 and compass 0.12 installed

    check http://rubyinstaller.org/downloads/

    Update compass to 0.12 with

    "gem update --system"
    

    and then

    "gem install compass"
    

    2. Alter compiler.rb in compass

    Go to Ruby193\lib\ruby\gems\1.9.1\gems\compass-0.12.0\lib\compass in line 10, replace

    self.from, self.to = File.expand_path(from), to
    

    with

    self.from, self.to = from.gsub('./', ''), to
    

    /!\ with some setup, it may be the other way around, but with the setup I'm trying to describe, it goes this way.

    3. point Assetic to the latest version that works with symfony 2.0.11

    edit the deps file like such :

    [assetic]
        git=http://github.com/kriswallsmith/assetic.git
        ;version=v1.0.2
        version=ac71449e46bed22c276da26bf54ab2f733b3801d
    [AsseticBundle]
        git=http://github.com/symfony/AsseticBundle.git
        target=/bundles/Symfony/Bundle/AsseticBundle
        ;version=v1.0.1
        version=da4a46ce37557dcf3068b8493b12bdbbe47455e2
    

    /!\ you'll need to remove the references to a specific version in the deps.lock file too !

    and issue a "php bin/vendors install".

    4. Change your config.yml

    here's what mine looks like now :

    # Assetic Configuration
    assetic:
        debug:          %kernel.debug%
        use_controller: false
        # java: /usr/bin/java
        filters:
            compass:
                bin: e:\outils\Ruby193\bin\compass.bat
    

    5. Use absolute path in compass.bat/sass.bat

    Go to your Ruby193\bin directory and edit compass.bat to set an absolute path to ruby.exe (do the same with sass.bat while you're at it)

    6. Change the call in template

    Here's what mine looks like now :

    {% stylesheets filter='compass' output='css/*.css'
            '@LndBimBundle/Resources/public/css/main.scss'
        %}
            <link href="{{ asset_url }}" type="text/css" rel="stylesheet" />
        {% endstylesheets %}
    

    Testing :

    For testing, you can use

    php app/console assetic:dump --verbose --no-debug
    

    That way if you get into an endless loop as I did, you can just ctrl+C, which makes testing way more efficient than in a browser

    /!\ You absolutely need to use --no-debug, or you'll end up with a bunch of unwanted lines in your css that look like :

    @media -sass-debug-info{filename{font-family:file\:\/\/C\:\/Users\/Mattso\/AppData\/Local\/Temp\/ass9DBF\.tmp\.scss}line{font-family:\0000359}}
    

    You can use

    die($this->commandline);
    

    in the run method of

    vendor\assetic\src\Assetic\Util\Process.php
    

    to show you the current command line, and test it.

    Mine currently looks like this :

    cmd /V:ON /E:ON /C ""e:\outils\Ruby193\bin\compass.bat" "compile" "C:\Users\Mattso\AppData\Local\Temp" "--config" "C:\Users\Mattso\AppData\Local\Temp\ass59BB.tmp" "--sass-dir" "" "--css-dir" "" "C:\Users\Mattso\AppData\Local\Temp\ass59BC.tmp.scss""
    

    Then you just need to type "php app/console assetic:dump --no-debug" in your cli to create the css files in web\css and refresh your site's page. Et... voila! (hopefully!)

    Note: As you may have noticed, I'm using the CompassFilter instead of the SassFilter. That is because it does the same thing (and more) and can actually be made to do what we expect of it. If anyone can find out how to fix the SassFilter, that would be swell. Meanwhile I already spent way too much time on this.

    I've been pulling my hair for days on that crazyness, I hope this post will be helpful to some other people's mental health ;)

  • mlarcher
    mlarcher about 12 years
    #3 is not clear at all. Which line is intended to be present ? I currently have the second one in compiler.rb. Also, adding the commit numbers in the deps file and refetching the vendors, I ended up with current 1.0.2, not the particular target commit. I had to get them manually. In the end, there isn't any info I wasn't aware about in your post and it doesn't allow me to get any further :/ Which version of php do you use ? With what version of windows ? Maybe that can make a difference...
  • leek
    leek about 12 years
    @Matt: In #3, the second line is the line intended to be added. I use Compass 0.12.rc.1 which may explain the differences. Also, if you plan on changing versions of repositories in your deps file - you must delete deps.lock first.
  • leek
    leek about 12 years
    @Matt: Other info - I use PHP 5.3.10 on Windows 7 x64
  • mlarcher
    mlarcher about 12 years
    I have the same setup, and things do not work. I actually run php 5.3.10 on windows 7 x64 with a 32 bit wamp. My Ruby\bin path is in the windows env PATH variable (#1}, %ruby%\bin\compass.bat and %ruby%\bin\sass.bat have a hardcoded path to ruby.exe (#2), %ruby%\lib\ruby\gems\1.9.1\gems\compass-*\lib\compass\compil‌​er.rb (#3) is ok (but I don't really need compass afaik), and assetic/AsseticBundle are upgraded to the commits you suggest (#5). About #4, my config/view are visible on pastebin.com/n6mWscnY ... Any idea what makes a difference between your setup and mine ??
  • mlarcher
    mlarcher about 12 years
    are you running wamp in 32 or 64 bit version ? How big is your scss file ?
  • leek
    leek about 12 years
    @Matt: I may have missed it, but what is the exact error/issue you are seeing? Would you mind posting the output from whatever command/method isn't working for you? Also, I'm using 32-bit versions of both Apache (2.4.1) and PHP.
  • leek
    leek about 12 years
  • mlarcher
    mlarcher about 12 years
    Right now I am testing with "php app/console assetic:dump --verbose", and there is no error. It just gets stuck in an endless loop. I tried removing the cssrewrite reference, but didn't get any better result.
  • mlarcher
    mlarcher about 12 years
    during the chat with leek, I started using the compass filter instead of sass filter, updated it to 0.12, and eventually got it to work. Crucial difference from his step by step on stackoverflow.com/a/9658410/3765 : I needed "self.from, self.to = from.gsub('./', ''), to" in compiler.rb, when it was the other way around for him. We believe it is related to the ruby version (1.9.3 vs 1.9.2) or compass version... Anyway, all seems fine at last now. Big up to leek ;)
  • Ryan
    Ryan almost 12 years
    I'm using Zend and can't find a config.yml. Thoughts on stackoverflow.com/questions/11054715/…?