diff --git a/lib/cache.js b/lib/cache.js
index <HASH>..<HASH> 100644
--- a/lib/cache.js
+++ b/lib/cache.js
@@ -47,11 +47,25 @@ var DCp = DiskCache.prototype;
 DCp.setDefaultP = util.cachedMethod(function(key, toStringP, fromStringP) {
     return this.cacheDirP.then(function(cacheDir) {
         var file = path.join(cacheDir, key + ".js");
+
         return util.openExclusiveP(file).then(function(fd) {
             var stringP = Q.resolve(toStringP());
+
             return stringP.then(function(string) {
                 return util.writeFdP(fd, string);
+
+            }, function(err) {
+                // Note that util.writeFdP closes the file descriptor
+                // under normal circumstances.
+                fs.closeSync(fd);
+
+                // If stringP resolved to an error, remove the cache file
+                // so we don't reuse it blindly in the future.
+                return util.unlinkP(file).then(function() {
+                    throw err;
+                });
             });
+
         }, function(err) {
             assert.strictEqual(err.code, "EEXIST"); // TODO
             // TODO Deal with inter-process race condition!
Make sure to unlink cache files that fail to build successfully.