@teambit/workspace 1.0.105 → 1.0.106

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.
@@ -4,6 +4,20 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.WorkspaceComponentLoader = void 0;
7
+ function _pMap() {
8
+ const data = _interopRequireDefault(require("p-map"));
9
+ _pMap = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _concurrency() {
15
+ const data = require("@teambit/legacy/dist/utils/concurrency");
16
+ _concurrency = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
7
21
  function _component() {
8
22
  const data = require("@teambit/component");
9
23
  _component = function () {
@@ -124,29 +138,389 @@ function _mergeConfigConflict() {
124
138
  return data;
125
139
  }
126
140
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
141
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
142
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
127
143
  function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
128
144
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
129
145
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
130
146
  class WorkspaceComponentLoader {
131
147
  // cache loaded components
132
- constructor(workspace, logger, dependencyResolver, envs) {
148
+ constructor(workspace, logger, dependencyResolver, envs, aspectLoader) {
133
149
  this.workspace = workspace;
134
150
  this.logger = logger;
135
151
  this.dependencyResolver = dependencyResolver;
136
152
  this.envs = envs;
153
+ this.aspectLoader = aspectLoader;
137
154
  _defineProperty(this, "componentsCache", void 0);
155
+ // cache loaded components
156
+ /**
157
+ * Cache components that loaded from scope (especially for get many for perf improvements)
158
+ */
159
+ _defineProperty(this, "scopeComponentsCache", void 0);
160
+ /**
161
+ * Cache extension list for components. used by get many for perf improvements.
162
+ * And to make sure we load extensions first.
163
+ */
164
+ _defineProperty(this, "componentsExtensionsCache", void 0);
165
+ _defineProperty(this, "componentLoadedSelfAsAspects", void 0);
138
166
  this.componentsCache = (0, _cacheFactory().createInMemoryCache)({
139
167
  maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
140
168
  });
169
+ this.scopeComponentsCache = (0, _cacheFactory().createInMemoryCache)({
170
+ maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
171
+ });
172
+ this.componentsExtensionsCache = (0, _cacheFactory().createInMemoryCache)({
173
+ maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
174
+ });
175
+ this.componentLoadedSelfAsAspects = (0, _cacheFactory().createInMemoryCache)({
176
+ maxSize: (0, _inMemoryCache().getMaxSizeForComponents)()
177
+ });
141
178
  }
142
179
  async getMany(ids, loadOpts, throwOnFailure = true) {
143
180
  const idsWithoutEmpty = (0, _lodash().compact)(ids);
144
- const errors = [];
181
+ this.logger.setStatusLine(`loading ${ids.length} component(s)`);
182
+ const loadOptsWithDefaults = Object.assign(
183
+ // We don't want to load extension or execute the load slot at this step
184
+ // we will do it later
185
+ // this important for better performance
186
+ // We don't want to resolveExtensionsVersions as with get many we call aspect merger merge before update dependencies
187
+ // so we will have the correct versions for extensions already and update them after will resolve wrong versions
188
+ // in some cases
189
+ {
190
+ loadExtensions: false,
191
+ executeLoadSlot: false,
192
+ loadSeedersAsAspects: true,
193
+ resolveExtensionsVersions: false
194
+ }, loadOpts || {});
195
+ const loadOrCached = {
196
+ idsToLoad: [],
197
+ fromCache: []
198
+ };
199
+ idsWithoutEmpty.forEach(id => {
200
+ const componentFromCache = this.getFromCache(id, loadOptsWithDefaults);
201
+ if (componentFromCache) {
202
+ loadOrCached.fromCache.push(componentFromCache);
203
+ } else {
204
+ loadOrCached.idsToLoad.push(id);
205
+ }
206
+ }, loadOrCached);
207
+ const {
208
+ components: loadedComponents,
209
+ invalidComponents
210
+ } = await this.getAndLoadSlotOrdered(loadOrCached.idsToLoad || [], loadOptsWithDefaults, throwOnFailure);
211
+ const components = [...loadedComponents, ...loadOrCached.fromCache];
212
+
213
+ // this.logger.clearStatusLine();
214
+ return {
215
+ components,
216
+ invalidComponents
217
+ };
218
+ }
219
+ async getAndLoadSlotOrdered(ids, loadOpts, throwOnFailure = true) {
220
+ if (!(ids !== null && ids !== void 0 && ids.length)) return {
221
+ components: [],
222
+ invalidComponents: []
223
+ };
224
+ const workspaceScopeIdsMap = await this.groupAndUpdateIds(ids);
225
+ const groupsToHandle = await this.buildLoadGroups(workspaceScopeIdsMap);
226
+ const groupsRes = (0, _lodash().compact)(await (0, _pMapSeries().default)(groupsToHandle, async group => {
227
+ const {
228
+ scopeIds,
229
+ workspaceIds,
230
+ aspects,
231
+ core,
232
+ seeders
233
+ } = group;
234
+ if (!workspaceIds.length && !scopeIds.length) return undefined;
235
+ const res = await this.getAndLoadSlot(workspaceIds, scopeIds, _objectSpread(_objectSpread({}, loadOpts), {}, {
236
+ core,
237
+ seeders,
238
+ aspects
239
+ }), throwOnFailure);
240
+ // We don't want to return components that were not asked originally (we do want to load them)
241
+ if (!group.seeders) return undefined;
242
+ return res;
243
+ }));
244
+ const finalRes = groupsRes.reduce((acc, curr) => {
245
+ return {
246
+ components: [...acc.components, ...curr.components],
247
+ invalidComponents: [...acc.invalidComponents, ...curr.invalidComponents]
248
+ };
249
+ }, {
250
+ components: [],
251
+ invalidComponents: []
252
+ });
253
+ return finalRes;
254
+ }
255
+ async buildLoadGroups(workspaceScopeIdsMap) {
256
+ const allIds = [...workspaceScopeIdsMap.workspaceIds.values(), ...workspaceScopeIdsMap.scopeIds.values()];
257
+ const groupedByIsCoreEnvs = (0, _lodash().groupBy)(allIds, id => {
258
+ return this.envs.isCoreEnv(id.toStringWithoutVersion());
259
+ });
260
+ await this.populateScopeAndExtensionsCache(groupedByIsCoreEnvs.false || [], workspaceScopeIdsMap);
261
+ const allExtIds = new Map();
262
+ groupedByIsCoreEnvs.false.forEach(id => {
263
+ const idStr = id.toString();
264
+ const fromCache = this.componentsExtensionsCache.get(idStr);
265
+ if (!fromCache || !fromCache.extensions) {
266
+ return;
267
+ }
268
+ fromCache.extensions.forEach(ext => {
269
+ if (!allExtIds.has(ext.stringId) && ext.newExtensionId) {
270
+ allExtIds.set(ext.stringId, ext.newExtensionId);
271
+ }
272
+ });
273
+ });
274
+ const allExtCompIds = Array.from(allExtIds.values());
275
+ await this.populateScopeAndExtensionsCache(allExtCompIds || [], workspaceScopeIdsMap);
276
+ const allExtIdsStr = allExtCompIds.map(id => id.toString());
277
+ const groupedByIsExtOfAnother = (0, _lodash().groupBy)(groupedByIsCoreEnvs.false, id => {
278
+ return allExtIdsStr.includes(id.toString());
279
+ });
280
+ const extIdsFromTheList = (groupedByIsExtOfAnother.true || []).map(id => id.toString());
281
+ const extsNotFromTheList = [];
282
+ for (const [, id] of allExtIds.entries()) {
283
+ if (!extIdsFromTheList.includes(id.toString())) {
284
+ extsNotFromTheList.push(id);
285
+ }
286
+ }
287
+ await this.groupAndUpdateIds(extsNotFromTheList, workspaceScopeIdsMap);
288
+ const layerdExtFromTheList = this.regroupExtIdsFromTheList(groupedByIsExtOfAnother.true);
289
+ const layerdExtGroups = layerdExtFromTheList.map(ids => {
290
+ return {
291
+ ids,
292
+ core: false,
293
+ aspects: true,
294
+ seeders: true
295
+ };
296
+ });
297
+ const groupsToHandle = [
298
+ // Always load first core envs
299
+ {
300
+ ids: groupedByIsCoreEnvs.true || [],
301
+ core: true,
302
+ aspects: true,
303
+ seeders: false
304
+ }, {
305
+ ids: extsNotFromTheList || [],
306
+ core: false,
307
+ aspects: true,
308
+ seeders: false
309
+ }, ...layerdExtGroups, {
310
+ ids: groupedByIsExtOfAnother.false || [],
311
+ core: false,
312
+ aspects: false,
313
+ seeders: true
314
+ }];
315
+ const groupsByWsScope = groupsToHandle.map(group => {
316
+ const groupedByWsScope = (0, _lodash().groupBy)(group.ids, id => {
317
+ return workspaceScopeIdsMap.workspaceIds.has(id.toString());
318
+ });
319
+ return {
320
+ workspaceIds: groupedByWsScope.true || [],
321
+ scopeIds: groupedByWsScope.false || [],
322
+ core: group.core,
323
+ aspects: group.aspects,
324
+ seeders: group.seeders
325
+ };
326
+ });
327
+ return groupsByWsScope;
328
+ }
329
+ regroupExtIdsFromTheList(ids) {
330
+ // TODO: implement this function
331
+ // this should handle a case when you have:
332
+ // compA that has extA and that extA has extB
333
+ // in that case we now get the following group:
334
+ // ids: [extA, extB]
335
+ // while we need extB to be in a different group before extA
336
+ return [ids];
337
+ }
338
+ async getAndLoadSlot(workspaceIds, scopeIds, loadOpts, throwOnFailure = true) {
339
+ const {
340
+ workspaceComponents,
341
+ scopeComponents,
342
+ invalidComponents
343
+ } = await this.getComponentsWithoutLoadExtensions(workspaceIds, scopeIds, loadOpts, throwOnFailure);
344
+ const components = workspaceComponents.concat(scopeComponents);
345
+ const allExtensions = components.map(component => {
346
+ return component.state._consumer.extensions;
347
+ });
348
+
349
+ // Ensure we won't load the same extension many times
350
+ // We don't want to ignore version here, as we do want to load different extensions with same id but different versions here
351
+ const mergedExtensions = _config().ExtensionDataList.mergeConfigs(allExtensions, false);
352
+ await this.workspace.loadComponentsExtensions(mergedExtensions);
353
+ const withAspects = await (0, _pMap().default)(components, component => this.executeLoadSlot(component), {
354
+ concurrency: (0, _concurrency().concurrentComponentsLimit)()
355
+ });
356
+
357
+ // const withAspects = await Promise.all(
358
+ // components.map((component) => {
359
+ // return this.executeLoadSlot(component);
360
+ // })
361
+ // );
362
+ await this.warnAboutMisconfiguredEnvs(withAspects);
363
+ // It's important to load the workspace components as aspects here
364
+ // otherwise the envs from the workspace won't be loaded at time
365
+ // so we will get wrong dependencies from component who uses envs from the workspace
366
+ if (loadOpts.loadSeedersAsAspects) {
367
+ await this.loadCompsAsAspects(workspaceComponents.concat(scopeComponents), {
368
+ loadApps: true,
369
+ loadEnvs: true,
370
+ loadAspects: loadOpts.aspects,
371
+ core: loadOpts.core,
372
+ seeders: loadOpts.seeders,
373
+ idsToNotLoadAsAspects: loadOpts.idsToNotLoadAsAspects
374
+ });
375
+ }
376
+ return {
377
+ components: withAspects,
378
+ invalidComponents
379
+ };
380
+ }
381
+
382
+ // TODO: this is similar to scope.main.runtime loadCompAspects func, we should merge them.
383
+ async loadCompsAsAspects(components, opts = {
384
+ loadApps: true,
385
+ loadEnvs: true,
386
+ loadAspects: true
387
+ }) {
388
+ const aspectIds = [];
389
+ components.forEach(component => {
390
+ var _opts$idsToNotLoadAsA, _appData$data, _envsData$data, _envsData$data2, _envsData$data3, _envsData$data4;
391
+ const firstTimeToLoad = this.componentLoadedSelfAsAspects.get(component.id.toString()) === undefined;
392
+ const excluded = (_opts$idsToNotLoadAsA = opts.idsToNotLoadAsAspects) === null || _opts$idsToNotLoadAsA === void 0 ? void 0 : _opts$idsToNotLoadAsA.includes(component.id.toString());
393
+ const isCore = this.aspectLoader.isCoreAspect(component.id.toStringWithoutVersion());
394
+ const alreadyLoaded = this.aspectLoader.isAspectLoaded(component.id.toString());
395
+ const skipLoading = excluded || isCore || alreadyLoaded || !firstTimeToLoad;
396
+ if (skipLoading) {
397
+ return;
398
+ }
399
+ const idStr = component.id.toString();
400
+ const appData = component.state.aspects.get('teambit.harmony/application');
401
+ if (opts.loadApps && appData !== null && appData !== void 0 && (_appData$data = appData.data) !== null && _appData$data !== void 0 && _appData$data.appName) {
402
+ aspectIds.push(idStr);
403
+ this.componentLoadedSelfAsAspects.set(idStr, true);
404
+ }
405
+ const envsData = component.state.aspects.get(_envs().EnvsAspect.id);
406
+ if (opts.loadEnvs && (envsData !== null && envsData !== void 0 && (_envsData$data = envsData.data) !== null && _envsData$data !== void 0 && _envsData$data.services || envsData !== null && envsData !== void 0 && (_envsData$data2 = envsData.data) !== null && _envsData$data2 !== void 0 && _envsData$data2.self || (envsData === null || envsData === void 0 || (_envsData$data3 = envsData.data) === null || _envsData$data3 === void 0 ? void 0 : _envsData$data3.type) === 'env')) {
407
+ aspectIds.push(idStr);
408
+ this.componentLoadedSelfAsAspects.set(idStr, true);
409
+ }
410
+ if (opts.loadAspects && (envsData === null || envsData === void 0 || (_envsData$data4 = envsData.data) === null || _envsData$data4 === void 0 ? void 0 : _envsData$data4.type) === 'aspect') {
411
+ aspectIds.push(idStr);
412
+ this.componentLoadedSelfAsAspects.set(idStr, true);
413
+ }
414
+ });
415
+ if (!aspectIds.length) return;
416
+ try {
417
+ await this.workspace.loadAspects(aspectIds, true, 'self loading aspects', {});
418
+ } catch (err) {
419
+ this.logger.warn(`failed loading components as aspects for components ${aspectIds.join(', ')}`, err);
420
+ // we ignore that errors at the moment
421
+ }
422
+ }
423
+ async populateScopeAndExtensionsCache(ids, workspaceScopeIdsMap) {
424
+ return (0, _pMapSeries().default)(ids, async id => {
425
+ const idStr = id.toString();
426
+ let componentFromScope;
427
+ if (!this.scopeComponentsCache.has(idStr)) {
428
+ try {
429
+ // Do not import automatically if it's missing, it will throw an error later
430
+ componentFromScope = await this.workspace.scope.get(id, undefined, false);
431
+ if (componentFromScope) {
432
+ this.scopeComponentsCache.set(idStr, componentFromScope);
433
+ }
434
+ // This is fine here, as it will be handled later in the process
435
+ } catch (err) {
436
+ const wsAspectLoader = this.workspace.getWorkspaceAspectsLoader();
437
+ wsAspectLoader.throwWsJsoncAspectNotFoundError(err);
438
+ this.logger.warn(`populateScopeAndExtensionsCache - failed loading component ${idStr} from scope`, err);
439
+ }
440
+ }
441
+ if (!this.componentsExtensionsCache.has(idStr) && workspaceScopeIdsMap.workspaceIds.has(idStr)) {
442
+ componentFromScope = componentFromScope || this.scopeComponentsCache.get(idStr);
443
+ const {
444
+ extensions,
445
+ errors
446
+ } = await this.workspace.componentExtensions(id, componentFromScope, undefined, {
447
+ loadExtensions: false
448
+ });
449
+ this.componentsExtensionsCache.set(idStr, {
450
+ extensions,
451
+ errors
452
+ });
453
+ }
454
+ });
455
+ }
456
+ async warnAboutMisconfiguredEnvs(components) {
457
+ const allIds = (0, _lodash().uniq)(components.map(component => this.envs.getEnvId(component)));
458
+ return Promise.all(allIds.map(envId => this.workspace.warnAboutMisconfiguredEnv(envId)));
459
+ }
460
+ async groupAndUpdateIds(ids, existingGroups) {
461
+ const result = existingGroups || {
462
+ scopeIds: new Map(),
463
+ workspaceIds: new Map()
464
+ };
465
+ await Promise.all(ids.map(async componentId => {
466
+ const inWs = await this.isInWsIncludeDeleted(componentId);
467
+ if (!inWs) {
468
+ result.scopeIds.set(componentId.toString(), componentId);
469
+ return undefined;
470
+ }
471
+ const resolvedVersions = this.resolveVersion(componentId);
472
+ result.workspaceIds.set(resolvedVersions.toString(), resolvedVersions);
473
+ return undefined;
474
+ }));
475
+ return result;
476
+ }
477
+ async isInWsIncludeDeleted(componentId) {
478
+ const nonDeletedWsIds = await this.workspace.listIds();
479
+ const deletedWsIds = await this.workspace.locallyDeletedIds();
480
+ const allWsIds = nonDeletedWsIds.concat(deletedWsIds);
481
+ const inWs = allWsIds.find(id => id.isEqual(componentId, {
482
+ ignoreVersion: !componentId.hasVersion()
483
+ }));
484
+ return !!inWs;
485
+ }
486
+ async getComponentsWithoutLoadExtensions(workspaceIds, scopeIds, loadOpts, throwOnFailure = true) {
145
487
  const invalidComponents = [];
146
- const longProcessLogger = this.logger.createLongProcessLogger('loading components', ids.length);
147
- const componentsP = (0, _pMapSeries().default)(idsWithoutEmpty, async id => {
148
- longProcessLogger.logProgress(id.toString());
149
- return this.get(id, undefined, undefined, undefined, loadOpts).catch(err => {
488
+ const errors = [];
489
+ const loadOptsWithDefaults = Object.assign(
490
+ // We don't want to load extension or execute the load slot at this step
491
+ // we will do it later
492
+ // this important for better performance
493
+ // We don't want to store deps in fs cache, as at this point extensions are not loaded yet
494
+ // so it might save a wrong deps into the cache
495
+ {
496
+ loadExtensions: false,
497
+ executeLoadSlot: false
498
+ }, loadOpts || {});
499
+ const idsIndex = {};
500
+ workspaceIds.forEach(id => {
501
+ idsIndex[id.toString()] = id;
502
+ });
503
+ const {
504
+ components: legacyComponents,
505
+ invalidComponents: legacyInvalidComponents,
506
+ removedComponents
507
+ } = await this.workspace.consumer.loadComponents(_componentId().ComponentIdList.fromArray(workspaceIds), false, loadOptsWithDefaults);
508
+ const allLegacyComponents = legacyComponents.concat(removedComponents);
509
+ legacyInvalidComponents.forEach(invalidComponent => {
510
+ const entry = {
511
+ id: idsIndex[invalidComponent.id.toString()],
512
+ err: invalidComponent.error
513
+ };
514
+ if (_component2().default.isComponentInvalidByErrorType(invalidComponent.error)) {
515
+ if (throwOnFailure) throw invalidComponent.error;
516
+ invalidComponents.push(entry);
517
+ }
518
+ if (this.isComponentNotExistsError(invalidComponent.error) || invalidComponent.error instanceof _componentNotFoundInPath().default) {
519
+ errors.push(entry);
520
+ }
521
+ });
522
+ const getWithCatch = (id, legacyComponent) => {
523
+ return this.get(id, legacyComponent, undefined, undefined, loadOptsWithDefaults).catch(err => {
150
524
  if (_component2().default.isComponentInvalidByErrorType(err) && !throwOnFailure) {
151
525
  invalidComponents.push({
152
526
  id,
@@ -163,19 +537,49 @@ class WorkspaceComponentLoader {
163
537
  }
164
538
  throw err;
165
539
  });
540
+ };
541
+
542
+ // await this.getConsumerComponent(id, loadOpts)
543
+ const componentsP = (0, _pMap().default)(allLegacyComponents, legacyComponent => {
544
+ // const componentsP = Promise.all(
545
+ // allLegacyComponents.map(async (legacyComponent) => {
546
+ let id = idsIndex[legacyComponent.id.toString()];
547
+ if (!id) {
548
+ const withoutVersion = idsIndex[legacyComponent.id.toStringWithoutVersion()] || legacyComponent.id;
549
+ if (withoutVersion) {
550
+ id = withoutVersion.changeVersion(legacyComponent.id.version);
551
+ idsIndex[legacyComponent.id.toString()] = id;
552
+ }
553
+ }
554
+ return getWithCatch(id, legacyComponent);
555
+ }, {
556
+ concurrency: (0, _concurrency().concurrentComponentsLimit)()
166
557
  });
167
- const components = await componentsP;
168
558
  errors.forEach(err => {
169
559
  this.logger.console(`failed loading component ${err.id.toString()}, see full error in debug.log file`);
170
560
  this.logger.warn(`failed loading component ${err.id.toString()}`, err.err);
171
561
  });
172
- // remove errored components
173
- const filteredComponents = (0, _lodash().compact)(components);
174
- longProcessLogger.end();
175
- return {
176
- components: filteredComponents,
177
- invalidComponents
178
- };
562
+ const components = (0, _lodash().compact)(await componentsP);
563
+
564
+ // Here we need to load many, otherwise we will get wrong overrides dependencies data
565
+ // as when loading the next batch of components (next group) we won't have the envs loaded
566
+
567
+ try {
568
+ // We don't want to load envs as part of this step, they will be loaded later
569
+ const scopeComponents = await this.workspace.scope.loadMany(scopeIds, undefined, {
570
+ loadApps: false,
571
+ loadEnvs: false
572
+ });
573
+ return {
574
+ workspaceComponents: components,
575
+ scopeComponents,
576
+ invalidComponents
577
+ };
578
+ } catch (err) {
579
+ const wsAspectLoader = this.workspace.getWorkspaceAspectsLoader();
580
+ wsAspectLoader.throwWsJsoncAspectNotFoundError(err);
581
+ throw err;
582
+ }
179
583
  }
180
584
  async getInvalid(ids) {
181
585
  const idsWithoutEmpty = (0, _lodash().compact)(ids);
@@ -198,20 +602,30 @@ class WorkspaceComponentLoader {
198
602
  });
199
603
  return errors;
200
604
  }
201
- async get(componentId, legacyComponent, useCache = true, storeInCache = true, loadOpts) {
202
- const bitIdWithVersion = (0, _utils().getLatestVersionNumber)(this.workspace.consumer.bitmapIdsFromCurrentLaneIncludeRemoved, componentId);
203
- const id = bitIdWithVersion.version ? componentId.changeVersion(bitIdWithVersion.version) : componentId;
204
- const fromCache = this.getFromCache(id, loadOpts);
605
+ async get(componentId, legacyComponent, useCache = true, storeInCache = true, loadOpts, getOpts = {
606
+ resolveIdVersion: true
607
+ }) {
608
+ const loadOptsWithDefaults = Object.assign({
609
+ loadExtensions: true,
610
+ executeLoadSlot: true
611
+ }, loadOpts || {});
612
+ const id = getOpts !== null && getOpts !== void 0 && getOpts.resolveIdVersion ? this.resolveVersion(componentId) : componentId;
613
+ const fromCache = this.getFromCache(componentId, loadOptsWithDefaults);
205
614
  if (fromCache && useCache) {
206
615
  return fromCache;
207
616
  }
208
- const consumerComponent = legacyComponent || (await this.getConsumerComponent(id, loadOpts));
617
+ let consumerComponent = legacyComponent;
618
+ const inWs = await this.isInWsIncludeDeleted(componentId);
619
+ if (inWs && !consumerComponent) {
620
+ consumerComponent = await this.getConsumerComponent(id, loadOptsWithDefaults);
621
+ }
622
+
209
623
  // in case of out-of-sync, the id may changed during the load process
210
624
  const updatedId = consumerComponent ? consumerComponent.id : id;
211
- const component = await this.loadOne(updatedId, consumerComponent, loadOpts);
625
+ const component = await this.loadOne(updatedId, consumerComponent, loadOptsWithDefaults);
212
626
  if (storeInCache) {
213
627
  this.addMultipleEnvsIssueIfNeeded(component); // it's in storeInCache block, otherwise, it wasn't fully loaded
214
- this.saveInCache(component, loadOpts);
628
+ this.saveInCache(component, loadOptsWithDefaults);
215
629
  }
216
630
  return component;
217
631
  }
@@ -225,6 +639,11 @@ class WorkspaceComponentLoader {
225
639
  throw err;
226
640
  }
227
641
  }
642
+ resolveVersion(componentId) {
643
+ const bitIdWithVersion = (0, _utils().getLatestVersionNumber)(this.workspace.consumer.bitmapIdsFromCurrentLaneIncludeRemoved, componentId);
644
+ const id = bitIdWithVersion.version ? componentId.changeVersion(bitIdWithVersion.version) : componentId;
645
+ return id;
646
+ }
228
647
  addMultipleEnvsIssueIfNeeded(component) {
229
648
  const envs = this.envs.getAllEnvsConfiguredOnComponent(component);
230
649
  const envIds = (0, _lodash().uniq)(envs.map(env => env.id));
@@ -235,6 +654,9 @@ class WorkspaceComponentLoader {
235
654
  }
236
655
  clearCache() {
237
656
  this.componentsCache.deleteAll();
657
+ this.scopeComponentsCache.deleteAll();
658
+ this.componentsExtensionsCache.deleteAll();
659
+ this.componentLoadedSelfAsAspects.deleteAll();
238
660
  }
239
661
  clearComponentCache(id) {
240
662
  const idStr = id.toString();
@@ -245,15 +667,19 @@ class WorkspaceComponentLoader {
245
667
  }
246
668
  }
247
669
  async loadOne(id, consumerComponent, loadOpts) {
248
- const componentFromScope = await this.workspace.scope.get(id);
670
+ const idStr = id.toString();
671
+ const componentFromScope = this.scopeComponentsCache.has(idStr) ? this.scopeComponentsCache.get(idStr) : await this.workspace.scope.get(id);
249
672
  if (!consumerComponent) {
250
673
  if (!componentFromScope) throw new (_exceptions().MissingBitMapComponent)(id.toString());
251
674
  return componentFromScope;
252
675
  }
676
+ const extErrorsFromCache = this.componentsExtensionsCache.has(idStr) ? this.componentsExtensionsCache.get(idStr) : undefined;
253
677
  const {
254
678
  extensions,
255
679
  errors
256
- } = await this.workspace.componentExtensions(id, componentFromScope);
680
+ } = extErrorsFromCache || (await this.workspace.componentExtensions(id, componentFromScope, undefined, {
681
+ loadExtensions: loadOpts === null || loadOpts === void 0 ? void 0 : loadOpts.loadExtensions
682
+ }));
257
683
  if (errors !== null && errors !== void 0 && errors.some(err => err instanceof _mergeConfigConflict().MergeConfigConflict)) {
258
684
  consumerComponent.issues.getOrCreate(_componentIssues().IssuesClasses.MergeConfigHasConflict).data = true;
259
685
  }
@@ -267,10 +693,17 @@ class WorkspaceComponentLoader {
267
693
  // componentFromScope.state = state;
268
694
  // const workspaceComponent = WorkspaceComponent.fromComponent(componentFromScope, this.workspace);
269
695
  const workspaceComponent = new (_workspaceComponent().WorkspaceComponent)(componentFromScope.id, componentFromScope.head, state, componentFromScope.tags, this.workspace);
270
- const updatedComp = await this.executeLoadSlot(workspaceComponent, loadOpts);
271
- return updatedComp;
696
+ if (loadOpts !== null && loadOpts !== void 0 && loadOpts.executeLoadSlot) {
697
+ return this.executeLoadSlot(workspaceComponent, loadOpts);
698
+ }
699
+ // const updatedComp = await this.executeLoadSlot(workspaceComponent, loadOpts);
700
+ return workspaceComponent;
272
701
  }
273
- return this.executeLoadSlot(this.newComponentFromState(id, state), loadOpts);
702
+ const newComponent = this.newComponentFromState(id, state);
703
+ if (!(loadOpts !== null && loadOpts !== void 0 && loadOpts.executeLoadSlot)) {
704
+ return newComponent;
705
+ }
706
+ return this.executeLoadSlot(newComponent, loadOpts);
274
707
  }
275
708
  saveInCache(component, loadOpts) {
276
709
  const cacheKey = createComponentCacheKey(component.id, loadOpts);
@@ -284,9 +717,19 @@ class WorkspaceComponentLoader {
284
717
  * as a result, when out-of-sync is happening and the id is changed to include scope-name in the
285
718
  * legacy-id, the component is the cache has the old id.
286
719
  */
287
- getFromCache(id, loadOpts) {
720
+ getFromCache(componentId, loadOpts) {
721
+ const bitIdWithVersion = this.resolveVersion(componentId);
722
+ const id = bitIdWithVersion.version ? componentId.changeVersion(bitIdWithVersion.version) : componentId;
288
723
  const cacheKey = createComponentCacheKey(id, loadOpts);
289
- const fromCache = this.componentsCache.get(cacheKey);
724
+ // If we try to look for the cache without load extensions/ without execute load slot
725
+ // but there is an entry after the load, we want to use it as well.
726
+ // as we want the component, so if we already loaded it with everything, it's fine.
727
+ // this sometime relevant for cases with tiny cache size (during tag)
728
+ const cacheKeyWithTrueLoadOpts = createComponentCacheKey(id, {
729
+ loadExtensions: true,
730
+ executeLoadSlot: true
731
+ });
732
+ const fromCache = this.componentsCache.get(cacheKey) || this.componentsCache.get(cacheKeyWithTrueLoadOpts);
290
733
  if (fromCache && fromCache.id.isEqual(id)) {
291
734
  return fromCache;
292
735
  }
@@ -372,7 +815,8 @@ class WorkspaceComponentLoader {
372
815
  }
373
816
  exports.WorkspaceComponentLoader = WorkspaceComponentLoader;
374
817
  function createComponentCacheKey(id, loadOpts) {
375
- return `${id.toString()}:${JSON.stringify(sortKeys(loadOpts ?? {}))}`;
818
+ const relevantOpts = (0, _lodash().pick)(loadOpts, ['loadExtensions', 'executeLoadSlot']);
819
+ return `${id.toString()}:${JSON.stringify(sortKeys(relevantOpts ?? {}))}`;
376
820
  }
377
821
  function sortKeys(obj) {
378
822
  return (0, _lodash().fromPairs)(Object.entries(obj).sort(([k1], [k2]) => k1.localeCompare(k2)));