Skip to content

Commit 12c73cf

Browse files
authored
Update app root on client gen (#2327)
This is a temporary fix to better support multiple clones of the same encore app. It will update the app root based on the directory `encore gen client` is called from rather than falling back to the last dir `encore run` was executed in.
1 parent 449fbcb commit 12c73cf

File tree

14 files changed

+326
-163
lines changed

14 files changed

+326
-163
lines changed

cli/cmd/encore/gen.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ To further narrow down the services to generate, use the '--services' flag.
7171
}
7272

7373
// Determine the app id, either from the argument or from the current directory.
74-
var appID string
74+
var appID, appRoot string
7575
if len(args) == 0 {
76+
var err error
7677
// First check the encore.app file.
77-
appRoot, _, err := cmdutil.MaybeAppRoot()
78+
appRoot, _, err = cmdutil.MaybeAppRoot()
7879
if err != nil && !errors.Is(err, cmdutil.ErrNoEncoreApp) {
7980
fatal(err)
8081
} else if appRoot != "" {
@@ -134,6 +135,7 @@ To further narrow down the services to generate, use the '--services' flag.
134135
OpenapiExcludePrivateEndpoints: &openAPIExcludePrivateEndpoints,
135136
TsSharedTypes: &tsSharedTypes,
136137
TsClientTarget: &tsDefaultClient,
138+
AppRoot: appRoot,
137139
})
138140
if err != nil {
139141
fatal(err)

cli/daemon/daemon.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ package daemon
44
import (
55
"bytes"
66
"context"
7+
"errors"
78
"io"
89
"strings"
910
"sync"
1011
"sync/atomic"
1112
"time"
1213

13-
"github.com/cockroachdb/errors"
1414
"github.com/golang/protobuf/ptypes/empty"
1515
"github.com/rs/zerolog"
1616
"github.com/rs/zerolog/log"
@@ -95,13 +95,22 @@ func (s *Server) GenClient(ctx context.Context, params *daemonpb.GenClientReques
9595
}
9696

9797
if envName == "local" {
98-
// Determine the app root
99-
app, err := s.apps.FindLatestByPlatformOrLocalID(params.AppId)
100-
if errors.Is(err, apps.ErrNotFound) {
101-
return nil, status.Errorf(codes.FailedPrecondition, "the app %s must be run locally before generating a client for the 'local' environment.",
102-
params.AppId)
103-
} else if err != nil {
104-
return nil, status.Errorf(codes.Internal, "unable to query app info: %v", err)
98+
var app *apps.Instance
99+
var err error
100+
// If the command was called with an app id, find the app instance by id.
101+
if params.AppRoot == "" {
102+
app, err = s.apps.FindLatestByPlatformOrLocalID(params.AppId)
103+
if errors.Is(err, apps.ErrNotFound) {
104+
return nil, status.Errorf(codes.FailedPrecondition, "the app %s must be run locally before generating a client for the 'local' environment.",
105+
params.AppId)
106+
} else if err != nil {
107+
return nil, status.Errorf(codes.Internal, "unable to query app info: %v", err)
108+
}
109+
} else { // Otherwise, track the app by its root directory.
110+
app, err = s.apps.Track(params.AppRoot)
111+
if err != nil {
112+
return nil, status.Errorf(codes.Internal, "unable to query app info: %v", err)
113+
}
105114
}
106115

107116
// Get the app metadata

proto/encore/daemon/daemon.pb.go

Lines changed: 17 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/encore/daemon/daemon.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ message GenClientRequest {
345345
// an instantiated client with the given target. The target can be e.g.
346346
// a variable, e.g. "import.meta.env.VITE_CLIENT_TARGET" or a string literal.
347347
optional string ts_client_target = 11;
348+
349+
// The root directory of the app to generate a client for.
350+
// Included to be able to handle multi clone scenarios.
351+
string app_root = 12;
348352
}
349353

350354
message GenClientResponse {

0 commit comments

Comments
 (0)