gulp-jasmine icon indicating copy to clipboard operation
gulp-jasmine copied to clipboard

deleteRequireCache blows the stack for circlular referenced files

Open SeanSobeySage opened this issue 7 years ago • 3 comments

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);
		}
	}
}

SeanSobeySage avatar Feb 04 '18 16:02 SeanSobeySage

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 avatar Mar 22 '18 17:03 tomyam1

@tomyam1 Could you submit as a pull request?

KurtPreston avatar Nov 23 '18 18:11 KurtPreston

@KurtPreston Feel free to use this snippet to submit a PR yourself.

tomyam1 avatar Nov 24 '18 09:11 tomyam1