Skip to content

Commit

Permalink
fix(serve): connect to Cloud if process is started outside of project…
Browse files Browse the repository at this point in the history
… dir (#4822)

* 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.

* chore: fix build

* fix: set localServerPort for only non dev/serve commands

* chore: updating typings to enforce explicit value for localServerPort

---------

Co-authored-by: Shumail Mohyuddin <shumailmohyuddin@gmail.com>
  • Loading branch information
edvald and shumailxyz authored Oct 6, 2023
1 parent e2df10e commit 61b424e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ export class CloudApi {
sessionId: string
projectId: string
commandInfo: CommandInfo
localServerPort?: number
localServerPort: number | undefined
environment: string
namespace: string
isDevCommand: boolean
Expand Down
3 changes: 2 additions & 1 deletion core/src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,8 @@ export abstract class Command<
sessionId: garden.sessionId,
projectId: garden.projectId,
commandInfo: garden.commandInfo,
localServerPort: server?.port,
// localServerPort only needs to be set for dev/serve commands
localServerPort: undefined,
environment: garden.environmentName,
namespace: garden.namespace,
isDevCommand: garden.commandInfo.name === "dev",
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,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,
Expand Down
28 changes: 26 additions & 2 deletions core/src/server/instance-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface GardenInstanceManagerParams {
log: Log
sessionId: string
plugins: GardenPluginReference[]
serveCommand?: ServeCommand
serveCommand: ServeCommand
extraCommands?: Command[]
defaultOpts?: Partial<GardenOpts>
cloudApiFactory?: CloudApiFactory
Expand Down Expand Up @@ -104,6 +104,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)
Expand Down Expand Up @@ -381,7 +382,7 @@ export class GardenInstanceManager {

const gardenParams = await resolveGardenParamsPartial(projectRoot, gardenOpts)

return this.ensureInstance(
const garden = await this.ensureInstance(
log,
{
projectRoot,
Expand All @@ -391,5 +392,28 @@ 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,
// set localServerPort only for dev/serve commands
localServerPort:
this.serveCommand.server.port && ["dev", "serve"].includes(garden.commandInfo.name)
? this.serveCommand.server.port
: undefined,
environment: garden.environmentName,
namespace: garden.namespace,
isDevCommand: true,
})
}

return garden
}
}

0 comments on commit 61b424e

Please sign in to comment.