Skip to content

Commit 8b15870

Browse files
authored
fix(sign), write the correct version according to the current snap in the package.json (#9039)
1 parent 9c27003 commit 8b15870

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

e2e/harmony/sign.e2e.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,21 +188,31 @@ describe('sign command', function () {
188188
helper.scopeHelper.addRemoteScope(helper.scopes.remotePath, signRemote.scopePath);
189189
signOutput = helper.command.sign(
190190
[`${secondScopeName}/comp1@${snapHash}`],
191-
`--lane ${helper.scopes.remote}/dev`,
191+
`--lane ${helper.scopes.remote}/dev --save-locally`,
192192
signRemote.scopePath
193193
);
194194
expect(signOutput).to.include('the following 1 component(s) were signed with build-status "succeed"');
195195
expect(signOutput).to.not.include('tag pipe');
196196
expect(signOutput).to.include('snap pipe');
197+
198+
const obj = helper.command.catObject(snapHash, true, signRemote.scopePath);
199+
const pkgAspectData = helper.command.getAspectsData(obj, Extensions.pkg);
200+
const version = pkgAspectData.data.pkgJson.version;
201+
expect(version).to.equal(`0.0.0-${snapHash}`);
197202
});
198203
it('should be able to sign previous snaps on this lane successfully', () => {
199204
helper.scopeHelper.addRemoteScope(helper.scopes.remotePath, signRemote.scopePath);
200205
signOutput = helper.command.sign(
201206
[`${secondScopeName}/comp1@${firstSnapHash}`],
202-
`--lane ${helper.scopes.remote}/dev`,
207+
`--lane ${helper.scopes.remote}/dev --save-locally`,
203208
signRemote.scopePath
204209
);
205210
expect(signOutput).to.include('the following 1 component(s) were signed with build-status "succeed"');
211+
212+
const obj = helper.command.catObject(firstSnapHash, true, signRemote.scopePath);
213+
const pkgAspectData = helper.command.getAspectsData(obj, Extensions.pkg);
214+
const version = pkgAspectData.data.pkgJson.version;
215+
expect(version).to.equal(`0.0.0-${firstSnapHash}`);
206216
});
207217
// todo: support exporting to a non-hub
208218
it.skip('should sign the last successfully and export', () => {

scopes/component/component-package-version/component-package-version.ts

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,13 @@ import { isHash } from '@teambit/component-version';
44
export const SNAP_VERSION_PREFIX = '0.0.0-';
55

66
/**
7-
* This function will calculate the package version for a component.
8-
* It is used mainly for supporting lanes and snaps during capsule creation
9-
* The general format is like this:
10-
* {0.0.0}-{snapHash}
11-
* but there are some exceptions:
12-
* in case the snapHash equal to the closestTagSemver snap hash the format will be just
13-
* {closestTagSemver}
14-
* @param component
7+
* get a valid semver package version of a component.
158
*/
16-
export function getComponentPackageVersion(component: Component, snapId?: string): string {
17-
const actualSnapId = snapId || component.head?.hash;
18-
if (!actualSnapId) {
19-
return '0.0.0';
20-
}
21-
// Checking if the snap is already a tag
22-
const tagBySnap = component.tags.byHash(actualSnapId);
23-
if (tagBySnap) {
24-
return `${tagBySnap.version}`;
25-
}
26-
return snapToSemver(actualSnapId);
9+
export function getComponentPackageVersion(component: Component): string {
10+
const version = component.id.version;
11+
if (!version)
12+
throw new Error(`getComponentPackageVersion: component ${component.id.toString()} is missing a version`);
13+
return snapToSemver(version);
2714
}
2815

2916
export function snapToSemver(version: string): string {

scopes/component/isolator/isolator.main.runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ export class IsolatorMain {
10971097
const depCapsule = capsules.getCapsule(dep.componentId);
10981098
let version = dep.version;
10991099
if (depCapsule) {
1100-
version = getComponentPackageVersion(depCapsule?.component);
1100+
version = getComponentPackageVersion(depCapsule.component);
11011101
} else {
11021102
version = snapToSemver(version);
11031103
}

src/e2e-helper/e2e-command-helper.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,11 @@ export default class CommandHelper {
482482
if (!artifacts) throw new Error(`unable to find artifacts data for ${id}`);
483483
return artifacts;
484484
}
485+
getAspectsData(versionObject: Record<string, any>, aspectId: string) {
486+
const builder = versionObject.extensions.find((e) => e.name === Extensions.builder);
487+
if (!builder) throw new Error(`getAspectsData: unable to find builder data`);
488+
return builder.data.aspectsData.find((a) => a.aspectId === aspectId);
489+
}
485490
reset(id: string, head = false, flag = '') {
486491
return this.runCmd(`bit reset ${id} ${head ? '--head' : ''} ${flag}`);
487492
}

0 commit comments

Comments
 (0)