From 8357763b179fe141029dc44e922dd4ad67e2af99 Mon Sep 17 00:00:00 2001 From: Mark McDonnell Date: Thu, 15 Feb 2024 11:19:27 +0000 Subject: [PATCH] fix: directory switching logic (#1132) --- pkg/commands/compute/build.go | 29 ++++++++---- pkg/commands/compute/publish.go | 79 +++++++++++++++++++-------------- pkg/commands/compute/serve.go | 29 ++++++------ 3 files changed, 82 insertions(+), 55 deletions(-) diff --git a/pkg/commands/compute/build.go b/pkg/commands/compute/build.go index 84abad5fd..afdc7841a 100644 --- a/pkg/commands/compute/build.go +++ b/pkg/commands/compute/build.go @@ -66,6 +66,7 @@ type BuildCommand struct { MetadataDisable bool MetadataFilterEnvVars string MetadataShow bool + SkipChangeDir bool // set by parent composite commands (e.g. serve, publish) } // NewBuildCommand returns a usable command registered under the parent. @@ -112,15 +113,18 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) { }() manifestPath := filepath.Join(wd, manifestFilename) - projectDir, err := ChangeProjectDirectory(c.Flags.Dir) - if err != nil { - return err - } - if projectDir != "" { - if c.Globals.Verbose() { - text.Info(out, ProjectDirMsg, projectDir) + var projectDir string + if !c.SkipChangeDir { + projectDir, err = ChangeProjectDirectory(c.Flags.Dir) + if err != nil { + return err + } + if projectDir != "" { + if c.Globals.Verbose() { + text.Info(out, ProjectDirMsg, projectDir) + } + manifestPath = filepath.Join(projectDir, manifestFilename) } - manifestPath = filepath.Join(projectDir, manifestFilename) } spinner, err := text.NewSpinner(out) @@ -135,7 +139,14 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) { }(c.Globals.ErrLog) err = spinner.Process(fmt.Sprintf("Verifying %s", manifestFilename), func(_ *text.SpinnerWrapper) error { - if projectDir != "" || c.Flags.Env != "" { + // The check for c.SkipChangeDir here is because we might need to attempt + // another read of the manifest file. To explain: if we're skipping the + // change of directory, it means we were called from a composite command, + // which has already changed directory to one that contains the fastly.toml + // file. This means we should try reading the manifest file from the new + // location as the potential ReadError() would have been based on the + // initial directory the CLI was invoked from. + if c.SkipChangeDir || projectDir != "" || c.Flags.Env != "" { err = c.Globals.Manifest.File.Read(manifestPath) } else { err = c.Globals.Manifest.File.ReadError() diff --git a/pkg/commands/compute/publish.go b/pkg/commands/compute/publish.go index 02bc83acb..a695de484 100644 --- a/pkg/commands/compute/publish.go +++ b/pkg/commands/compute/publish.go @@ -37,6 +37,9 @@ type PublishCommand struct { statusCheckOff bool statusCheckPath string statusCheckTimeout int + + // Publish private fields + projectDir string } // NewPublishCommand returns a usable command registered under the parent. @@ -93,6 +96,42 @@ func NewPublishCommand(parent argparser.Registerer, g *global.Data, build *Build // non-deterministic ways. It's best to leave those nested commands to handle // the progress indicator. func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) { + wd, err := os.Getwd() + if err != nil { + return fmt.Errorf("failed to get current working directory: %w", err) + } + defer func() { + _ = os.Chdir(wd) + }() + + c.projectDir, err = ChangeProjectDirectory(c.dir.Value) + if err != nil { + return err + } + if c.projectDir != "" { + if c.Globals.Verbose() { + text.Info(out, ProjectDirMsg, c.projectDir) + } + } + + err = c.Build(in, out) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + + text.Break(out) + + err = c.Deploy(in, out) + if err != nil { + c.Globals.ErrLog.Add(err) + return err + } + return nil +} + +// Build constructs and executes the build logic. +func (c *PublishCommand) Build(in io.Reader, out io.Writer) error { // Reset the fields on the BuildCommand based on PublishCommand values. if c.dir.WasSet { c.build.Flags.Dir = c.dir.Value @@ -121,33 +160,14 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) { if c.metadataShow.WasSet { c.build.MetadataShow = c.metadataShow.Value } - - err = c.build.Exec(in, out) - if err != nil { - c.Globals.ErrLog.Add(err) - return err - } - - text.Break(out) - - wd, err := os.Getwd() - if err != nil { - return fmt.Errorf("failed to get current working directory: %w", err) - } - defer func() { - _ = os.Chdir(wd) - }() - - projectDir, err := ChangeProjectDirectory(c.dir.Value) - if err != nil { - return err - } - if projectDir != "" { - if c.Globals.Verbose() { - text.Info(out, ProjectDirMsg, projectDir) - } + if c.projectDir != "" { + c.build.SkipChangeDir = true // we've already changed directory } + return c.build.Exec(in, out) +} +// Deploy constructs and executes the deploy logic. +func (c *PublishCommand) Deploy(in io.Reader, out io.Writer) error { // Reset the fields on the DeployCommand based on PublishCommand values. if c.dir.WasSet { c.deploy.Dir = c.dir.Value @@ -180,12 +200,5 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) { c.deploy.StatusCheckTimeout = c.statusCheckTimeout } c.deploy.StatusCheckPath = c.statusCheckPath - - err = c.deploy.Exec(in, out) - if err != nil { - c.Globals.ErrLog.Add(err) - return err - } - - return nil + return c.deploy.Exec(in, out) } diff --git a/pkg/commands/compute/serve.go b/pkg/commands/compute/serve.go index 022d71753..90547299b 100644 --- a/pkg/commands/compute/serve.go +++ b/pkg/commands/compute/serve.go @@ -65,6 +65,7 @@ type ServeCommand struct { file string profileGuest bool profileGuestDir argparser.OptionalString + projectDir string skipBuild bool watch bool watchDir argparser.OptionalString @@ -114,14 +115,6 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) { } } - if !c.skipBuild { - err = c.Build(in, out) - if err != nil { - return err - } - text.Break(out) - } - manifestFilename := EnvironmentManifest(c.env.Value) if c.env.Value != "" { if c.Globals.Verbose() { @@ -138,15 +131,23 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) { }() manifestPath := filepath.Join(wd, manifestFilename) - projectDir, err := ChangeProjectDirectory(c.dir.Value) + c.projectDir, err = ChangeProjectDirectory(c.dir.Value) if err != nil { return err } - if projectDir != "" { + if c.projectDir != "" { if c.Globals.Verbose() { - text.Info(out, ProjectDirMsg, projectDir) + text.Info(out, ProjectDirMsg, c.projectDir) + } + manifestPath = filepath.Join(c.projectDir, manifestFilename) + } + + if !c.skipBuild { + err = c.Build(in, out) + if err != nil { + return err } - manifestPath = filepath.Join(projectDir, manifestFilename) + text.Break(out) } c.setBackendsWithDefaultOverrideHostIfMissing(out) @@ -268,7 +269,9 @@ func (c *ServeCommand) Build(in io.Reader, out io.Writer) error { if c.metadataShow.WasSet { c.build.MetadataShow = c.metadataShow.Value } - + if c.projectDir != "" { + c.build.SkipChangeDir = true // we've already changed directory + } return c.build.Exec(in, out) }