Files in subdirectories cause parser crash in 1.3
This is a more precise definition for issue #716:
My test case:
I have a file imported from a subdirectory:
@import "ui/gameplay";
When I create an error in the file, it throws a LessError, but LessError itself throws an error, because it is incorrectly pointing to "gameplay.less" instead of "ui/gameplay.less". I've stepped through the code and all references to this.filename are stripped of the path, so when the error is raised, it can't retrieve the correct index in parser.imports.contents.
I think the discrepancy is here:
Line 230 of less.js: that.contents[path] = contents;
Line 3235 of less.js: var filename = href.match(/([^\/]+)$/)[1];
The latter line strips all filename references of any path information, and so the link between the filename and parser.imports.contents[] is subsequently broken. Since the filename reference is used throughout, not sure what the fix would be without passing in the "root" path directory.
This also implies that filename conflicts might arise if two files in separate directories with the same filename are both imported.
Any chance someone could look at this?
crickets
tumbleweed
Hi guys! @cloudhead recently added myself and @ryancampbell as contributors. We're going through the issues and prioritizing bugs now. Have you found a workaround for this issue that you can share in the meantime?
No, I did spend some time going through the JavaScript, but it looks like there's a lot of changes to make. I don't think those changes would be difficult for @cloudhead or someone familiar with the code, because really, it's just an incorrect file reference being passed to LessError. It needs to be passed the filename plus subdirectory path, and somewhere, that subdirectory is being stripped. However, I don't know the implications offhand of changing the initial file reference, because there's a bunch of OTHER places that seem to depend on that variable being returned without a subdirectory.
So, basically, if a LESS file in a subdirectory throws an error, right now LESS can't report that error, because the error reporting function itself throws an error. And for me, the original error was hard to track down because it wasn't with anything new I'd written. I had inadvertently passed two arguments to a function that was expecting one. Before LESS 1.3, it would ignore the second. Now, it throws a fatal error (which I don't agree with, since it means 1.3 is not backward compatible, but that's the story), and due to this weird bug, the error just compounds itself.
So it was a breaking change + a bug in error handling that causes this. But, I'll see if I have time to take another look this weekend and recommend specific changes. Part of the problem is I know frak all about pull requests / etc, so all I can do right now is try to be as specific as I can be about pointing to the problem, but I don't currently have the tools / knowledge to fix it.
Oh, and thanks @sashasklar and @ryancampbell for your support! I'm sure @cloudhead appreciates it, and I do as well. I think a lot of people have a lot of love for LESS, and community support is one of the great things that can keep a project like this going. I try to answer LESS questions here that I have answers to, and I know it can be time-consuming. So I do value your time, and any time you can devote to fixing this particular bug would be mucho appreciated.
Any progress on this?
Hey @matthewdl, trying to get an update out with some other bugfixes before I tackle this. As you'd expect it's something where I need more familiarity with the codebase before I start digging around with that logic.
I am getting a error similar to this.
@import "../../../../gameplay.less";
It breaks when the tree is more then 4 level deep. Not a very consistent bug. Can reproduce it on mac os lion when less compiler is ran twice. The second time its failing to capture that import file.
Works fine for files which are less then 4 level deep.
if gameplay.less is renamed to gameplay2.less then it again works for the first time.
possible these issues are linked? if not if they can be fixed since some fixes on subdirectories are going on?
So far can only reproduce it on mac os lion, upto date node/npm/less. compiling server side.
I've the same issue here (with bootstrap 2.0.2). Basically it seems they have an error on grid.less.
As there is an error, LessError is called and the first param (e) contains the error. LessError try to get the "input" based on the filename contained on (e). To get it, it search in this array parser.imports.contents[filenames].
And this is the point, it seems the file which contains the error is not added to the array. Then, getInput return undefined and getLocation throws an exception...
Hope it'll help.
@sashasklar and @ryancampbell, are you both able to approve pull requests for LESS? There's quite a backlog of issues that have actually been fixed by other developers, but they rarely get pulled in (not this one, though, as far as I know).
I was able to circumvent this error (I think) in the latest release of Crunch, by not passing a filename parameter to the LESS parser. No idea why that did it, but happy to have a workaround.
Good to know I'm not the only one getting these utterly worthless error messages. This is what happens when fame-hungry Node kids try to write compilers.
@khiltd Necessary personal insult?
@sashasklar @ryancampbell Can we get some updates on pull requests / status of bug fixes? Do you need any extra help dealing with requests? Just wondering how we can get LESS back on track and get things updated quickly.
Hey @matthewdl, I can't speak for @ryancampbell but I've had to bow out of contributing on LESS. I would contact @cloudhead directly if you're interested in the gig.
Okay, so noted.
Glad to finally see what is actually causing this error. Does anyone know if there's a fix available yet?
I tried patching it with
function getInput(e, env) {
if (e.filename && env.filename && (e.filename !== env.filename)) {
var filename = basename(e.filename)
var contents = parser.imports.contents[filename]
if (contents == null) {
return ("INTERNAL ERROR: File " + e.filename + " is not found in parser contents hash!");
}
return contents;
} else {
return input;
}
}
and ironically, just passing back any value at all is enough to resolve some error conditions.
Also created a test project in Play 2.0 demoing the bug.
https://github.com/wsargent/testproject