Skip to content

Commit

Permalink
fix(checkout), fetch current lane object first to find new comps (#7361)
Browse files Browse the repository at this point in the history
Currently, this is working only when `bit import` was running first,
which updates the local lane object with the remote.
Otherwise, it wouldn't bring the new components because the local lane
doesn't have them.
  • Loading branch information
davidfirst authored May 5, 2023
1 parent a69ea53 commit 6027473
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
1 change: 0 additions & 1 deletion e2e/harmony/lanes/lanes.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,6 @@ describe('bit lane command', function () {
helper.command.export();
firstWorkspaceAfterExport = helper.scopeHelper.cloneLocalScope();
helper.scopeHelper.getClonedLocalScope(secondWorkspace);
helper.command.import();
});
it('bit checkout with --workspace-only flag should not add the component and should suggest omitting --workspace-only flag', () => {
const output = helper.command.checkoutHead('--skip-dependency-installation --workspace-only');
Expand Down
22 changes: 15 additions & 7 deletions scopes/component/checkout/checkout.main.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BitError } from '@teambit/bit-error';
import { compact } from 'lodash';
import { BEFORE_CHECKOUT } from '@teambit/legacy/dist/cli/loader/loader-messages';
import { ApplyVersionResults } from '@teambit/merging';
import ImporterAspect, { ImporterMain } from '@teambit/importer';
import { HEAD, LATEST } from '@teambit/legacy/dist/constants';
import { ComponentWriterAspect, ComponentWriterMain } from '@teambit/component-writer';
import {
Expand Down Expand Up @@ -60,13 +61,18 @@ export type ComponentStatusBeforeMergeAttempt = {
type CheckoutTo = 'head' | 'reset' | string;

export class CheckoutMain {
constructor(private workspace: Workspace, private logger: Logger, private componentWriter: ComponentWriterMain) {}
constructor(
private workspace: Workspace,
private logger: Logger,
private componentWriter: ComponentWriterMain,
private importer: ImporterMain
) {}

async checkout(checkoutProps: CheckoutProps): Promise<ApplyVersionResults> {
const consumer = this.workspace.consumer;
const { version, ids, promptMergeOptions } = checkoutProps;
await this.syncNewComponents(checkoutProps);
await this.workspace.scope.import(ids || [], { useCache: false, preferDependencyGraph: true });
await this.importer.importCurrentObjects(); // important. among others, it fetches the remote lane object and its new components.
const bitIds = BitIds.fromArray(ids?.map((id) => id._legacy) || []);
const { components } = await consumer.loadComponents(bitIds);

Expand Down Expand Up @@ -202,7 +208,7 @@ export class CheckoutMain {
});
} catch (err) {
// don't stop the process. it's possible that the scope doesn't exist yet because these are new components
this.logger.error(`unable to sync new components due to an error`, err);
this.logger.error(`unable to sync new components, if these components are really new, ignore the error`, err);
}
}

Expand Down Expand Up @@ -253,6 +259,7 @@ export class CheckoutMain {
}

private async getNewComponentsFromLane(ids: ComponentID[]): Promise<ComponentID[]> {
// current lane object is up to date due to the previous `importCurrentObjects()` call
const lane = await this.workspace.consumer.getCurrentLaneObject();
if (!lane) {
return [];
Expand Down Expand Up @@ -384,18 +391,19 @@ export class CheckoutMain {
}

static slots = [];
static dependencies = [CLIAspect, WorkspaceAspect, LoggerAspect, ComponentWriterAspect];
static dependencies = [CLIAspect, WorkspaceAspect, LoggerAspect, ComponentWriterAspect, ImporterAspect];

static runtime = MainRuntime;

static async provider([cli, workspace, loggerMain, compWriter]: [
static async provider([cli, workspace, loggerMain, compWriter, importer]: [
CLIMain,
Workspace,
LoggerMain,
ComponentWriterMain
ComponentWriterMain,
ImporterMain
]) {
const logger = loggerMain.createLogger(CheckoutAspect.id);
const checkoutMain = new CheckoutMain(workspace, logger, compWriter);
const checkoutMain = new CheckoutMain(workspace, logger, compWriter, importer);
cli.register(new CheckoutCmd(checkoutMain));
return checkoutMain;
}
Expand Down
2 changes: 1 addition & 1 deletion scopes/workspace/workspace/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ export class Workspace implements ComponentFactory {

/**
* if checked out to a lane and the lane exists in the remote,
* return the remote lane id (name+scope). otherwise, return null.
* return the remote lane. otherwise, return null.
*/
async getCurrentRemoteLane(): Promise<Lane | null> {
const currentLaneId = this.getCurrentLaneId();
Expand Down
5 changes: 1 addition & 4 deletions src/consumer/consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,16 +304,13 @@ export default class Consumer {
return Promise.all(componentsP);
}

/**
* For legacy, it loads all the dependencies. For Harmony, it's not needed.
*/
async loadComponentFromModelImportIfNeeded(id: BitId, throwIfNotExist = true): Promise<Component> {
const scopeComponentsImporter = this.scope.scopeImporter;
const getModelComponent = async (): Promise<ModelComponent> => {
if (throwIfNotExist) return this.scope.getModelComponent(id);
const modelComponent = await this.scope.getModelComponentIfExist(id);
if (modelComponent) return modelComponent;
await scopeComponentsImporter.importMany({ ids: new BitIds(id) });
await scopeComponentsImporter.importMany({ ids: new BitIds(id), preferDependencyGraph: true });
return this.scope.getModelComponent(id);
};
const modelComponent = await getModelComponent();
Expand Down

0 comments on commit 6027473

Please sign in to comment.