Skip to content

Commit 8636e15

Browse files
authored
fix(tag), exit immediately when a pre-release id is invalid (#9063)
Currently it saves the objects and throws in a later phase, which make the `Version` object corrupted.
1 parent dd2aba5 commit 8636e15

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

e2e/harmony/tag-harmony.e2e.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,23 @@ describe('tag components on Harmony', function () {
361361
expect(tagOutput).to.have.string('[email protected]');
362362
});
363363
});
364+
describe('invalid pre-release after normal tag', () => {
365+
let result: string;
366+
before(() => {
367+
helper.scopeHelper.setNewLocalAndRemoteScopes();
368+
helper.fixtures.populateComponents(1);
369+
helper.command.tagAllWithoutBuild();
370+
result = helper.general.runWithTryCatch(`bit tag --unmodified --pre-release "h?h"`);
371+
});
372+
it('should throw an error', () => {
373+
expect(result).to.have.string('is not a valid semantic version');
374+
});
375+
it('should not create a new version', () => {
376+
const comp = helper.command.catComponent('comp1');
377+
const ver1Hash = comp.versions['0.0.1'];
378+
expect(comp.head).to.equal(ver1Hash);
379+
});
380+
});
364381
describe('soft-tag pre-release', () => {
365382
let tagOutput: string;
366383
before(() => {

src/scope/models/model-component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ComponentOverrides from '../../consumer/config/component-overrides';
1717
import ValidationError from '../../error/validation-error';
1818
import logger from '../../logger/logger';
1919
import { getStringifyArgs } from '@teambit/legacy.utils';
20-
import { getLatestVersion } from '@teambit/pkg.modules.semver-helper';
20+
import { getLatestVersion, validateVersion } from '@teambit/pkg.modules.semver-helper';
2121
import ComponentObjects from '../component-objects';
2222
import { SnapsDistance } from '../component-ops/snaps-distance';
2323
import { getDivergeData } from '../component-ops/get-diverge-data';
@@ -602,7 +602,9 @@ export default class Component extends BitObject {
602602
if (exactVersion && this.versions[exactVersion]) {
603603
throw new VersionAlreadyExists(exactVersion, this.id());
604604
}
605-
return exactVersion || this.version(releaseType, incrementBy, preReleaseId);
605+
const version = exactVersion || this.version(releaseType, incrementBy, preReleaseId);
606+
validateVersion(version);
607+
return version;
606608
}
607609

608610
isEqual(component: Component, considerOrphanedVersions = true): boolean {

src/scope/version-validator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
/* eslint-disable complexity */
12
import { PackageJsonValidator as PJV } from '@teambit/pkg.package-json.validator';
23
import R from 'ramda';
34
import { lt, gt } from 'semver';
45
import packageNameValidate from 'validate-npm-package-name';
56
import { ComponentID, ComponentIdList } from '@teambit/component-id';
67
import { BitError } from '@teambit/bit-error';
7-
import { isSnap } from '@teambit/component-version';
8+
import { isSnap, isTag } from '@teambit/component-version';
89
import { DEPENDENCIES_FIELDS } from '../constants';
910
import { SchemaName } from '../consumer/component/component-schema';
1011
import { Dependencies } from '../consumer/component/dependencies';
@@ -316,6 +317,11 @@ ${duplicationStr}`);
316317
});
317318
});
318319
}
320+
const ver = version.componentId?.version;
321+
if (ver && !isSnap(ver) && !isTag(ver)) {
322+
throw new VersionInvalid(`${message}, the version "${ver}" is invalid. it's not a hash (snap) nor a tag`);
323+
}
324+
319325
if (version.isLegacy) {
320326
// mainly to make sure that all Harmony components are saved with schema
321327
// if they don't have schema, they'll fail on this test

0 commit comments

Comments
 (0)