Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache bundle graph on failure #9366

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading