Dir.glob raise File::NotFound error when in non existent path

Hi everyone, I find a bug recently with Crystal.

The title look obvious, but it’s not.

Basically if you run a crystal code from a shell when bash is in a dir doesn’t exist anymore, this error occur:

The executed command was this one:

Ism.ports.each do |port|
                        if Dir.exists?(port.directoryPath) && !Dir.glob("#{port.directoryPath}/*").empty?
                            oldPortList.push(port.name)
                        end
                    end

It bug when crystal run Dir.glob.

As you can see in the screenshot, the ls command return nothing because the current dir where I am was deleted

I need to bypass this bug because it’s very annoying

Hello,

I read your post, but I found it a bit difficult to understand the main point. Perhaps you wrote it in a hurry, frustrated with the computer. That happens a lot.

Let’s use a concrete example to clarify:

  1. Create a temporary directory and navigate into it.
mkdir tmp
cd tmp
  1. In another terminal, delete the directory.
rm -r tmp
  1. Back in the first terminal, run the following command:
crystal eval 'Dir.glob("*")'

This results in a File::NotFoundError. Is this the issue you’re referring to?
If so, could you explain what behavior you would like to see in this situation?

If you really think it is a bug, you should report it and request a fix, rather than trying to work around it.

You are completely right, I rushed a bit my bad.

But you got completely the point.

I agree with you too, it’s better to fix than just do a workaround .

My point in this situation is: normally Dir.glob have as purpose to return a list of files.

So this function for me should work, even it is call from (unfortunately ) a no more existent path.

Because this kind of situation can happen quite often in a shell .

I think the problem actually is the implementation of Dir.glob. Look like this function perform a dir change command

No, it does not change the working directory. But it queries the current working directory. If that path doesn’t exit, the method fails. Looks like a bug. Dir.glob should account for a non-existing working directory and handle that gracefully.

1 Like

Okay. So now I will need to wait for a fix I guess. Thank you anyway !

1 Like

Great. I see that you have created issue in crystal for another case that is very similar. It would be helpful for everyone if you write an issue on this matter as well.
The Rails issue template will help you create an issues that are easy to understand.

  1. Reproduction procedure
  2. Expected behavior
  3. Actual behavior