Skip to content

lessc randomly fails to import all the files #1432

Closed

Description

@glasserc

I have a complicated set of LESS files that evolved without any clear design in mind. In particular, I have member_agreement.less, which imports variables.less, standard_margins.less, links.less, and campaign_strip.less. links.less imports fonts.less, which imports mixins.less, which imports variables.less. campaign_strip.less imports fonts.less and variables.less. (Files available on request.) fonts.less uses a mixin .charcoal defined in mixins.less. Normally that all works fine, but sometimes, randomly, about one time in ten, I get a NameError:

NameError: .charcoal is undefined in /home/ethan/Jobs/Pave/core/web/pilot/static/less/fonts.less on line 93, column 3:
92   .family-graphik;
93   .charcoal;
94 }

I tried to debug this further using --verbose, which was useless. Failing that, I added code to import.js:83:

    console.log("visit", this.path.value);

When lessc runs to completion, I see output like this:

Thu Jul 18 19:42:34 EDT 2013
visit ../variables.less
visit ../modules/standard_margins.less
visit ../modules/links.less
visit ../fonts.less
visit mixins.less
visit variables.less
visit ../modules/campaign_strip.less
visit ../fonts.less
visit ../variables.less
lessc: wrote /home/ethan/Jobs/Pave/core/web/pilot/static/css-output/pages/member_agreement.css

When it breaks, I see this instead:

Thu Jul 18 19:42:36 EDT 2013
visit ../variables.less
visit ../modules/standard_margins.less
visit ../modules/links.less
visit ../fonts.less
visit mixins.less
visit ../modules/campaign_strip.less
visit ../fonts.less
visit ../variables.less

I noticed that variables.less was missing in this second output. Perhaps mixins.less wasn't fully processed?

I'm not a Node developer, though I have some experience with JavaScript. I see that in index.js, there's code like this:

        if (env.syncImport) {
            try {
                data = fs.readFileSync(pathname, 'utf-8');
                parseFile(null, data);
            } catch (e) {
                parseFile(e);
            }
        } else {
            fs.readFile(pathname, 'utf-8', parseFile);
        }

I see that sometimes imports are handled synchronously, but normally they aren't. It's my hypothesis that there is a race condition somewhere in the handling of asynchronous imports. I would try using the syncImport option directly, but it seems this isn't exposed as a command-line argument. To test this hypothesis, I copied the code from the syncImport branch to the other branch, giving:

        if (env.syncImport) {
            try {
                data = fs.readFileSync(pathname, 'utf-8');
                parseFile(null, data);
            } catch (e) {
                parseFile(e);
            }
        } else {
            data = fs.readFileSync(pathname, 'utf-8');
            parseFile(null, data);
        }

When run like this, the name errors go away.

This is on Ubuntu 13.04 (my dev laptop) running npm 1.3.2 with lessc 1.4.1.

Please let me know if I can provide any assistance debugging this issue.

