Fix for issue #592, which hides the original error message with one generated by the error handling logic. #703

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+30 −17
Split
View
@@ -121,6 +121,7 @@ function loadStyleSheets(callback, reload) {
}
function loadStyleSheet(sheet, callback, reload, remaining) {
+ var contents = sheet.contents || {}; // Passing a ref to top importing parser content cache trough 'sheet' arg.
var url = window.location.href.replace(/[#?].*$/, '');
var href = sheet.href.replace(/\?.*$/, '');
var css = cache && cache.getItem(href);
@@ -147,11 +148,13 @@ function loadStyleSheet(sheet, callback, reload, remaining) {
} else {
// Use remote copy (re-parse)
try {
+ contents[filename] = data; // Updating top importing parser content cache
new(less.Parser)({
optimization: less.optimization,
paths: [href.replace(/[\w\.-]+$/, '')],
mime: sheet.type,
- filename: filename
+ filename: filename,
+ 'contents': contents // Passing top importing parser content cache ref down.
}).parse(data, function (e, root) {
if (e) { return error(e, href) }
try {
View
@@ -93,7 +93,8 @@ less.Parser.importer = function (file, paths, callback, env) {
// TODO: Undo this at some point,
// or use different approach.
- paths.unshift('.');
+ var paths = [].concat(paths); // Avoid passing paths by reference down the import tree...
+ paths.unshift('.'); // ...which results on a lot of repeated '.' paths.
for (var i = 0; i < paths.length; i++) {
try {
@@ -109,11 +110,13 @@ less.Parser.importer = function (file, paths, callback, env) {
fs.readFile(pathname, 'utf-8', function(e, data) {
if (e) return callback(e);
+ env.contents[pathname] = data; // Updating top importing parser content cache.
new(less.Parser)({
paths: [path.dirname(pathname)].concat(paths),
- filename: pathname
+ filename: pathname,
+ contents: env.contents
}).parse(data, function (e, root) {
- callback(e, root, data);
+ callback(e, root, data); // TODO: Remove "data" argument? No longer used!
});
});
} else {
View
@@ -65,15 +65,20 @@ less.Parser = function Parser(env) {
var that = this;
+ // Top parser on an import tree must be sure there is one "env"
+ // which will then be passed arround by reference.
+ var env = env || { };
+ if (!env.contents) { env.contents={}; } // env.contents must be passed arround with top env
+
// This function is called after all files
// have been imported through `@import`.
var finish = function () {};
var imports = this.imports = {
paths: env && env.paths || [], // Search paths, when importing
queue: [], // Files which haven't been imported yet
- files: {}, // Holds the imported parse trees
- contents: {}, // Holds the imported file contents
+// files: {}, // Holds the imported parse trees - TODO: Not used, REMOVE?
+ contents: env.contents, // Holds the imported file contents
mime: env && env.mime, // MIME type of .less files
error: null, // Error in parsing/evaluating an import
push: function (path, callback) {
@@ -83,10 +88,10 @@ less.Parser = function Parser(env) {
//
// Import a file asynchronously
//
+ // TODO: Remove "contents" argument?
less.Parser.importer(path, this.paths, function (e, root, contents) {
that.queue.splice(that.queue.indexOf(path), 1); // Remove the path from the queue
- that.files[path] = root; // Store the root
- that.contents[path] = contents;
+// that.files[path] = root; // Store the root - TODO: Not used, REMOVE?
if (e && !that.error) { that.error = e }
callback(e, root);
@@ -191,17 +196,18 @@ less.Parser = function Parser(env) {
}
}
- function basename(pathname) {
- if (less.mode === 'node') {
- return require('path').basename(pathname);
- } else {
- return pathname.match(/[^\/]+$/)[0];
- }
- }
+// TODO: Removing this - Using basename() here can mess the cache due to name clashes across the import tree!!!
+// function basename(pathname) {
+// if (less.mode === 'node') {
+// return require('path').basename(pathname);
+// } else {
+// return pathname.match(/[^\/]+$/)[0];
+// }
+// }
function getInput(e, env) {
if (e.filename && env.filename && (e.filename !== env.filename)) {
- return parser.imports.contents[basename(e.filename)];
+ return parser.imports.contents[e.filename];
} else {
return input;
}
@@ -1293,7 +1299,8 @@ if (less.mode === 'browser' || less.mode === 'rhino') {
// We pass `true` as 3rd argument, to force the reload of the import.
// This is so we can get the syntax tree as opposed to just the CSS output,
// as we need this to evaluate the current stylesheet.
- loadStyleSheet({ href: path, title: path, type: env.mime }, function (e) {
+ // __ Now using the hack of passing a ref to top parser's content cache in the 1st arg. __
+ loadStyleSheet({ href: path, title: path, type: env.mime, contents: env.contents }, function (e) {
if (e && typeof(env.errback) === "function") {
env.errback.call(null, path, paths, callback, env);
} else {