@teambit/merging 1.0.107 → 1.0.108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,126 @@
1
+ import { DependencyResolverAspect } from '@teambit/dependency-resolver';
2
+ import { compact } from 'lodash';
3
+ import { MergeStrategyResult, conflictIndicator, GenericConfigOrRemoved } from './config-merger';
4
+
5
+ const DEP_RESOLVER_VERSION_INDENTATION = 8;
6
+ const CONFLICT_MARKER_INDENTATION = 7;
7
+
8
+ export class ConfigMergeResult {
9
+ constructor(
10
+ readonly compIdStr: string,
11
+ private currentLabel: string,
12
+ private otherLabel: string,
13
+ private results: MergeStrategyResult[]
14
+ ) {}
15
+ hasConflicts(): boolean {
16
+ return this.results.some((result) => result.conflict);
17
+ }
18
+ generateMergeConflictFile(): string | null {
19
+ const resultsWithConflict = this.results.filter((result) => result.conflict);
20
+ if (!resultsWithConflict.length) return null;
21
+ const allConflicts = compact(resultsWithConflict.map((result) => this.generateConflictStringPerAspect(result)));
22
+ const configMergeFormatted = allConflicts.map((c) => this.formatConflict(c));
23
+ const conflictStr = `{
24
+ ${this.concatenateConflicts(configMergeFormatted)}
25
+ }`;
26
+ return conflictStr;
27
+ }
28
+ getSuccessfullyMergedConfig(): Record<string, any> {
29
+ const resultsWithMergedConfig = this.results.filter((result) => result.mergedConfig);
30
+ return resultsWithMergedConfig.reduce((acc, curr) => {
31
+ const currObject = { [curr.id]: curr.mergedConfig };
32
+ return { ...acc, ...currObject };
33
+ }, {});
34
+ }
35
+
36
+ getDepsResolverResult(): MergeStrategyResult | undefined {
37
+ return this.results.find((result) => result.id === DependencyResolverAspect.id);
38
+ }
39
+
40
+ private generateConflictStringPerAspect(result: MergeStrategyResult): string | undefined {
41
+ if (!result.conflict) return undefined;
42
+ if (result.id === DependencyResolverAspect.id) {
43
+ return this.depsResolverConfigGenerator(result.conflict);
44
+ }
45
+ return this.basicConflictGenerator(result.id, result.conflict);
46
+ }
47
+
48
+ private depsResolverConfigGenerator(conflict: Record<string, any>): string {
49
+ const mergedConfigSplit = JSON.stringify({ policy: conflict }, undefined, 2).split('\n');
50
+ const conflictLines = mergedConfigSplit.map((line) => {
51
+ if (!line.includes(conflictIndicator)) return line;
52
+ const [, currentVal, otherVal] = line.split('::');
53
+ return `${'<'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.currentLabel}
54
+ ${' '.repeat(DEP_RESOLVER_VERSION_INDENTATION)}"version": "${currentVal}",
55
+ =======
56
+ ${' '.repeat(DEP_RESOLVER_VERSION_INDENTATION)}"version": "${otherVal}",
57
+ ${'>'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.otherLabel}`;
58
+ });
59
+ // replace the first line with line with the id
60
+ conflictLines.shift();
61
+ conflictLines.unshift(`"${DependencyResolverAspect.id}": {`);
62
+ return conflictLines.join('\n');
63
+ }
64
+
65
+ private basicConflictGenerator(id: string, conflictObj: Record<string, any>): string {
66
+ const { currentConfig, otherConfig } = conflictObj;
67
+ let conflict: string;
68
+ if (currentConfig === '-') {
69
+ conflict = `${'<'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.currentLabel}
70
+ "${id}": "-"
71
+ =======
72
+ "${id}": ${JSON.stringify(otherConfig, undefined, 2)}
73
+ ${'>'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.otherLabel}`;
74
+ } else if (otherConfig === '-') {
75
+ conflict = `${'<'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.currentLabel}
76
+ "${id}": ${JSON.stringify(currentConfig, undefined, 2)}
77
+ =======
78
+ "${id}": "-"
79
+ ${'>'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.otherLabel}`;
80
+ } else {
81
+ const formatConfig = (conf: GenericConfigOrRemoved) => {
82
+ const confStr = JSON.stringify(conf, undefined, 2);
83
+ const confStrSplit = confStr.split('\n');
84
+ confStrSplit.shift(); // remove first {
85
+ confStrSplit.pop(); // remove last }
86
+ return confStrSplit.join('\n');
87
+ };
88
+ conflict = `"${id}": {
89
+ ${'<'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.currentLabel}
90
+ ${formatConfig(currentConfig)}
91
+ =======
92
+ ${formatConfig(otherConfig)}
93
+ ${'>'.repeat(CONFLICT_MARKER_INDENTATION)} ${this.otherLabel}
94
+ }`;
95
+ }
96
+
97
+ return conflict;
98
+ }
99
+
100
+ private formatConflict(conflict: string) {
101
+ return (
102
+ conflict
103
+ .split('\n')
104
+ // add 2 spaces before each line
105
+ .map((line) => ` ${line}`)
106
+ // remove the white spaces before the conflict indicators
107
+ .map((line) => line.replace(/ *(<<<<<<<|>>>>>>>|=======)/g, '$1'))
108
+ .join('\n')
109
+ );
110
+ }
111
+
112
+ private concatenateConflicts(conflicts: string[]) {
113
+ const conflictsWithComma = conflicts.map((conflict, index) => {
114
+ if (index === conflicts.length - 1) return conflict; // last element in the array, no need to add a comma
115
+ if (conflict.endsWith('}')) return `${conflict},`; // ends normally with a closing brace, add a comma.
116
+ // if it doesn't end with a closing brace, it means it ends with a conflict indicator.
117
+ // the comma should be added after the last line with a closing brace.
118
+ const conflictSplit = conflict.split('\n');
119
+ // find the last line with '}' and add a comma after it
120
+ const lastLineWithClosingBrace = conflictSplit.lastIndexOf(' }');
121
+ conflictSplit[lastLineWithClosingBrace] += ',';
122
+ return conflictSplit.join('\n');
123
+ });
124
+ return conflictsWithComma.join('\n');
125
+ }
126
+ }
@@ -0,0 +1,568 @@
1
+ import { ComponentID } from '@teambit/component-id';
2
+ import semver from 'semver';
3
+ import { Logger } from '@teambit/logger';
4
+ import BuilderAspect from '@teambit/builder';
5
+ import { isHash } from '@teambit/component-version';
6
+ import {
7
+ DependencyResolverAspect,
8
+ SerializedDependency,
9
+ VariantPolicy,
10
+ VariantPolicyEntry,
11
+ } from '@teambit/dependency-resolver';
12
+ import { Lane } from '@teambit/legacy/dist/scope/models';
13
+ import { EnvsAspect } from '@teambit/envs';
14
+ import { ExtensionDataEntry, ExtensionDataList } from '@teambit/legacy/dist/consumer/config/extension-data';
15
+ import { compact, omit, uniqBy } from 'lodash';
16
+ import { ConfigMergeResult } from './config-merge-result';
17
+
18
+ export type GenericConfigOrRemoved = Record<string, any> | '-';
19
+
20
+ type EnvData = { id: string; version?: string; config?: GenericConfigOrRemoved };
21
+
22
+ type SerializedDependencyWithPolicy = SerializedDependency & { policy?: string; packageName?: string };
23
+
24
+ export const conflictIndicator = 'CONFLICT::';
25
+
26
+ export type MergeStrategyResult = {
27
+ id: string;
28
+ mergedConfig?: GenericConfigOrRemoved;
29
+ conflict?: Record<string, any>;
30
+ };
31
+ type MergeStrategyParamsWithRemoved = {
32
+ id: string;
33
+ currentConfig: GenericConfigOrRemoved;
34
+ otherConfig: GenericConfigOrRemoved;
35
+ baseConfig?: GenericConfigOrRemoved;
36
+ };
37
+ type MergeStrategyParams = {
38
+ id: string;
39
+ currentExt: ExtensionDataEntry;
40
+ otherExt: ExtensionDataEntry;
41
+ baseExt?: ExtensionDataEntry;
42
+ };
43
+
44
+ /**
45
+ * perform 3-way merge of component configuration (aspects).
46
+ * normally this is needed when merging one lane into another. the component may have different aspects config in each lane.
47
+ * the baseAspects are the aspects of the component in the diversion point (the common ancestor of the two lanes).
48
+ * the currentAspects are the aspects of the component in the current lane.
49
+ * the otherAspects are the aspects of the component in the other lane. this is the lane we merge into the current lane.
50
+ *
51
+ * the basic merging strategy is a simple comparison between the aspect-configs, if they're different, we have a conflict.
52
+ * we have two special cases:
53
+ *
54
+ * 1. dependency-resolver: we do a deeper check for the policy, we compare each dependency separately. also, we take
55
+ * into account not only the config, but also the data. this is needed because some dependencies are automatically
56
+ * added by Bit (from the import statements in the code) and they're not in the config. the final config has the deps
57
+ * from both sources, the config and the data. The way we know to differentiate between them is by the "force" prop.
58
+ * the config has always force: true.
59
+ *
60
+ * 2. envs: if we don't treat it specially, the user will need to make the change not only in the envs aspect, but also
61
+ * in the deps-resolver (because the env is added as a devDependency) and also in the aspect itself (because
62
+ * teambit.envs/env has only the id and not the version). to make it simpler, we ignore the envs in the deps-resolver
63
+ * we ignore the individual aspect that is the env itself. we only show teambit.envs/env and we put the env id and
64
+ * version. later, when the component is loaded, we split the id and the version and put them in the correct places.
65
+ * see workspace.componentExtension / adjustEnvsOnConfigMerge for more details.
66
+ */
67
+ export class ConfigMerger {
68
+ private currentEnv: EnvData;
69
+ private otherEnv: EnvData;
70
+ private baseEnv?: EnvData;
71
+ private handledExtIds: string[] = [BuilderAspect.id]; // don't try to merge builder, it's possible that at one end it wasn't built yet, so it's empty
72
+ private otherLaneIdsStr: string[];
73
+ constructor(
74
+ private compIdStr: string,
75
+ private workspaceIds: ComponentID[],
76
+ otherLane: Lane | undefined,
77
+ private currentAspects: ExtensionDataList,
78
+ private baseAspects: ExtensionDataList,
79
+ private otherAspects: ExtensionDataList,
80
+ private currentLabel: string,
81
+ private otherLabel: string,
82
+ private logger: Logger
83
+ ) {
84
+ this.otherLaneIdsStr = otherLane?.toBitIds().map((id) => id.toString()) || [];
85
+ }
86
+
87
+ merge(): ConfigMergeResult {
88
+ this.logger.debug(`\n************** start config-merger for ${this.compIdStr} **************`);
89
+ this.logger.debug(`currentLabel: ${this.currentLabel}`);
90
+ this.logger.debug(`otherLabel: ${this.otherLabel}`);
91
+ this.populateEnvs();
92
+ const results = this.currentAspects.map((currentExt) => {
93
+ const id = currentExt.stringId;
94
+ if (this.handledExtIds.includes(id)) return null;
95
+ this.handledExtIds.push(id);
96
+ const baseExt = this.baseAspects.findExtension(id, true);
97
+ const otherExt = this.otherAspects.findExtension(id, true);
98
+ if (otherExt) {
99
+ // try to 3-way-merge
100
+ return this.mergePerStrategy({ id, currentExt, otherExt, baseExt });
101
+ }
102
+ // exist in current but not in other
103
+ if (baseExt) {
104
+ // was removed on other
105
+ return { id, conflict: { currentConfig: this.getConfig(currentExt), otherConfig: '-' } };
106
+ }
107
+ // exist in current but not in other and base, so it got created on current. nothing to do.
108
+ return null;
109
+ });
110
+ const otherAspectsNotHandledResults = this.otherAspects.map((otherExt) => {
111
+ let id = otherExt.stringId;
112
+ if (this.handledExtIds.includes(id)) return null;
113
+ this.handledExtIds.push(id);
114
+ if (otherExt.extensionId && otherExt.extensionId.hasVersion()) {
115
+ // avoid using the id from the other lane if it exits in the workspace. prefer the id from the workspace.
116
+ const idFromWorkspace = this.getIdFromWorkspace(otherExt.extensionId.toStringWithoutVersion());
117
+ if (idFromWorkspace) {
118
+ const existingExt = this.currentAspects.findExtension(otherExt.extensionId.toStringWithoutVersion(), true);
119
+ if (existingExt) return null; // the aspect is set currently, no need to add it again.
120
+ id = idFromWorkspace._legacy.toString();
121
+ }
122
+ }
123
+ const baseExt = this.baseAspects.findExtension(id, true);
124
+ if (baseExt) {
125
+ // was removed on current
126
+ return { id, conflict: { currentConfig: '-', otherConfig: this.getConfig(otherExt) } };
127
+ }
128
+ // exist in other but not in current and base, so it got created on other.
129
+ return { id, mergedConfig: this.getConfig(otherExt) };
130
+ });
131
+ const envResult = [this.envStrategy()] || [];
132
+ this.logger.debug(`*** end config-merger for ${this.compIdStr} ***\n`);
133
+ return new ConfigMergeResult(
134
+ this.compIdStr,
135
+ this.currentLabel,
136
+ this.otherLabel,
137
+ compact([...results, ...otherAspectsNotHandledResults, ...envResult])
138
+ );
139
+ }
140
+
141
+ private populateEnvs() {
142
+ // populate ids
143
+ const getEnvId = (ext: ExtensionDataList) => {
144
+ const envsAspect = ext.findCoreExtension(EnvsAspect.id);
145
+ if (!envsAspect) throw new Error(`unable to find ${EnvsAspect.id} aspect for ${this.compIdStr}`);
146
+ const env = envsAspect.config.env || envsAspect.data.id;
147
+ if (!env)
148
+ throw new Error(`unable to find env for ${this.compIdStr}, the config and data of ${EnvsAspect.id} are empty}`);
149
+ return env;
150
+ };
151
+ const currentEnv = getEnvId(this.currentAspects);
152
+ this.currentEnv = { id: currentEnv };
153
+ const otherEnv = getEnvId(this.otherAspects);
154
+ this.otherEnv = { id: otherEnv };
155
+ const baseEnv = this.baseAspects ? getEnvId(this.baseAspects) : undefined;
156
+ if (baseEnv) this.baseEnv = { id: baseEnv };
157
+
158
+ // populate version
159
+ const currentEnvAspect = this.currentAspects.findExtension(currentEnv, true);
160
+ if (currentEnvAspect) {
161
+ this.handledExtIds.push(currentEnvAspect.stringId);
162
+ this.currentEnv.version = currentEnvAspect.extensionId?.version;
163
+ this.currentEnv.config = this.getConfig(currentEnvAspect);
164
+ }
165
+ const otherEnvAspect = this.otherAspects.findExtension(otherEnv, true);
166
+ if (otherEnvAspect) {
167
+ this.handledExtIds.push(otherEnvAspect.stringId);
168
+ this.otherEnv.version = otherEnvAspect.extensionId?.version;
169
+ this.otherEnv.config = this.getConfig(otherEnvAspect);
170
+ }
171
+ if (this.baseEnv) {
172
+ const baseEnvAspect = this.baseAspects.findExtension(baseEnv, true);
173
+ if (baseEnvAspect) {
174
+ this.baseEnv.version = baseEnvAspect.extensionId?.version;
175
+ this.baseEnv.config = this.getConfig(baseEnvAspect);
176
+ }
177
+ }
178
+ }
179
+
180
+ private envStrategy(): MergeStrategyResult | null {
181
+ const mergeStrategyParams: MergeStrategyParamsWithRemoved = {
182
+ id: EnvsAspect.id,
183
+ currentConfig: {
184
+ env: this.currentEnv.version ? `${this.currentEnv.id}@${this.currentEnv.version}` : this.currentEnv.id,
185
+ },
186
+ otherConfig: { env: this.otherEnv.version ? `${this.otherEnv.id}@${this.otherEnv.version}` : this.otherEnv.id },
187
+ };
188
+ if (this.baseEnv) {
189
+ mergeStrategyParams.baseConfig = {
190
+ env: this.baseEnv?.version ? `${this.baseEnv.id}@${this.baseEnv.version}` : this.baseEnv?.id,
191
+ };
192
+ }
193
+ if (this.currentEnv.id === this.otherEnv.id && this.currentEnv.version === this.otherEnv.version) {
194
+ return null;
195
+ }
196
+ if (this.isIdInWorkspaceOrOtherLane(this.currentEnv.id, this.otherEnv.version)) {
197
+ // the env currently used is part of the workspace, that's what the user needs. don't try to resolve anything.
198
+ return null;
199
+ }
200
+ return this.basicConfigMerge(mergeStrategyParams);
201
+ }
202
+
203
+ private areConfigsEqual(configA: GenericConfigOrRemoved, configB: GenericConfigOrRemoved) {
204
+ return JSON.stringify(configA) === JSON.stringify(configB);
205
+ }
206
+
207
+ private mergePerStrategy(mergeStrategyParams: MergeStrategyParams): MergeStrategyResult | null {
208
+ const { id, currentExt, otherExt, baseExt } = mergeStrategyParams;
209
+ const depResolverResult = this.depResolverStrategy(mergeStrategyParams);
210
+
211
+ if (depResolverResult) {
212
+ // if (depResolverResult.mergedConfig || depResolverResult?.conflict) console.log("\n\nDepResolverResult", this.compIdStr, '\n', JSON.stringify(depResolverResult, undefined, 2))
213
+ return depResolverResult;
214
+ }
215
+ const currentConfig = this.getConfig(currentExt);
216
+ const otherConfig = this.getConfig(otherExt);
217
+ const baseConfig = baseExt ? this.getConfig(baseExt) : undefined;
218
+
219
+ return this.basicConfigMerge({ id, currentConfig, otherConfig, baseConfig });
220
+ }
221
+
222
+ private basicConfigMerge(mergeStrategyParams: MergeStrategyParamsWithRemoved) {
223
+ const { id, currentConfig, otherConfig, baseConfig } = mergeStrategyParams;
224
+ if (this.areConfigsEqual(currentConfig, otherConfig)) {
225
+ return null;
226
+ }
227
+ if (baseConfig && this.areConfigsEqual(baseConfig, otherConfig)) {
228
+ // was changed on current
229
+ return null;
230
+ }
231
+ if (baseConfig && this.areConfigsEqual(baseConfig, currentConfig)) {
232
+ // was changed on other
233
+ return { id, mergedConfig: otherConfig };
234
+ }
235
+ // either no baseConfig, or baseConfig is also different from both: other and local. that's a conflict.
236
+ return { id, conflict: { currentConfig, otherConfig, baseConfig } };
237
+ }
238
+
239
+ private depResolverStrategy(params: MergeStrategyParams): MergeStrategyResult | undefined {
240
+ if (params.id !== DependencyResolverAspect.id) return undefined;
241
+ this.logger.trace(`start depResolverStrategy for ${this.compIdStr}`);
242
+ const { currentExt, otherExt, baseExt } = params;
243
+
244
+ const currentConfig = this.getConfig(currentExt);
245
+ const currentConfigPolicy = this.getPolicy(currentConfig);
246
+ const otherConfig = this.getConfig(otherExt);
247
+ const otherConfigPolicy = this.getPolicy(otherConfig);
248
+
249
+ const baseConfig = baseExt ? this.getConfig(baseExt) : undefined;
250
+ const baseConfigPolicy = baseConfig ? this.getPolicy(baseConfig) : undefined;
251
+
252
+ this.logger.debug(`currentConfig, ${JSON.stringify(currentConfig, undefined, 2)}`);
253
+ this.logger.debug(`otherConfig, ${JSON.stringify(otherConfig, undefined, 2)}`);
254
+ this.logger.debug(`baseConfig, ${JSON.stringify(baseConfig, undefined, 2)}`);
255
+
256
+ const getAllDeps = (ext: ExtensionDataList): SerializedDependencyWithPolicy[] => {
257
+ const data = ext.findCoreExtension(DependencyResolverAspect.id)?.data.dependencies;
258
+ if (!data) return [];
259
+ const policy = ext.findCoreExtension(DependencyResolverAspect.id)?.data.policy || [];
260
+ return data.map((d) => {
261
+ const idWithoutVersion = d.__type === 'package' ? d.id : d.id.split('@')[0];
262
+ const existingPolicy = policy.find((p) => p.dependencyId === idWithoutVersion);
263
+ const getPolicyVer = () => {
264
+ if (d.__type === 'package') return undefined; // for packages, the policy is already the version
265
+ if (existingPolicy) return existingPolicy.value.version; // currently it's missing, will be implemented by @Gilad
266
+ return d.version;
267
+ // if (!semver.valid(d.version)) return d.version; // could be a hash
268
+ // // default to `^` or ~ if starts with zero, until we save the policy from the workspace during tag/snap.
269
+ // return d.version.startsWith('0.') ? `~${d.version}` : `^${d.version}`;
270
+ };
271
+ return {
272
+ ...d,
273
+ id: idWithoutVersion,
274
+ policy: getPolicyVer(),
275
+ };
276
+ });
277
+ };
278
+ const getDataPolicy = (ext: ExtensionDataList): VariantPolicyEntry[] => {
279
+ return ext.findCoreExtension(DependencyResolverAspect.id)?.data.policy || [];
280
+ };
281
+
282
+ const getAutoDeps = (ext: ExtensionDataList): SerializedDependencyWithPolicy[] => {
283
+ const allDeps = getAllDeps(ext);
284
+ return allDeps.filter((d) => d.source === 'auto');
285
+ };
286
+
287
+ const currentAutoData = getAutoDeps(this.currentAspects);
288
+ const currentAllData = getAllDeps(this.currentAspects);
289
+ const currentDataPolicy = getDataPolicy(this.currentAspects);
290
+ const otherData = getAutoDeps(this.otherAspects);
291
+ const currentAndOtherData = uniqBy(currentAutoData.concat(otherData), (d) => d.id);
292
+ const currentAndOtherComponentsData = currentAndOtherData.filter((c) => c.__type === 'component');
293
+ const baseData = getAutoDeps(this.baseAspects);
294
+
295
+ const getCompIdStrByPkgNameFromData = (pkgName: string): string | undefined => {
296
+ const found = currentAndOtherComponentsData.find((d) => d.packageName === pkgName);
297
+ return found?.id;
298
+ };
299
+
300
+ const getFromCurrentDataByPackageName = (pkgName: string) => {
301
+ return currentAllData.find((d) => {
302
+ if (d.__type === 'package') return d.id === pkgName;
303
+ return d.packageName === pkgName;
304
+ });
305
+ };
306
+
307
+ const getFromCurrentDataPolicyByPackageName = (pkgName: string) => {
308
+ return currentDataPolicy.find((d) => d.dependencyId === pkgName);
309
+ };
310
+
311
+ const mergedPolicy = {
312
+ dependencies: [],
313
+ devDependencies: [],
314
+ peerDependencies: [],
315
+ };
316
+ const conflictedPolicy = {
317
+ dependencies: [],
318
+ devDependencies: [],
319
+ peerDependencies: [],
320
+ };
321
+ let hasConflict = false;
322
+ const lifecycleToDepType = {
323
+ runtime: 'dependencies',
324
+ dev: 'devDependencies',
325
+ peer: 'peerDependencies',
326
+ };
327
+ const handleConfigMerge = () => {
328
+ const addVariantPolicyEntryToPolicy = (dep: VariantPolicyEntry) => {
329
+ const compIdStr = getCompIdStrByPkgNameFromData(dep.dependencyId);
330
+ if (compIdStr && this.isIdInWorkspaceOrOtherLane(compIdStr, dep.value.version)) {
331
+ // no need to add if the id exists in the workspace (regardless the version)
332
+ return;
333
+ }
334
+ const fromCurrentData = getFromCurrentDataByPackageName(dep.dependencyId);
335
+ if (fromCurrentData && !dep.force) {
336
+ if (fromCurrentData.version === dep.value.version) return;
337
+ if (
338
+ !isHash(fromCurrentData.version) &&
339
+ !isHash(dep.value.version) &&
340
+ semver.satisfies(fromCurrentData.version, dep.value.version)
341
+ ) {
342
+ return;
343
+ }
344
+ }
345
+ const fromCurrentDataPolicy = getFromCurrentDataPolicyByPackageName(dep.dependencyId);
346
+ if (fromCurrentDataPolicy && fromCurrentDataPolicy.value.version === dep.value.version) {
347
+ // -- updated comment --
348
+ // not sure why this block is needed. this gets called also from this if: `if (baseConfig && this.areConfigsEqual(baseConfig, currentConfig)) {`
349
+ // and in this case, it's possible that current/base has 5 deps, and other just added one and it has 6.
350
+ // in which case, we do need to add all these 5 in additional to the new one. otherwise, only the new one appears in the final
351
+ // merged object, and all the 5 deps are lost.
352
+ // --- previous comment ---
353
+ // that's a bug. if it's in the data.policy, it should be in data.dependencies.
354
+ // return;
355
+ }
356
+ const depType = lifecycleToDepType[dep.lifecycleType];
357
+ mergedPolicy[depType].push({
358
+ name: dep.dependencyId,
359
+ version: dep.value.version,
360
+ force: dep.force,
361
+ });
362
+ };
363
+
364
+ if (this.areConfigsEqual(currentConfig, otherConfig)) {
365
+ return;
366
+ }
367
+ if (baseConfig && this.areConfigsEqual(baseConfig, otherConfig)) {
368
+ // was changed on current
369
+ return;
370
+ }
371
+ if (currentConfig === '-' || otherConfig === '-') {
372
+ throw new Error('not implemented. Is it possible to have it as minus?');
373
+ }
374
+ if (baseConfig && this.areConfigsEqual(baseConfig, currentConfig)) {
375
+ // was changed on other
376
+ if (otherConfigPolicy.length) {
377
+ otherConfigPolicy.forEach((dep) => {
378
+ addVariantPolicyEntryToPolicy(dep);
379
+ });
380
+ }
381
+ return;
382
+ }
383
+
384
+ // either no baseConfig, or baseConfig is also different from both: other and local. that's a conflict.
385
+ if (!currentConfig.policy && !otherConfig.policy) return;
386
+ const currentAndOtherConfig = uniqBy(currentConfigPolicy.concat(otherConfigPolicy), (d) => d.dependencyId);
387
+ currentAndOtherConfig.forEach((dep) => {
388
+ const depType = lifecycleToDepType[dep.lifecycleType];
389
+ const currentDep = currentConfigPolicy.find((d) => d.dependencyId === dep.dependencyId);
390
+ const otherDep = otherConfigPolicy.find((d) => d.dependencyId === dep.dependencyId);
391
+ const baseDep = baseConfigPolicy?.find((d) => d.dependencyId === dep.dependencyId);
392
+
393
+ if (!otherDep) {
394
+ return;
395
+ }
396
+ if (!currentDep) {
397
+ // only on other
398
+ addVariantPolicyEntryToPolicy(otherDep);
399
+ return;
400
+ }
401
+ const currentVer = currentDep.value.version;
402
+ const otherVer = otherDep.value.version;
403
+ if (currentVer === otherVer) {
404
+ return;
405
+ }
406
+ const baseVer = baseDep?.value.version;
407
+ if (baseVer && baseVer === otherVer) {
408
+ return;
409
+ }
410
+ if (baseVer && baseVer === currentVer) {
411
+ addVariantPolicyEntryToPolicy(otherDep);
412
+ return;
413
+ }
414
+ const compIdStr = getCompIdStrByPkgNameFromData(dep.dependencyId);
415
+ if (compIdStr && this.isIdInWorkspaceOrOtherLane(compIdStr, otherVer)) {
416
+ // no need to add if the id exists in the workspace (regardless the version)
417
+ return;
418
+ }
419
+
420
+ hasConflict = true;
421
+ conflictedPolicy[depType].push({
422
+ name: currentDep.dependencyId,
423
+ version: `${conflictIndicator}${currentVer}::${otherVer}::`,
424
+ force: currentDep.force,
425
+ });
426
+ });
427
+ };
428
+
429
+ handleConfigMerge();
430
+
431
+ const hasConfigForDep = (depType: string, depName: string) => mergedPolicy[depType].find((d) => d.name === depName);
432
+ const getDepIdAsPkgName = (dep: SerializedDependencyWithPolicy) => {
433
+ if (dep.__type !== 'component') {
434
+ return dep.id;
435
+ }
436
+ // @ts-ignore
437
+ return dep.packageName;
438
+ };
439
+
440
+ const addSerializedDepToPolicy = (dep: SerializedDependencyWithPolicy) => {
441
+ const depType = lifecycleToDepType[dep.lifecycle];
442
+ if (dep.__type === 'component' && this.isIdInWorkspaceOrOtherLane(dep.id, dep.version)) {
443
+ return;
444
+ }
445
+ if (hasConfigForDep(depType, dep.id)) {
446
+ return; // there is already config for it.
447
+ }
448
+ mergedPolicy[depType].push({
449
+ name: getDepIdAsPkgName(dep),
450
+ version: dep.policy || dep.version,
451
+ force: false,
452
+ });
453
+ };
454
+
455
+ this.logger.debug(
456
+ `currentData, ${currentAllData.length}\n${currentAllData
457
+ .map((d) => `${d.__type} ${d.id} ${d.version}`)
458
+ .join('\n')}`
459
+ );
460
+ this.logger.debug(
461
+ `otherData, ${otherData.length}\n${otherData.map((d) => `${d.__type} ${d.id} ${d.version}`).join('\n')}`
462
+ );
463
+ this.logger.debug(
464
+ `baseData, ${baseData.length}\n${baseData.map((d) => `${d.__type} ${d.id} ${d.version}`).join('\n')}`
465
+ );
466
+
467
+ // eslint-disable-next-line complexity
468
+ currentAndOtherData.forEach((depData) => {
469
+ this.logger.trace(`depData.id, ${depData.id}`);
470
+ if (this.isEnv(depData.id)) {
471
+ // ignore the envs
472
+ return;
473
+ }
474
+ const currentDep = currentAllData.find((d) => d.id === depData.id);
475
+ const otherDep = otherData.find((d) => d.id === depData.id);
476
+ const baseDep = baseData.find((d) => d.id === depData.id);
477
+
478
+ this.logger.trace(`currentDep`, currentDep);
479
+ this.logger.trace(`otherDep`, otherDep);
480
+ this.logger.trace(`baseDep`, baseDep);
481
+ if (!otherDep) {
482
+ return;
483
+ }
484
+ if (!currentDep) {
485
+ if (baseDep) {
486
+ // exists in other and base, so it was removed from current
487
+ return;
488
+ }
489
+ // only on other
490
+ addSerializedDepToPolicy(otherDep);
491
+ return;
492
+ }
493
+
494
+ if (currentDep.policy && otherDep.policy) {
495
+ if (semver.satisfies(currentDep.version, otherDep.policy)) {
496
+ return;
497
+ }
498
+ if (semver.satisfies(otherDep.version, currentDep.policy)) {
499
+ return;
500
+ }
501
+ }
502
+
503
+ const currentVer = currentDep.policy || currentDep.version;
504
+ const otherVer = otherDep.policy || otherDep.version;
505
+ if (currentVer === otherVer) {
506
+ return;
507
+ }
508
+ const baseVer = baseDep?.policy || baseDep?.version;
509
+ if (baseVer && baseVer === otherVer) {
510
+ return;
511
+ }
512
+ const currentId = currentDep.id;
513
+ if (currentDep.__type === 'component' && this.isIdInWorkspaceOrOtherLane(currentId, otherDep.version)) {
514
+ // dependencies that exist in the workspace, should be ignored. they'll be resolved later to the version in the ws.
515
+ return;
516
+ }
517
+ const depType = lifecycleToDepType[currentDep.lifecycle];
518
+ if (hasConfigForDep(depType, currentDep.id)) {
519
+ return; // there is already config for it.
520
+ }
521
+ if (baseVer && baseVer === currentVer) {
522
+ addSerializedDepToPolicy(otherDep);
523
+ return;
524
+ }
525
+ hasConflict = true;
526
+ conflictedPolicy[depType].push({
527
+ name: getDepIdAsPkgName(currentDep),
528
+ version: `${conflictIndicator}${currentVer}::${otherVer}::`,
529
+ force: false,
530
+ });
531
+ });
532
+
533
+ ['dependencies', 'devDependencies', 'peerDependencies'].forEach((depType) => {
534
+ if (!mergedPolicy[depType].length) delete mergedPolicy[depType];
535
+ if (!conflictedPolicy[depType].length) delete conflictedPolicy[depType];
536
+ });
537
+
538
+ const config = Object.keys(mergedPolicy).length ? { policy: mergedPolicy } : undefined;
539
+ const conflict = hasConflict ? conflictedPolicy : undefined;
540
+
541
+ this.logger.debug('final mergedConfig', config);
542
+ this.logger.debug('final conflict', conflict);
543
+
544
+ return { id: params.id, mergedConfig: config, conflict };
545
+ }
546
+
547
+ private isIdInWorkspaceOrOtherLane(id: string, versionOnOtherLane?: string): boolean {
548
+ return Boolean(this.getIdFromWorkspace(id)) || this.otherLaneIdsStr.includes(`${id}@${versionOnOtherLane}`);
549
+ }
550
+
551
+ private getIdFromWorkspace(id: string): ComponentID | undefined {
552
+ return this.workspaceIds.find((c) => c.toStringWithoutVersion() === id);
553
+ }
554
+
555
+ private isEnv(id: string) {
556
+ return id === this.currentEnv.id || id === this.otherEnv.id;
557
+ }
558
+
559
+ private getConfig(ext: ExtensionDataEntry): GenericConfigOrRemoved {
560
+ if (ext.rawConfig === '-') return ext.rawConfig;
561
+ return omit(ext.rawConfig, ['__specific']);
562
+ }
563
+
564
+ private getPolicy(config): VariantPolicyEntry[] {
565
+ if (!config.policy) return [];
566
+ return VariantPolicy.fromConfigObject(config.policy).entries;
567
+ }
568
+ }