gulp-jasmine
gulp-jasmine copied to clipboard
deleteRequireCache blows the stack for circlular referenced files
Ignoring the design implications of such a situation, if you have 2 source code files that reference each other the deleteRequireCache()
function as is will recursively get stuck trying to delete the other reference and eventually blow the stack:
RangeError: Maximum call stack size exceeded
It seems a viable solution would be to delete from cache before iterating through the child references? It solved the problem in my case:
function deleteRequireCache(id) {
if (!id || id.includes('node_modules')) {
return;
}
const files = require.cache[id];
if (files !== undefined) {
delete require.cache[id];
for (const file of Object.keys(files.children)) {
deleteRequireCache(files.children[file].id);
}
}
}
Also got hit by this, this is what I did to detect the cyclic dependecy:
function deleteRequireCache(id, paths) {
if (!id || id.indexOf('node_modules') !== -1) {
return;
}
if (!paths) {
paths = [ id ];
} else {
paths.push(id);
}
if (paths.indexOf(id) < paths.length - 1) {
const separator = '\n -> ';
throw new Error(`gulp-jasmine can't handle cyclic dependencies:${separator}${paths.join(separator)}`)
}
var files = require.cache[id];
if (files !== undefined) {
Object.keys(files.children).forEach(function (file) {
deleteRequireCache(files.children[file].id, paths.slice());
});
delete require.cache[id];
}
}
@tomyam1 Could you submit as a pull request?
@KurtPreston Feel free to use this snippet to submit a PR yourself.