Skip to content

Commit

Permalink
Cache bundle graph on failure (#9366)
Browse files Browse the repository at this point in the history
* Add test to write bundle graph to cache on failure

* Write bundle graph to cache on failure

* Use cache interface to load blob
  • Loading branch information
irismoini authored Nov 9, 2023
1 parent febe169 commit d58e336
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/core/core/src/requests/BundleGraphRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,17 @@ class BundlerRunner {
await this.runDevDepRequest(devDepRequest);
}
} catch (e) {
if (internalBundleGraph != null) {
this.api.storeResult(
{
bundleGraph: internalBundleGraph,
changedAssets: new Map(),
assetRequests: [],
},
this.cacheKey,
);
}

throw new ThrowableDiagnostic({
diagnostic: errorToDiagnostic(e, {
origin: name,
Expand Down
73 changes: 73 additions & 0 deletions packages/core/integration-tests/test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
getParcelOptions,
assertNoFilePathInCache,
findAsset,
bundle,
fsFixture,
} from '@parcel/test-utils';
import {md} from '@parcel/diagnostic';
import fs from 'fs';
Expand All @@ -29,6 +31,9 @@ import {createWorkerFarm} from '@parcel/core';
import resolveOptions from '@parcel/core/src/resolveOptions';
import logger from '@parcel/logger';
import sinon from 'sinon';
import {version} from '@parcel/core/package.json';
import {deserialize} from '@parcel/core/src/serializer';
import {hashString} from '@parcel/rust';

let inputDir: string;
let packageManager = new NodePackageManager(inputFS, '/');
Expand Down Expand Up @@ -6154,6 +6159,74 @@ describe('cache', function () {
assert.equal(res, 4);
});

it('should write bundle graph to cache on bundling error', async function () {
let overlayFSPackageManager = new NodePackageManager(
overlayFS,
__dirname,
);
let entries = 'source/index.js';
let options = {
mode: 'production',
defaultTargetOptions: {
shouldScopeHoist: false,
},
packageManager: overlayFSPackageManager,
shouldDisableCache: false,
inputFS: overlayFS,
cacheDir: path.join(__dirname, '.parcel-cache'),
};

await fsFixture(overlayFS)`
source
foo.js:
export default 2;
index.js:
import('./foo');
export default 1;
.parcelrc:
{
"extends": "@parcel/config-default",
"bundler": "./test-bundler.js"
}
test-bundler.js:
import {Bundler} from '@parcel/plugin'
import DefaultBundler from '@parcel/bundler-default'
const CONFIG = Symbol.for('parcel-plugin-config');
export default new Bundler({
loadConfig({config, options}) {
return DefaultBundler[CONFIG].loadConfig({config, options});
},
bundle({bundleGraph, config}) {
DefaultBundler[CONFIG].bundle({bundleGraph, config});
},
optimize() {throw new Error("Intentionally throw error")},
});
yarn.lock:`;
// $FlowFixMe
await assert.rejects(() => bundle(entries, options));

let resolvedOptions = await resolveOptions(
getParcelOptions(entries, options),
);

let bundleGraphCacheKey = hashString(
`${version}:BundleGraph:${
JSON.stringify(resolvedOptions.entries) ?? ''
}${resolvedOptions.mode}`,
);

assert(
deserialize(
await resolvedOptions.cache.getLargeBlob(bundleGraphCacheKey),
),
);
});

it('should invalidate when a terser config is modified', async function () {
let b = await testCache({
mode: 'production',
Expand Down

0 comments on commit d58e336

Please sign in to comment.