ModClean / modclean

Remove unwanted files and directories from your node_modules folder
MIT License
400 stars 15 forks source link

--ignore option does not work #18

Closed jehy closed 6 years ago

jehy commented 6 years ago

--ignore option does not work, at least for "*.sh" pattern.

Reproduce:

mkdir test
cd test
mkdir node_modules
cd node_modules/
mkdir test_module
cd test_module/
touch smth.sh
cd ../../
modclean --patterns="default:safe,default:caution,default:danger" --run --ignore="*.sh" --verbose --test

Expected not to remove smth.sh, actual result:

MODCLEAN Version 2.1.2

RUNNING IN TEST MODE When running in test mode, files will not be deleted from the file system.

EVENT> start VERBOSE> { "cwd": "/home/jehy/tmp/test/node_modules", "patterns": [ "default:safe", "default:caution", "default:danger" ], "additionalPatterns": [], "ignorePatterns": [ "*.sh" ], "noDirs": false, "ignoreCase": true, "dotFiles": true, "modulesDir": "node_modules", "removeEmptyDirs": true, "errorHalt": false, "test": true, "followSymlink": false } Searching for files in /home/jehy/tmp/test/node_modules...
EVENT> files
Found 1 files/folders to remove

EVENT> process
VERBOSE> DELETED (1/1) test_module/smth.sh
EVENT> finish
VERBOSE> FINISH Deleted 1 files/folders of 1
EVENT> complete

FILES/FOLDERS DELETED
Total: 1
Skipped: 0
Empty: 0

jehy commented 6 years ago

Even specifying --ignore="smth.sh" does not work :(

jehy commented 6 years ago

Environment: Ubuntu 17.10, node v8.9.1, using last version of modclean

jehy commented 6 years ago

Specifying --ignore="***/*.sh" works but that's very strange. Seems like an issue with node-glob package.

KyleRoss commented 6 years ago

Thanks for reporting, you're right about this one. I dove back through the code and it appears I never actually merged the provided ignore list from the ignorePatterns configuration property into the list that is pulled in the from patterns, so no matter what you provide, it shouldn't work.

I'm going through some of my open source modules and upgrading them and doing some refactoring. This is on my list and should get it fixed soon.

Thanks for reporting!

KyleRoss commented 6 years ago

@jehy I think I figured out what may be causing this issue. Since the patterns already has a *.sh pattern to match, node-glob gets into a race condition where it is not sure whether it should match or not match that pattern. In this case, the match takes precedence over the ignore pattern. When you specify ***/*.sh, it will work because this pattern is more "global" than the default *.sh pattern.

The only way I can think about fixing this without impacting performance would be to compare any passed in ignore patterns with the match patterns and remove those from the match patterns if they exist.

I'm going to see if there is any better way of handling this, but for right now, that seems like the best solution.

KyleRoss commented 6 years ago

Added functionality that will remove passed in ignore patterns from the matching patterns if it exists in the matching patterns list. This will prevent the race condition you were seeing here. This has been release in version 3.0.0-beta.1.

I'm working on writing tests now and once that is complete, 3.0.0 will be released.

Thanks again for reporting this!