Activity

  1. lukeapage commented on Jul 19, 2013

    @lukeapage
    Member

    Thanks, I see this very, very occasionally in a test failing with a missing chunk of css but I haven't tracked it down.. we should spend more time validating the logic, thanks

  2. rjgotten commented on Aug 7, 2013

    @rjgotten
    Contributor

    Ah. So it is a race condition after all. I was bitten by what seems to be this bug several times over the course of today and not very, very occasionally at all: it reproduces once every 10 or so compiles at the least for me.

    I'd certainly appreciate a formal patch.
    In the mean time, I guess I'll be bruteforcing a fix by copying the sync branch as mentioned in the issue report.

  3. lukeapage commented on Aug 7, 2013

    @lukeapage
    Member

    If you have an example where I can reproduce even 1 in 10, that would help.

  4. rjgotten commented on Aug 8, 2013

    @rjgotten
    Contributor

    If you have an example where I can reproduce even 1 in 10, that would help.

    It looks like you can trigger it fairly consistenly if you import a less sheet containing only another import of a less sheet, which in turn only contains parametrized mixins.

    As soon as I added an unparametrized CSS selector to the intermediate sheet in that set of 3, the problem seemed to disappear. (Something like *{} works and still doesn't output any real CSS because it sets no properties.)

    My guess is you are switching to an asynchronous file read for imports in a less sheet when the parsed less sheet doesn't directly need any of the dependencies it imports, but do not check if the ancestor sheet requires its imports to be satisfied synchronously.

  5. glasserc commented on Aug 9, 2013

    @glasserc
    Author

    I'm not as sure of the mechanism of action as @rjgotten. I have a test case as well -- unfortunately it's seven different files, and I can't attach a tarball to this issue. It's based on my real code, but hopefully much shorter.

    First is main.less:

    @import "variables.less";
    @import "module-1.less";
    @import "module-2.less";
    @import "module-3.less";

    Second is variables.less:

    @colorFoo: #123456;

    Third is module-1.less:

    @import "fonts.less";
    
    .module-1-foo {
      .my-font;
    }

    Fourth is fonts.less:

    @import "mixins.less";
    
    .my-font {
      font-family: "Graphik Web",Helvetica,Arial,sans-serif;
      .mixin-foo;
    }

    Fifth is mixins.less:

    @import "variables.less";
    
    .mixin-foo {
      color: @colorFoo;
    }

    Sixth is module-2.less -- note that it's almost exactly the same as module-1.less.

    @import "fonts.less";
    
    .module-2-foo {
      .my-font;
    }

    Last is module-3.less, which is also almost exactly the same as modules 1 and 2.

    @import "fonts.less";
    
    .module-3-foo {
      .my-font;
    }

    At the command line, I run:

    $ while lessc main.less ; do date; done
    

    I get:

    NameError: .mixin-foo is undefined in /home/ethan/tmp/break-lessc/fonts.less on line 5, column 3:
    4   font-family: "Graphik Web",Helvetica,Arial,sans-serif;
    5   .mixin-foo;
    6 }
    

    I haven't tried to reduce this test case that much. I did try to remove modules 2 and 3. With neither module, the while loop continues for a long while without failing. With both modules, it fails pretty quickly. With just module 2, it fails but it takes longer.

    Hope this helps. Thanks for looking into this!

  6. lukeapage commented on Aug 15, 2013

    @lukeapage
    Member

    Thanks for the info, I could reproduce and then with lots of console.logs I tracked it down.

    The issue was that we were importing the whole tree and importing sub nodes even if they were going to be skipped.. so..

    fonts.less Skipped
    mixins.less <-- not skipped
    fonts.less <-- not skipped
    mixins.less Skipped

    but if you skip one file, it skips sub files, so it essentially wasn't including mixins.

    The fix is an extra check on the if, so it does this

    fonts.less Skipped
    mixins.less <-- not even imported
    fonts.less <-- not skipped
    mixins.less <-- not skipped

  7. lukeapage commented on Aug 15, 2013

    @lukeapage
    Member
  8. lukeapage commented on Aug 15, 2013

    @lukeapage
    Member

    If you would like to try it out, you can do this
    npm install -g https://github.com/less/less.js/tarball/1.5.0-wip to upgrade globally (or without -g if you want to try it out with node node_modules/less.js/bin/lessc)

  9. rjgotten commented on Aug 19, 2013

    @rjgotten
    Contributor

    but if you skip one file, it skips sub files, so it essentially wasn't including mixins

    So why did switching to synchronous imports fix the problem as well? Do synchronous imports take a completely disjoint code path that didn't suffer this bug?

    [EDIT]
    Ah wait... These import instructions are pushed onto a stack... So you must be popping them somewhere else? A reverse order combined with synchronous (i.e. blocking) imports would kind of skirt around the issue, wouldn't it?

  10. lukeapage commented on Aug 19, 2013

    @lukeapage
    Member

    synchronous mean it acts like a deep first tree and always takes the same
    route, instead of it being random what returns first, so yes it avoids the
    issue.

  11. glasserc commented on Aug 19, 2013

    @glasserc
    Author

    My minimal testing seems to show the problem as being fixed. Thanks so much!

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions