From 703f79e4cea5dc195ee9db855f3e1637469510da Mon Sep 17 00:00:00 2001 From: David First Date: Tue, 3 Sep 2024 15:58:14 -0400 Subject: [PATCH] fix(sign), throw a descriptive error when the requested snap belong to another component (#9171) --- scopes/scope/scope/scope-component-loader.ts | 7 +++++++ src/scope/models/version.ts | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/scopes/scope/scope/scope-component-loader.ts b/scopes/scope/scope/scope-component-loader.ts index 89c740037f5e..8770f27916fa 100644 --- a/scopes/scope/scope/scope-component-loader.ts +++ b/scopes/scope/scope/scope-component-loader.ts @@ -6,6 +6,7 @@ import ConsumerComponent from '@teambit/legacy/dist/consumer/component'; import { ModelComponent, Version } from '@teambit/legacy/dist/scope/models'; import { Ref } from '@teambit/legacy/dist/scope/objects'; import { VERSION_ZERO } from '@teambit/legacy/dist/scope/models/model-component'; +import { BitError } from '@teambit/bit-error'; import { getMaxSizeForComponents, InMemoryCache, createInMemoryCache } from '@teambit/harmony.modules.in-memory-cache'; import type { ScopeMain } from './scope.main.runtime'; @@ -51,6 +52,12 @@ export class ScopeComponentLoader { if (versionStr === VERSION_ZERO) return undefined; const newId = id.changeVersion(versionStr); const version = await modelComponent.loadVersion(versionStr, this.scope.legacyScope.objects); + const versionOriginId = version.originId; + if (versionOriginId && !versionOriginId.isEqualWithoutVersion(id)) { + throw new BitError( + `version "${versionStr}" seem to be originated from "${versionOriginId.toString()}", not from "${id.toStringWithoutVersion()}"` + ); + } const snap = await this.getHeadSnap(modelComponent); const state = await this.createStateFromVersion(id, version); const tagMap = this.getTagMap(modelComponent); diff --git a/src/scope/models/version.ts b/src/scope/models/version.ts index 083a51d3752e..70ad0be46bcd 100644 --- a/src/scope/models/version.ts +++ b/src/scope/models/version.ts @@ -695,6 +695,11 @@ export default class Version extends BitObject { get originLaneId(): LaneId | undefined { return this.origin?.lane ? new LaneId({ name: this.origin.lane.name, scope: this.origin.lane.scope }) : undefined; } + get originId(): ComponentID | undefined { + return this.origin?.id + ? ComponentID.fromObject({ scope: this.origin.id.scope, name: this.origin.id.name }) + : undefined; + } setDist(dist: Source | undefined) { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!