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

fix: Avoid key-error when inserting asset chunks #550

Merged
merged 6 commits into from
Mar 3, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
add to other parser versions and test
  • Loading branch information
ajay-sentry committed Feb 28, 2025
commit e97b3d9445b40116330972f88aed8b5d278a7549
3 changes: 2 additions & 1 deletion shared/bundle_analysis/parsers/v1.py
Original file line number Diff line number Diff line change
@@ -342,8 +342,9 @@ def _create_associations(self):
asset_names = self.chunk_asset_names_index[chunk.unique_external_id]
inserts.extend(
[
dict(asset_id=asset_name_to_id[asset_name], chunk_id=chunk_id)
dict(asset_id=asset_name_to_id.get(asset_name), chunk_id=chunk_id)
for asset_name in asset_names
if asset_name_to_id.get(asset_name) is not None
]
)
if inserts:
3 changes: 2 additions & 1 deletion shared/bundle_analysis/parsers/v2.py
Original file line number Diff line number Diff line change
@@ -345,8 +345,9 @@ def _create_associations(self):
asset_names = self.chunk_asset_names_index[chunk.unique_external_id]
inserts.extend(
[
dict(asset_id=asset_name_to_id[asset_name], chunk_id=chunk_id)
dict(asset_id=asset_name_to_id.get(asset_name), chunk_id=chunk_id)
for asset_name in asset_names
if asset_name_to_id.get(asset_name) is not None
]
)
if inserts:
49 changes: 49 additions & 0 deletions tests/samples/sample_bundle_stats_w_bad_chunk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"version": "__VERSION__",
"plugin": {
"name": "codecov-vite-bundle-analysis-plugin",
"version": "1.0.0"
},
"builtAt": 1701451048604,
"duration": 331,
"bundler": { "name": "rollup", "version": "3.29.4" },
"bundleName": "sample",
"assets": [
{
"name": "assets/LazyComponent-fcbb0922.js",
"size": 294,
"gzipSize": 293,
"normalized": "assets/LazyComponent-*.js"
}
],
"chunks": [
{
"id": "LazyComponent",
"uniqueId": "0-LazyComponent",
"entry": false,
"initial": true,
"files": ["assets/LazyComponent-fcbb0922.js"],
"names": ["LazyComponent"]
},
{
"id": "this_claims_to_have_an_asset_that_does_not_exist",
"uniqueId": "invalid_chunk",
"entry": false,
"initial": true,
"files": ["assets/asset_that_does_not_exist.js"],
"names": ["Dodo"]
}
],
"modules": [
{
"name": "./src/LazyComponent/LazyComponent.tsx",
"size": 497,
"chunkUniqueIds": ["0-LazyComponent"]
},
{
"name": "./xxx.jpg",
"size": 0,
"chunkUniqueIds": ["invalid_chunk"]
}
]
}
36 changes: 36 additions & 0 deletions tests/unit/bundle_analysis/test_bundle_analysis.py
Original file line number Diff line number Diff line change
@@ -72,6 +72,12 @@
/ "sample_bundle_stats_dynamic_import_routing_1.json"
)

sample_bundle_stats_path_10 = (
Path(__file__).parent.parent.parent
/ "samples"
/ "sample_bundle_stats_w_bad_chunk.json"
)


def _table_rows_count(db_session: DbSession) -> Tuple[int]:
return (
@@ -1054,3 +1060,33 @@ def test_bundle_report_route_report_with_dynamic_imports():
assert route_report.get_size("/sverdle/users") == 2111
finally:
report.cleanup()


@pytest.mark.parametrize("version", ["1", "2", "3"])
def test_bundle_report_cleans_bad_chunks(version):
try:
report = BundleAnalysisReport()

# Read and modify the JSON content
with open(sample_bundle_stats_path_10, "r") as f:
content = f.read().replace("__VERSION__", version)

# Create a temporary file with the modified content
import tempfile

with tempfile.NamedTemporaryFile(
mode="w", suffix=".json", delete=False
) as temp:
temp.write(content)
temp_path = Path(temp.name)

report.ingest(temp_path)
bundle_report = report.bundle_report("sample")

assets = sorted([item.hashed_name for item in bundle_report.asset_reports()])
assert assets == [
"assets/LazyComponent-fcbb0922.js",
]
temp_path.unlink()
finally:
report.cleanup()
Loading
Oops, something went wrong.