From 33cc986e1de3792718aef87410763bbc84367d32 Mon Sep 17 00:00:00 2001 From: "Michael \"Z\" Goddard" Date: Mon, 4 Jun 2018 22:02:26 -0400 Subject: [PATCH] fix(cache): reset corrupted caches (#375) --- index.js | 12 ++++++++--- lib/util/log-messages.js | 10 ++++++++++ .../fib.js | 3 +++ .../index.js | 3 +++ .../webpack.config.js | 20 +++++++++++++++++++ tests/serializers.js | 6 ++++++ 6 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/serializer-append-2-base-1dep-bad-cache/fib.js create mode 100644 tests/fixtures/serializer-append-2-base-1dep-bad-cache/index.js create mode 100644 tests/fixtures/serializer-append-2-base-1dep-bad-cache/webpack.config.js diff --git a/index.js b/index.js index e1937cf1..e549425f 100644 --- a/index.js +++ b/index.js @@ -356,9 +356,15 @@ class HardSourceWebpackPlugin { contextNormalModuleId, copyWithDeser, }), - ]).then(() => { - // console.log('cache in', Date.now() - start); - }); + ]) + .catch(error => { + logMessages.serialBadCache(compiler, error); + + return rimraf(cacheDirPath); + }) + .then(() => { + // console.log('cache in', Date.now() - start); + }); }); } diff --git a/lib/util/log-messages.js b/lib/util/log-messages.js index 6ee2605c..ed049d6c 100644 --- a/lib/util/log-messages.js +++ b/lib/util/log-messages.js @@ -43,6 +43,16 @@ exports.cacheNoParity = (compiler, { parityRoot }) => { ); }; +exports.serialBadCache = (compiler, error) => { + const loggerSerial = new LoggerFactory(compiler).create().from('serial'); + loggerSerial.error( + { + id: 'serialzation--bad-cache', + }, + ['Cache is corrupted.', error.stack || error.message || error].join('\n'), + ); +}; + const logCore = compiler => new LoggerFactory(compiler).create().from('core'); exports.configHashSetButNotUsed = (compiler, { cacheDirectory }) => { diff --git a/tests/fixtures/serializer-append-2-base-1dep-bad-cache/fib.js b/tests/fixtures/serializer-append-2-base-1dep-bad-cache/fib.js new file mode 100644 index 00000000..a0a9ae75 --- /dev/null +++ b/tests/fixtures/serializer-append-2-base-1dep-bad-cache/fib.js @@ -0,0 +1,3 @@ +module.exports = function(n) { + return n + (n > 0 ? n - 1 : 0); +}; diff --git a/tests/fixtures/serializer-append-2-base-1dep-bad-cache/index.js b/tests/fixtures/serializer-append-2-base-1dep-bad-cache/index.js new file mode 100644 index 00000000..8acf0395 --- /dev/null +++ b/tests/fixtures/serializer-append-2-base-1dep-bad-cache/index.js @@ -0,0 +1,3 @@ +var fib = require('./fib'); + +console.log(fib(3)); diff --git a/tests/fixtures/serializer-append-2-base-1dep-bad-cache/webpack.config.js b/tests/fixtures/serializer-append-2-base-1dep-bad-cache/webpack.config.js new file mode 100644 index 00000000..ec5966f0 --- /dev/null +++ b/tests/fixtures/serializer-append-2-base-1dep-bad-cache/webpack.config.js @@ -0,0 +1,20 @@ +var HardSourceWebpackPlugin = require('../../..'); +var SerializerAppend2Plugin = require('../../../lib/SerializerAppend2Plugin'); + +module.exports = { + context: __dirname, + entry: './index.js', + output: { + path: __dirname + '/tmp', + filename: 'main.js', + }, + plugins: [ + new HardSourceWebpackPlugin({ + cacheDirectory: 'cache', + environmentHash: { + root: __dirname + '/../../..', + }, + }), + new SerializerAppend2Plugin(), + ], +}; diff --git a/tests/serializers.js b/tests/serializers.js index 8f30a207..eae7eeaa 100644 --- a/tests/serializers.js +++ b/tests/serializers.js @@ -136,4 +136,10 @@ describe('hard source serializers - serializer abilities', function() { }); }); + itCompilesChange('serializer-append-2-base-1dep-bad-cache', {}, { + 'tmp/cache/module/log0000': '', + }, function(output) { + expect(output.run2).to.eql(output.run2); + }); + });