[Not bug, I'm stupid] setDirectory() includeDirectories is not working

Hi JUCE team, @t0m

I want to report an issue on DirectoryContentsList::setDirectory, the bool includeDirectories in it is not working properly.

After preparing a folder including 4 files and 1 folder with 1 subfile , then set the folder including these files to be the root with setDirectory(), then no matter the includeDirectories if true or false, the function getNumFiles() will always return 5, not ignoring the subfolder accordingly, and the 5th filename in fileInfo will always be the subfolder name. This will directly lead to crash say using the function getFile(int index) to load audio file from this DirectoryContentsList.

If it is designed to work like this then what is this bool includeDirectories used for? It doesn’t control if the subfiles in subfolders should be scanned seeing DirectoryContentsList::refresh(), but there should be a setting at least to help ignore folders in the directory, but this setting seems not exposed to us to control.

Please help, thanks!

Can you post a short code example, as it will be easier to comment on with more context. Surround your example with a ‘triple tic’ to format your code.

Ok, here’s my code, although I don’t think they have anything wrong

Setting the directory

            mDirectoryList.setDirectory(mFilePathURL.getLocalFile().getParentDirectory(), false, true);

Then get the file number in this directory, always the same no matter true/false for the includeDirectories setting in setDirectory()

    mTotalFileNumInDir = mDirectoryList.getNumFiles();
    DBG(mTotalFileNumInDir);

So I printed out the file name causing crash, then I found it is the folder name.

    mFilePathURL = juce::URL(mDirectoryList.getFile(fileOrder));
    mFileName = mDirectoryList.getFile(fileOrder).getFileName();
    DBG(mFileName);

So I got confused by bool includeDirectories, not sure what’s its purpose.

void DirectoryContentsList::setDirectory	(	const File & 	directory,
bool 	includeDirectories,
bool 	includeFiles 
)	

DirectoryContentsList runs asynchronously, are you checking if it is done before trying to use it?

bool DirectoryContentsList::isSillLoading()

Yes, I added a changeListener to my DirectoryContentsList then check the file number and file info in changeListenerCallback()

I am not 100% sure, but a quick look at the juce code seems to indicate that the changeListener callback is not synchronized with the search being done, it only indicates something has caused a search to begin. You might try checking the result of isSillLoading in the callback to verify.

I’m also not 100% sure, but from my test, setDirectory() will trigger 3 times of changeCallback(), the file count will be 0, 0, then final result. So I’m assuming the 3rd time is indicating it’s already finished searching.

If you check isSillLoading you will know for sure. :nerd_face:

I did checked, already stopped searching after 3rd time changeCallback. And because the loading time is too short, you can’t even use DBG to caught it in processBlock

The code looks right to me. The directory flag code seems correct, the number of files comes from the container so it matches the items you can retrieve, etc. I’ve just been browsing the code, but since you have a scenario with the issue, you can debug it. You could either step through the code in the debugger, or a quick way to verify what is going on could be to put some logging into the DirectoryContentsList class.

Just checked my code again, and I found I didn’t set the directory in the right place, I AM STUPID AF!!! Thanks for helping~

I’m glad you found the issue. One of the things I like about JUCE is being able to debug the library. If I find a bug in JUCE, I can let the devs know where the problem is. And, it can help determine if it’s a bug in our code. Happy coding! :nerd_face: