Skip to content

Commit

Permalink
fix(lane-merge), remove a package from workspace.jsonc policy if newl…
Browse files Browse the repository at this point in the history
…y introduced during merge (#8817)

The scenario is as follows:
Lane-A has comp-X in the workspace.jsonc policy, it is not part of the
lane.
Lane-B has this comp-X as part of the lane. 
Lane-B is now merged into Lane-A.
This PR removes the entry of comp-x package from workspace.jsonc, so
then this component doesn't exist in both: .bitmap and workspace.jsonc.
  • Loading branch information
davidfirst authored Apr 23, 2024
1 parent ddd47f0 commit 37a1d1f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
30 changes: 30 additions & 0 deletions e2e/harmony/lanes/merge-lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1522,4 +1522,34 @@ describe('merge lanes', function () {
expect(foo).to.include('<<<<<<<');
});
});
describe('merging lane with a component newly introduced where it was a package before', () => {
let laneAWs: string;
let comp2PkgName: string;
before(() => {
helper.scopeHelper.setNewLocalAndRemoteScopes();
helper.fixtures.populateComponents(1, false);
helper.command.createLane('lane-a');
helper.command.snapAllComponentsWithoutBuild();
helper.command.export();
laneAWs = helper.scopeHelper.cloneLocalScope();
helper.command.switchLocalLane('main');
helper.command.mergeLane('lane-a', '-x');
helper.command.export();
helper.fixtures.populateComponents(2);
helper.command.tagAllWithoutBuild();
helper.command.export();
helper.command.createLane('lane-b');
helper.command.snapAllComponentsWithoutBuild('--unmodified');
helper.command.export();
comp2PkgName = helper.general.getPackageNameByCompName('comp2', false);
helper.scopeHelper.getClonedLocalScope(laneAWs);
helper.npm.addFakeNpmPackage(comp2PkgName, '0.0.1');
helper.workspaceJsonc.addPolicyToDependencyResolver({ dependencies: { [comp2PkgName]: '0.0.1' } });
});
it('should remove the package from workspace.jsonc', () => {
helper.command.mergeLane('lane-b', '-x');
const policy = helper.workspaceJsonc.getPolicyFromDependencyResolver();
expect(policy.dependencies).to.not.have.property(comp2PkgName);
});
});
});
39 changes: 35 additions & 4 deletions scopes/component/merging/merging.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import {
WorkspaceConfigUpdateResult,
} from '@teambit/config-merger';
import { SnapsDistance } from '@teambit/legacy/dist/scope/component-ops/snaps-distance';
import componentIdToPackageName from '@teambit/legacy/dist/utils/bit/component-id-to-package-name';
import DependencyResolverAspect, { DependencyResolverMain } from '@teambit/dependency-resolver';
import { InstallMain, InstallAspect } from '@teambit/install';
import { ScopeAspect, ScopeMain } from '@teambit/scope';
import { MergeCmd } from './merge-cmd';
Expand Down Expand Up @@ -121,7 +123,8 @@ export class MergingMain {
private importer: ImporterMain,
private config: ConfigMain,
private remove: RemoveMain,
private configMerger: ConfigMergerMain
private configMerger: ConfigMergerMain,
private depResolver: DependencyResolverMain
) {}

async merge(
Expand Down Expand Up @@ -248,6 +251,8 @@ export class MergingMain {

const succeededComponents = allComponentsStatus.filter((componentStatus) => !componentStatus.unchangedMessage);

const currentLaneIdsBeforeMerge = currentLane?.toComponentIds();

const componentsResults = await this.applyVersionMultiple(
succeededComponents,
otherLaneId,
Expand Down Expand Up @@ -275,7 +280,10 @@ export class MergingMain {

await legacyScope.objects.unmergedComponents.write();

if (this.workspace) await consumer.writeBitMap(`merge ${otherLaneId.toString()}`);
if (this.workspace) {
await consumer.writeBitMap(`merge ${otherLaneId.toString()}`);
await this.removeFromWsJsonPolicyIfExists(componentsResults, currentLane, currentLaneIdsBeforeMerge);
}

if (componentIdsToRemove.length && this.workspace) {
const compBitIdsToRemove = ComponentIdList.fromArray(componentIdsToRemove);
Expand Down Expand Up @@ -341,6 +349,25 @@ export class MergingMain {
};
}

async removeFromWsJsonPolicyIfExists(
componentsResults: ApplyVersionWithComps[],
currentLane?: Lane,
currentLaneIdsBeforeMerge?: ComponentIdList
) {
const newlyIntroducedIds = currentLane
?.toComponentIds()
.filter((id) => !currentLaneIdsBeforeMerge?.hasWithoutVersion(id));
const newlyIntroducedComponentIds = ComponentIdList.fromArray(newlyIntroducedIds || []);
const components = compact(
componentsResults
.map((c) => c.legacyCompToWrite)
.filter((c) => c && newlyIntroducedComponentIds.hasWithoutVersion(c.id))
);
const packages = components.map((c) => componentIdToPackageName(c));
const isRemoved = this.depResolver.removeFromRootPolicy(packages);
if (isRemoved) await this.depResolver.persistConfig('merge (remove packages)');
}

/**
* this function gets called from two different commands:
* 1. "bit merge <ids...>", when merging a component from a remote to the local.
Expand Down Expand Up @@ -663,6 +690,7 @@ export class MergingMain {
RemoveAspect,
GlobalConfigAspect,
ConfigMergerAspect,
DependencyResolverAspect,
];
static runtime = MainRuntime;
static async provider([
Expand All @@ -679,6 +707,7 @@ export class MergingMain {
remove,
globalConfig,
configMerger,
depResolver,
]: [
CLIMain,
Workspace,
Expand All @@ -692,7 +721,8 @@ export class MergingMain {
ConfigMain,
RemoveMain,
GlobalConfigMain,
ConfigMergerMain
ConfigMergerMain,
DependencyResolverMain
]) {
const logger = loggerMain.createLogger(MergingAspect.id);
const merging = new MergingMain(
Expand All @@ -706,7 +736,8 @@ export class MergingMain {
importer,
config,
remove,
configMerger
configMerger,
depResolver
);
cli.register(new MergeCmd(merging, globalConfig));
return merging;
Expand Down

0 comments on commit 37a1d1f

Please sign in to comment.