Skip to content

Commit

Permalink
fix(remove): block soft-remove main components when on a lane (#7112)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidfirst authored Mar 3, 2023
1 parent c4ff95c commit 36746b0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
16 changes: 16 additions & 0 deletions e2e/harmony/lanes/bit-remove-on-lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,20 @@ describe('bit lane command', function () {
expect(() => helper.command.export()).to.not.throw();
});
});
describe('soft-remove main component when on a lane', () => {
before(() => {
helper.scopeHelper.setNewLocalAndRemoteScopes();
helper.fixtures.populateComponents(2);
helper.command.tagAllWithoutBuild();
helper.command.export();
helper.command.createLane();
helper.command.snapComponentWithoutBuild('comp1', '--unmodified');
helper.command.export();
});
it('should throw an error', () => {
expect(() => helper.command.removeComponent('comp2', '--soft')).to.throw(
'the following components belong to main, they cannot be soft-removed when on a lane'
);
});
});
});
12 changes: 12 additions & 0 deletions scopes/component/remove/remove.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class RemoveMain {
.join('\n')}`
);
}
await this.throwForMainComponentWhenOnLane(components);
await removeComponentsFromNodeModules(
this.workspace.consumer,
components.map((c) => c.state._consumer)
Expand All @@ -89,6 +90,17 @@ export class RemoveMain {
return componentIds;
}

private async throwForMainComponentWhenOnLane(components: Component[]) {
const currentLane = await this.workspace.getCurrentLaneObject();
if (!currentLane) return; // user on main
const laneComps = currentLane.toBitIds();
const mainComps = components.filter((comp) => !laneComps.hasWithoutVersion(comp.id._legacy));
if (mainComps.length) {
throw new BitError(`the following components belong to main, they cannot be soft-removed when on a lane. consider removing them without --soft.
${mainComps.map((c) => c.id.toString()).join('\n')}`);
}
}

getRemoveInfo(component: Component): RemoveInfo {
const data = component.config.extensions.findExtension(RemoveAspect.id)?.config as RemoveInfo | undefined;
return {
Expand Down
12 changes: 10 additions & 2 deletions scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ import { BitMap } from './bit-map';
import { WorkspaceAspect } from './workspace.aspect';
import { GraphIdsFromFsBuilder } from './build-graph-ids-from-fs';
import { AspectsMerger } from './aspects-merger';
import { AspectPackage, GetConfiguredUserAspectsPackagesOptions, WorkspaceAspectsLoader } from './workspace-aspects-loader';
import {
AspectPackage,
GetConfiguredUserAspectsPackagesOptions,
WorkspaceAspectsLoader,
} from './workspace-aspects-loader';
import { MergeConflictFile } from './merge-conflict-file';

export type EjectConfResult = {
Expand Down Expand Up @@ -677,6 +681,10 @@ export class Workspace implements ComponentFactory {
return this.consumer.getCurrentLaneId();
}

async getCurrentLaneObject(): Promise<Lane | null> {
return this.consumer.getCurrentLaneObject();
}

isOnMain(): boolean {
return this.consumer.isOnMain();
}
Expand Down Expand Up @@ -1263,7 +1271,7 @@ the following envs are used in this workspace: ${availableEnvs.join(', ')}`);
return cacheDir;
}

getWorkspaceAspectsLoader(): WorkspaceAspectsLoader{
getWorkspaceAspectsLoader(): WorkspaceAspectsLoader {
const workspaceAspectsLoader = new WorkspaceAspectsLoader(
this,
this.scope,
Expand Down

0 comments on commit 36746b0

Please sign in to comment.