19

I run a hosted continuous integration company, and we run our customers' code on Linux. Each time we run the code, we run it in a separate virtual machine. A frequent problem that arises is that a customer's tests will sometimes fail because of the directory ordering of their code checked out on the VM.

Let me go into more detail. On OSX, the HFS+ file system ensures that directories are always traversed in the same order. Programmers who use OSX assume that if it works on their machine, it must work everywhere. But it often doesn't work on Linux, because linux file systems do not offer ordering guarantees when traversing directories.

As an example, consider there are 2 files, a.rb, b.rb. a.rb defines MyObject, and b.rb uses MyObject. If a.rb is loaded first, everything will work. If b.rb is loaded first, it will try to access an undefined variable MyObject, and fail.

But worse than this, is that it doesn't always just fail. Because the file system ordering on Linux is not ordered, it will be a different order on different machines. This is worse because sometimes the tests pass, and sometimes they fail. This is the worst possible result.

So my question is, is there a way to make file system ordering repeatable. Some flag to ext4 perhaps, that says it will always traverse directories in some order? Or maybe a different file system that has this guarantee?

4 Answers4

15

I know it's not the answer you're looking for, but I believe the correct solution is to avoid depending on the ordering of files in a directory. Maybe it's always consistent across all HFS+ filesystems, and maybe you could find a way to make it consistent in ext4 or some other filesystem as well, but it will cost you more trouble in the long run than it will save. Someone else using your application will run into a nasty surprise when they don't realize that it's compatible only with some types of filesystems and not others. The order may change if a filesystem is restored from backup. You'll likely run into compatibility problems because the HFS+ consistent order and the ext4 consistent order might not be the same.

Just read all of the directory entries and sort the list lexicographically before using it. Just like ls does.

You mention files a.rb and b.rb, but if we're talking about programming language source files, shouldn't each file already be responsible for ensuring that it imports all its dependencies?

Useless
  • 103
Celada
  • 6,430
6

The POSIX call in Linux readdir() doesn't guarantee any consistent ordering. If you want ordered results, the application that is handling files is responsible for ordering how they are presented to calling functions.

https://stackoverflow.com/questions/8977441/does-readdir-guarantee-an-order

Now, since you said this was your customer's code and you can't fix it, you could possibly alter the linked libraries that are used to provide a consistent readdir() call. That would take some work and be worth its own question. For a quick reference to that, see http://www.ibm.com/developerworks/linux/library/l-glibc/index.html.

Altering this could spawn some other entire series of issues that I may not be able to foresee. You are strongly cautioned, but it may be a solution if your customer cannot be properly educated.

Jeff Ferland
  • 20,987
2

Modern Linux (ext4) adds a B-tree index for file lists. One of his effect is default files order depends on a hash of their names.

To disable this feature use :

tune2fs -O^dir_index

Yup
  • 21
  • 1
1

Educate your customer that there is an inherent order dependency that should be explicitly stated. Offer to help the customer express the dependency in such a way that a compile works on all systems and have the customer adopt the changed flow that captures the compilation order dependency.

If the customer wants to be able to compile on other machines then it would be churlish of them to think that it comes for free.

Paddy3118
  • 111