Repository navigation
Description
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
lukeapage commentedon Jul 19, 2013
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
rjgotten commentedon Aug 7, 2013
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.
lukeapage commentedon Aug 7, 2013
If you have an example where I can reproduce even 1 in 10, that would help.
rjgotten commentedon Aug 8, 2013
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.
glasserc commentedon Aug 9, 2013
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:
Second is variables.less:
Third is module-1.less:
Fourth is fonts.less:
Fifth is mixins.less:
Sixth is module-2.less -- note that it's almost exactly the same as module-1.less.
Last is module-3.less, which is also almost exactly the same as modules 1 and 2.
At the command line, I run:
I get:
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!
lukeapage commentedon Aug 15, 2013
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
lukeapage commentedon Aug 15, 2013
a10be4d
lukeapage commentedon Aug 15, 2013
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-wipto upgrade globally (or without -g if you want to try it out withnode node_modules/less.js/bin/lessc)rjgotten commentedon Aug 19, 2013
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?
lukeapage commentedon Aug 19, 2013
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.
glasserc commentedon Aug 19, 2013
My minimal testing seems to show the problem as being fixed. Thanks so much!