diff --git a/scopes/compilation/compiler/workspace-compiler.ts b/scopes/compilation/compiler/workspace-compiler.ts index f2d8ed9ec939..917ffc524136 100644 --- a/scopes/compilation/compiler/workspace-compiler.ts +++ b/scopes/compilation/compiler/workspace-compiler.ts @@ -408,7 +408,7 @@ export class WorkspaceCompiler { componentLoadOptions.loadSeedersAsAspects = false; } let components = await this.workspace.getMany(componentIds, componentLoadOptions); - await this.loadExternalEnvs(components); + await this.workspace.loadExternalEnvs(components); // reload components as we might cleared the cache as part of the loadExternalEnvs components = await this.workspace.getMany(componentIds, componentLoadOptions); const grouped = await this.buildGroupsToCompile(components, graph); @@ -423,47 +423,6 @@ export class WorkspaceCompiler { return results.flat(); } - /** - * This will ensue that the envs of the components are loaded before the compilation starts. - * @param components - */ - private async loadExternalEnvs(components: Component[]) { - const componentsIdsStr = components.map((c) => c.id.toString()); - const envIdsCompIdsMap = {}; - const compsWithWrongEnvId: string[] = []; - await Promise.all( - components.map(async (component) => { - // It's important to use calculate here to get the real id even if it's not loaded - const envId = (await this.envs.calculateEnvId(component)).toString(); - // This might be different from the env id above, because the component might be loaded before the env - // in that case we will need to clear the cache of that component - const envIdByGet = this.envs.getEnvId(component); - if (envId !== envIdByGet) { - compsWithWrongEnvId.push(component.id.toString()); - } - // If it's part of the components it will be handled later as it's not external - // and might need to be compiled as well - if (componentsIdsStr.includes(envId)) return undefined; - if (!envIdsCompIdsMap[envId]) envIdsCompIdsMap[envId] = [component.id.toString()]; - envIdsCompIdsMap[envId].push(component.id.toString()); - }) - ); - const externalEnvsIds = Object.keys(envIdsCompIdsMap); - - if (!externalEnvsIds.length) return; - const nonLoadedEnvs = externalEnvsIds.filter((envId) => !this.envs.isEnvRegistered(envId)); - await this.workspace.loadAspects(nonLoadedEnvs); - const idsToClearCache: string[] = uniq( - nonLoadedEnvs - .reduce((acc, envId) => { - const compIds = envIdsCompIdsMap[envId]; - return [...acc, ...compIds]; - }, [] as string[]) - .concat(compsWithWrongEnvId) - ); - this.workspace.clearComponentsCache(idsToClearCache.map((id) => ComponentID.fromString(id))); - } - private async runCompileComponents( components: Component[], options: CompileOptions, diff --git a/scopes/workspace/install/install.main.runtime.ts b/scopes/workspace/install/install.main.runtime.ts index 054188232667..dce2e9d7a38c 100644 --- a/scopes/workspace/install/install.main.runtime.ts +++ b/scopes/workspace/install/install.main.runtime.ts @@ -678,6 +678,7 @@ export class InstallMain { linkedRootDeps: Record; } ): Promise<{ componentsAndManifests: ComponentsAndManifests; mergedRootPolicy: WorkspacePolicy }> { + await this.workspace.loadExternalEnvs(await this.workspace.getMany(this.workspace.listIds())); const mergedRootPolicy = await this.addConfiguredAspectsToWorkspacePolicy(); await this.addConfiguredGeneratorEnvsToWorkspacePolicy(mergedRootPolicy); const componentsAndManifests = await this._getComponentsManifests(installer, mergedRootPolicy, options); diff --git a/scopes/workspace/workspace/workspace.ts b/scopes/workspace/workspace/workspace.ts index 9555a226f0b5..7f95d88a2da9 100644 --- a/scopes/workspace/workspace/workspace.ts +++ b/scopes/workspace/workspace/workspace.ts @@ -2505,6 +2505,47 @@ the following envs are used in this workspace: ${uniq(availableEnvs).join(', ')} }); return componentPolicies; } + + /** + * This will ensue that the envs of the components are loaded before the compilation starts. + * @param components + */ + public async loadExternalEnvs(components: Component[]) { + const componentsIdsStr = components.map((c) => c.id.toString()); + const envIdsCompIdsMap = {}; + const compsWithWrongEnvId: string[] = []; + await Promise.all( + components.map(async (component) => { + // It's important to use calculate here to get the real id even if it's not loaded + const envId = (await this.envs.calculateEnvId(component)).toString(); + // This might be different from the env id above, because the component might be loaded before the env + // in that case we will need to clear the cache of that component + const envIdByGet = this.envs.getEnvId(component); + if (envId !== envIdByGet) { + compsWithWrongEnvId.push(component.id.toString()); + } + // If it's part of the components it will be handled later as it's not external + // and might need to be compiled as well + if (componentsIdsStr.includes(envId)) return undefined; + if (!envIdsCompIdsMap[envId]) envIdsCompIdsMap[envId] = [component.id.toString()]; + envIdsCompIdsMap[envId].push(component.id.toString()); + }) + ); + const externalEnvsIds = Object.keys(envIdsCompIdsMap); + + if (!externalEnvsIds.length) return; + const nonLoadedEnvs = externalEnvsIds.filter((envId) => !this.envs.isEnvRegistered(envId)); + await this.loadAspects(nonLoadedEnvs); + const idsToClearCache: string[] = uniq( + nonLoadedEnvs + .reduce((acc, envId) => { + const compIds = envIdsCompIdsMap[envId]; + return [...acc, ...compIds]; + }, [] as string[]) + .concat(compsWithWrongEnvId) + ); + this.clearComponentsCache(idsToClearCache.map((id) => ComponentID.fromString(id))); + } } /**