Skip to content

Commit

Permalink
fix(agoric-cli): Fully generalize package name extractor from zip fil…
Browse files Browse the repository at this point in the history
…es (#9669)

## Description

Following #9667 we learn that the bundle analyzer needs to understand exotic package names that include `-dev$hash` and such. To that end, this change fully generalizes the package name detector based on the `@` prefix distinguishing a package name with two path components.

### Security Considerations

None.

### Scaling Considerations

None.

### Documentation Considerations

None.

### Testing Considerations

None.

### Upgrade Considerations

None.
  • Loading branch information
mergify[bot] authored Jul 12, 2024
2 parents 3f2cb5c + 1de3fd4 commit 7b85450
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/agoric-cli/src/lib/bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { ZipReader } from '@endo/zip';
/** @import {Bundle} from '@agoric/swingset-vat'; */
/** @import {CoreEvalPlan} from '@agoric/deploy-script-support/src/writeCoreEvalParts.js' */

const PACKAGE_NAME_RE = /(?<packageName>.*-v[\d.]+(?:-n\d+)?)\//;
// exported for testing
export const PACKAGE_NAME_RE = /^(?:@[^/]+\/)?[^/]+/;

/**
* @typedef {{ name: string, label: string, location: string, modules: Record<string, {compartment: string, module: string}>}} Compartment
Expand Down Expand Up @@ -70,7 +71,7 @@ export const statBundle = async bundleFilename => {
if (filename === 'compartment-map.json') {
continue;
}
const { packageName } = filename.match(PACKAGE_NAME_RE)?.groups ?? {};
const packageName = filename.match(PACKAGE_NAME_RE)?.[0];
assert(packageName, `invalid filename ${filename}`);
byPackage[packageName] ||= 0;
byPackage[packageName] += size;
Expand Down
39 changes: 39 additions & 0 deletions packages/agoric-cli/test/bundles-regExp.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava';
import { PACKAGE_NAME_RE } from '../src/lib/bundles.js';

const goodPatterns = [
['@agoric/assert-v0.6.0'],
['@agoric/base-zone-v0.1.0/', '@agoric/base-zone-v0.1.0'],
['@endo/base64-v1.0.5-n1/index.js', '@endo/base64-v1.0.5-n1'],
['@endo/base64-v1.0.5-n1/decode.js', '@endo/base64-v1.0.5-n1'],
[
'@agoric/store-v0.9.3-dev-37ec151.0+37ec151/src/legacy/legacyWeakMap.js',
'@agoric/store-v0.9.3-dev-37ec151.0+37ec151',
],
[
'calypso-contract-v0.1.0/src/proposals/core-proposal.js',
'calypso-contract-v0.1.0',
],
['/index.js', undefined],
['/src', undefined],
];

test('simple positive', t => {
for (const pattern of goodPatterns) {
const name = pattern[0];
const expected = pattern.length === 2 ? pattern[1] : pattern[0];

t.is(name.match(PACKAGE_NAME_RE)?.[0], expected);
}
});

const badPatterns = [
'/user/name/sdk/node_modules/@agoric/assert-v0.6.0',
'/random/@agoric/base-zone-v0.1.0/',
];

test('simple negative', t => {
for (const pattern of badPatterns) {
t.falsy(pattern.match(PACKAGE_NAME_RE)?.[0], `expected ${pattern} to fail`);
}
});

0 comments on commit 7b85450

Please sign in to comment.