Previous | Up | Next

Elixir's System.cmd and '*' expansion

Today I learned…

I recently tried to use Elixir’s System.cmd/2 to clear some on-disk state, as part of a test. Unfortunately, my tests started to fail for unclear reasons, and closer inspection proved that the state was not being cleared.

Here’s the command I was using:

 {"", 0} = System.cmd("rm", ["-rf", "/tmp/myapp.mymodule/*"])

Had I read the manual page linked above, I’d have known that:

wildcard expansion will not happen (unless Path.wildcard/2 is used explicitly)

What ended up happening was that the * was being passed verbatim to the rm command. Since no such file existed inside the directory, nothing was actually deleted.

I changed the invocation to simply delete the entire directory, avoiding expansion:

 {"", 0} = System.cmd("rm", ["-rf", "/tmp/myapp.mymodule"])

The non-hacky solution is to use an appropriate function from the File module.

Previous | Up | Next