From 2fc311f250916bcd2e8147dd8a997da67b57762f Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Thu, 13 Jul 2023 03:38:54 +0200 Subject: [PATCH] fix(serve): connect to Cloud if process is started outside of project dir This came up in the Desktop client because it'd start the serve command outside of any project root, and thus not connect to Cloud at all for the live view etc. to work. --- core/src/commands/serve.ts | 2 +- core/src/server/instance-manager.ts | 24 ++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/core/src/commands/serve.ts b/core/src/commands/serve.ts index 2ac94a1b79..80516ebd5e 100644 --- a/core/src/commands/serve.ts +++ b/core/src/commands/serve.ts @@ -160,7 +160,7 @@ export class ServeCommand< const session = await cloudApi.registerSession({ parentSessionId: undefined, projectId, - // Use the process (i.e. parent command) session ID for the serve command session + // Use the process (i.e. parent command) session ID for the serve/dev command session sessionId: manager.sessionId, commandInfo: garden.commandInfo, localServerPort: this.server.port, diff --git a/core/src/server/instance-manager.ts b/core/src/server/instance-manager.ts index 0313f52efa..5cab41d518 100644 --- a/core/src/server/instance-manager.ts +++ b/core/src/server/instance-manager.ts @@ -51,7 +51,7 @@ interface GardenInstanceManagerParams { log: Log sessionId: string plugins: GardenPluginReference[] - serveCommand?: ServeCommand + serveCommand: ServeCommand extraCommands?: Command[] defaultOpts?: Partial cloudApiFactory?: CloudApiFactory @@ -103,6 +103,7 @@ export class GardenInstanceManager { this.defaultOpts = defaultOpts || {} this.plugins = plugins this.cloudApiFactory = cloudApiFactory || CloudApi.factory + this.serveCommand = serveCommand this.events = new EventBus() this.monitors = new MonitorManager(log, this.events) @@ -380,7 +381,7 @@ export class GardenInstanceManager { const gardenParams = await resolveGardenParamsPartial(projectRoot, gardenOpts) - return this.ensureInstance( + const garden = await this.ensureInstance( log, { projectRoot, @@ -390,5 +391,24 @@ export class GardenInstanceManager { }, gardenOpts ) + + if (cloudApi && garden.projectId && this.serveCommand.server) { + // Ensure cloud session is registered for the domain and server session, since this may not happen on startup + // if the command isn't started in a Garden project root. This is a no-op if it's already registered. + // FIXME: We still need to rethink on the Cloud side how sessions are scoped + await cloudApi.registerSession({ + parentSessionId: undefined, + projectId: garden.projectId, + // Use the process (i.e. parent command) session ID for the serve/dev command session + sessionId: this.sessionId, + commandInfo: garden.commandInfo, + localServerPort: this.serveCommand.server.port, + environment: garden.environmentName, + namespace: garden.namespace, + isDevCommand: true, + }) + } + + return garden } }