Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the behavior of file.recurse so that it will log an error if it p... #1170

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,13 @@ file.recurse = function recurse(rootdir, callback, subdir) {
var abspath = subdir ? path.join(rootdir, subdir) : rootdir;
fs.readdirSync(abspath).forEach(function(filename) {
var filepath = path.join(abspath, filename);
if (fs.statSync(filepath).isDirectory()) {
var isDirectory = false;
try {
isDirectory = fs.statSync(filepath).isDirectory();
} catch (e) {
grunt.log.error(e);
}
if (isDirectory) {
recurse(rootdir, callback, unixifyPath(path.join(subdir || '', filename || '')));
} else {
callback(unixifyPath(filepath), rootdir, subdir, filename);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback would still fire if there is an error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is, yeah. I am not sure what would be best to do in the situation. Current behavior is a fatal error and a hard stop if it encounters a broken symlink. With this change, it will log an error and continue on. Unfortunately, fs.statSync(filepath) just throws a generic error, so we can't get more specific with the catch clause.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@
"semver": "2.1.0",
"shelljs": "~0.2.5"
}
}
}
1 change: 1 addition & 0 deletions test/fixtures/symlinks/bad-symlink
17 changes: 17 additions & 0 deletions test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,23 @@ exports['file'] = {

test.done();
},
'recurse bad symlink': function(test) {
var rootdir = 'test/fixtures/symlinks';

var expected = {};
expected[rootdir+'/bad-symlink'] = [rootdir, 'bad-symlink'];

var actual = {};
try{
grunt.file.recurse(rootdir, function(abspath, rootdir, subdir, filename) {
actual[abspath] = [rootdir, filename];
});
} catch (e) {
// We log an error if the file doesn't exist, expected behavior
}
test.deepEqual(actual, expected, 'file recurse will process broken symlinks');
test.done();
},
'recurse': function(test) {
test.expect(1);
var rootdir = 'test/fixtures/expand';
Expand Down