diff --git a/scopes/component/component-writer/component-writer.main.runtime.ts b/scopes/component/component-writer/component-writer.main.runtime.ts index 9f1d8fd20b80..eb2458d57eb4 100644 --- a/scopes/component/component-writer/component-writer.main.runtime.ts +++ b/scopes/component/component-writer/component-writer.main.runtime.ts @@ -104,13 +104,26 @@ export class ComponentWriterMain { skipWriteConfigFiles = false ): Promise { this.logger.debug('installPackagesGracefully, start installing packages'); + // Clear all components cache to ensure fresh dependency data is loaded. + // Without this, the install process might use stale cached components that don't have proper + // dependency information, resulting in missing package dependencies. + // Load the components to trigger full dependency detection before install. + // This ensures all dependencies from the source code are detected and added to the MissingPackagesDependenciesOnFs + // issue, which is used to merge dependencies from the model during manifest generation. + // We clear cache first to ensure components are loaded fresh from the filesystem. + this.workspace.clearAllComponentsCache(); + this.logger.debug('installPackagesGracefully, loading components to detect dependencies'); + await this.workspace.getMany(componentIds); + this.logger.debug('installPackagesGracefully, components loaded'); try { const installOpts = { dedupe: true, updateExisting: false, import: false, writeConfigFiles: !skipWriteConfigFiles, - dependenciesGraph: await this.workspace.scope.getDependenciesGraphByComponentIds(componentIds), + // Don't pass dependenciesGraph during import-install to ensure all dependencies + // are resolved fresh from the component manifests. The pre-computed graph from the model + // may be incomplete for newly imported components whose dependencies aren't fully resolved yet. }; await this.installer.install(undefined, installOpts); this.logger.debug('installPackagesGracefully, completed installing packages successfully'); diff --git a/scopes/dependencies/dependency-resolver/dependencies/dependency-list-factory.ts b/scopes/dependencies/dependency-resolver/dependencies/dependency-list-factory.ts index 4c7e3986a2dd..453aed1061a3 100644 --- a/scopes/dependencies/dependency-resolver/dependencies/dependency-list-factory.ts +++ b/scopes/dependencies/dependency-resolver/dependencies/dependency-list-factory.ts @@ -59,12 +59,22 @@ export class DependencyListFactory { } // All deps defined in model const depListFromModel = await this.getDependenciesFromLegacyModelComponent(componentFromModel); - // Only deps from model which are also required in the current component on fs (currently missing) + + // For component dependencies (bit components), always include them from the model if missingPackages has at least one entry. + // This is because component dependency detection may be incomplete during initial load (e.g., for Vue SFC files), + // but we still need to install these dependencies. Package dependencies are filtered by missingPackages to respect + // user changes in the source code. + const hasAnyMissingPackages = missingPackages.length > 0; const filteredDepList = depListFromModel.filter((dep) => { const packageName = dep.getPackageName?.(); if (!packageName) { return false; } + // For component dependencies, include them if we have any missing packages detected (indicating the component is being analyzed) + if (dep.constructor.name === 'ComponentDependency' && hasAnyMissingPackages) { + return true; + } + // For package dependencies, only include if they're in the missing packages list return missingPackages.includes(packageName); }); return filteredDepList;