Skip to content

Commit e16d859

Browse files
authored
fix, handle soft-remove on a new lane and import soft-removed components (teambit#7137)
## Proposed Changes - when a lane is new (not exported yet), don't allow soft-remove. suggest to "bit remove" instead. we already do the same when components are new. - fix obscure error `"Cannot read properties of undefined (reading 'id')` when importing locally soft-removed component.
1 parent ea2e0a7 commit e16d859

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

e2e/commands/remove.e2e.1.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ describe('bit remove command', function () {
221221
helper.command.tagWithoutBuild();
222222
helper.command.export();
223223

224-
helper.command.removeComponent('comp2', '--soft');
224+
helper.command.softRemoveComponent('comp2');
225225
afterRemove = helper.scopeHelper.cloneLocalScope();
226226
});
227227
it('bit status should show a section of removed components', () => {
@@ -392,4 +392,29 @@ describe('bit remove command', function () {
392392
expect(removeAspect.config.removed).to.be.true;
393393
});
394394
});
395+
describe('soft-remove when a lane is new', () => {
396+
before(() => {
397+
helper.scopeHelper.setNewLocalAndRemoteScopes();
398+
helper.fixtures.populateComponents(2);
399+
helper.command.createLane();
400+
helper.command.snapAllComponentsWithoutBuild();
401+
});
402+
it('should throw an error suggesting to remove without --soft', () => {
403+
expect(() => helper.command.softRemoveComponent('comp1')).to.throw();
404+
});
405+
});
406+
407+
describe('soft-remove then import', () => {
408+
before(() => {
409+
helper.scopeHelper.setNewLocalAndRemoteScopes();
410+
helper.command.createLane();
411+
helper.fixtures.populateComponents(2);
412+
helper.command.snapAllComponentsWithoutBuild();
413+
helper.command.export();
414+
helper.command.softRemoveComponent('comp2');
415+
});
416+
it('should not throwing an error upon import', () => {
417+
expect(() => helper.command.importComponent('comp2')).to.not.throw();
418+
});
419+
});
395420
});

scopes/component/remove/remove.main.runtime.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ export class RemoveMain {
6161

6262
async softRemove(componentsPattern: string): Promise<ComponentID[]> {
6363
if (!this.workspace) throw new ConsumerNotFound();
64+
const currentLane = await this.workspace.getCurrentLaneObject();
65+
if (currentLane?.isNew) {
66+
throw new BitError(
67+
`unable to soft-remove on a new (not-exported) lane "${currentLane.name}". please remove without --soft`
68+
);
69+
}
6470
const componentIds = await this.workspace.idsByPattern(componentsPattern);
6571
const components = await this.workspace.getMany(componentIds);
6672
const newComps = components.filter((c) => !c.id.hasVersion());

src/consumer/component-ops/component-status-loader.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import mapSeries from 'p-map-series';
22
import { Consumer } from '..';
3-
import { BitId } from '../../bit-id';
3+
import { BitId, BitIds } from '../../bit-id';
44
import { LATEST } from '../../constants';
55
import ShowDoctorError from '../../error/show-doctor-error';
66
import { ModelComponent } from '../../scope/models';
@@ -64,7 +64,14 @@ export class ComponentStatusLoader {
6464
// loadOne to not find model component as it assumes there is no version
6565
// also, don't leave the id as is, otherwise, it'll cause issues with import --merge, when
6666
// imported version is bigger than .bitmap, it won't find it and will consider as deleted
67-
componentFromFileSystem = await this.consumer.loadComponent(id.changeVersion(LATEST));
67+
const { components, removedComponents } = await this.consumer.loadComponents(
68+
new BitIds(id.changeVersion(LATEST))
69+
);
70+
if (removedComponents.length) {
71+
status.deleted = true;
72+
return status;
73+
}
74+
componentFromFileSystem = components[0];
6875
} catch (err: any) {
6976
if (
7077
err instanceof MissingFilesFromComponent ||

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ export default class CommandHelper {
201201
removeComponent(id: string, flags = '') {
202202
return this.runCmd(`bit remove ${id} --silent ${flags}`);
203203
}
204+
softRemoveComponent(id: string, flags = '') {
205+
return this.runCmd(`bit remove ${id} --silent --soft ${flags}`);
206+
}
204207
deprecateComponent(id: string, flags = '') {
205208
return this.runCmd(`bit deprecate ${id} ${flags}`);
206209
}

0 commit comments

Comments
 (0)