Skip to content

Commit 8357763

Browse files
authored
fix: directory switching logic (#1132)
1 parent 5cbf344 commit 8357763

File tree

3 files changed

+82
-55
lines changed

3 files changed

+82
-55
lines changed

pkg/commands/compute/build.go

+20-9
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ type BuildCommand struct {
6666
MetadataDisable bool
6767
MetadataFilterEnvVars string
6868
MetadataShow bool
69+
SkipChangeDir bool // set by parent composite commands (e.g. serve, publish)
6970
}
7071

7172
// 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) {
112113
}()
113114
manifestPath := filepath.Join(wd, manifestFilename)
114115

115-
projectDir, err := ChangeProjectDirectory(c.Flags.Dir)
116-
if err != nil {
117-
return err
118-
}
119-
if projectDir != "" {
120-
if c.Globals.Verbose() {
121-
text.Info(out, ProjectDirMsg, projectDir)
116+
var projectDir string
117+
if !c.SkipChangeDir {
118+
projectDir, err = ChangeProjectDirectory(c.Flags.Dir)
119+
if err != nil {
120+
return err
121+
}
122+
if projectDir != "" {
123+
if c.Globals.Verbose() {
124+
text.Info(out, ProjectDirMsg, projectDir)
125+
}
126+
manifestPath = filepath.Join(projectDir, manifestFilename)
122127
}
123-
manifestPath = filepath.Join(projectDir, manifestFilename)
124128
}
125129

126130
spinner, err := text.NewSpinner(out)
@@ -135,7 +139,14 @@ func (c *BuildCommand) Exec(in io.Reader, out io.Writer) (err error) {
135139
}(c.Globals.ErrLog)
136140

137141
err = spinner.Process(fmt.Sprintf("Verifying %s", manifestFilename), func(_ *text.SpinnerWrapper) error {
138-
if projectDir != "" || c.Flags.Env != "" {
142+
// The check for c.SkipChangeDir here is because we might need to attempt
143+
// another read of the manifest file. To explain: if we're skipping the
144+
// change of directory, it means we were called from a composite command,
145+
// which has already changed directory to one that contains the fastly.toml
146+
// file. This means we should try reading the manifest file from the new
147+
// location as the potential ReadError() would have been based on the
148+
// initial directory the CLI was invoked from.
149+
if c.SkipChangeDir || projectDir != "" || c.Flags.Env != "" {
139150
err = c.Globals.Manifest.File.Read(manifestPath)
140151
} else {
141152
err = c.Globals.Manifest.File.ReadError()

pkg/commands/compute/publish.go

+46-33
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ type PublishCommand struct {
3737
statusCheckOff bool
3838
statusCheckPath string
3939
statusCheckTimeout int
40+
41+
// Publish private fields
42+
projectDir string
4043
}
4144

4245
// NewPublishCommand returns a usable command registered under the parent.
@@ -93,6 +96,42 @@ func NewPublishCommand(parent argparser.Registerer, g *global.Data, build *Build
9396
// non-deterministic ways. It's best to leave those nested commands to handle
9497
// the progress indicator.
9598
func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
99+
wd, err := os.Getwd()
100+
if err != nil {
101+
return fmt.Errorf("failed to get current working directory: %w", err)
102+
}
103+
defer func() {
104+
_ = os.Chdir(wd)
105+
}()
106+
107+
c.projectDir, err = ChangeProjectDirectory(c.dir.Value)
108+
if err != nil {
109+
return err
110+
}
111+
if c.projectDir != "" {
112+
if c.Globals.Verbose() {
113+
text.Info(out, ProjectDirMsg, c.projectDir)
114+
}
115+
}
116+
117+
err = c.Build(in, out)
118+
if err != nil {
119+
c.Globals.ErrLog.Add(err)
120+
return err
121+
}
122+
123+
text.Break(out)
124+
125+
err = c.Deploy(in, out)
126+
if err != nil {
127+
c.Globals.ErrLog.Add(err)
128+
return err
129+
}
130+
return nil
131+
}
132+
133+
// Build constructs and executes the build logic.
134+
func (c *PublishCommand) Build(in io.Reader, out io.Writer) error {
96135
// Reset the fields on the BuildCommand based on PublishCommand values.
97136
if c.dir.WasSet {
98137
c.build.Flags.Dir = c.dir.Value
@@ -121,33 +160,14 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
121160
if c.metadataShow.WasSet {
122161
c.build.MetadataShow = c.metadataShow.Value
123162
}
124-
125-
err = c.build.Exec(in, out)
126-
if err != nil {
127-
c.Globals.ErrLog.Add(err)
128-
return err
129-
}
130-
131-
text.Break(out)
132-
133-
wd, err := os.Getwd()
134-
if err != nil {
135-
return fmt.Errorf("failed to get current working directory: %w", err)
136-
}
137-
defer func() {
138-
_ = os.Chdir(wd)
139-
}()
140-
141-
projectDir, err := ChangeProjectDirectory(c.dir.Value)
142-
if err != nil {
143-
return err
144-
}
145-
if projectDir != "" {
146-
if c.Globals.Verbose() {
147-
text.Info(out, ProjectDirMsg, projectDir)
148-
}
163+
if c.projectDir != "" {
164+
c.build.SkipChangeDir = true // we've already changed directory
149165
}
166+
return c.build.Exec(in, out)
167+
}
150168

169+
// Deploy constructs and executes the deploy logic.
170+
func (c *PublishCommand) Deploy(in io.Reader, out io.Writer) error {
151171
// Reset the fields on the DeployCommand based on PublishCommand values.
152172
if c.dir.WasSet {
153173
c.deploy.Dir = c.dir.Value
@@ -180,12 +200,5 @@ func (c *PublishCommand) Exec(in io.Reader, out io.Writer) (err error) {
180200
c.deploy.StatusCheckTimeout = c.statusCheckTimeout
181201
}
182202
c.deploy.StatusCheckPath = c.statusCheckPath
183-
184-
err = c.deploy.Exec(in, out)
185-
if err != nil {
186-
c.Globals.ErrLog.Add(err)
187-
return err
188-
}
189-
190-
return nil
203+
return c.deploy.Exec(in, out)
191204
}

pkg/commands/compute/serve.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type ServeCommand struct {
6565
file string
6666
profileGuest bool
6767
profileGuestDir argparser.OptionalString
68+
projectDir string
6869
skipBuild bool
6970
watch bool
7071
watchDir argparser.OptionalString
@@ -114,14 +115,6 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
114115
}
115116
}
116117

117-
if !c.skipBuild {
118-
err = c.Build(in, out)
119-
if err != nil {
120-
return err
121-
}
122-
text.Break(out)
123-
}
124-
125118
manifestFilename := EnvironmentManifest(c.env.Value)
126119
if c.env.Value != "" {
127120
if c.Globals.Verbose() {
@@ -138,15 +131,23 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
138131
}()
139132
manifestPath := filepath.Join(wd, manifestFilename)
140133

141-
projectDir, err := ChangeProjectDirectory(c.dir.Value)
134+
c.projectDir, err = ChangeProjectDirectory(c.dir.Value)
142135
if err != nil {
143136
return err
144137
}
145-
if projectDir != "" {
138+
if c.projectDir != "" {
146139
if c.Globals.Verbose() {
147-
text.Info(out, ProjectDirMsg, projectDir)
140+
text.Info(out, ProjectDirMsg, c.projectDir)
141+
}
142+
manifestPath = filepath.Join(c.projectDir, manifestFilename)
143+
}
144+
145+
if !c.skipBuild {
146+
err = c.Build(in, out)
147+
if err != nil {
148+
return err
148149
}
149-
manifestPath = filepath.Join(projectDir, manifestFilename)
150+
text.Break(out)
150151
}
151152

152153
c.setBackendsWithDefaultOverrideHostIfMissing(out)
@@ -268,7 +269,9 @@ func (c *ServeCommand) Build(in io.Reader, out io.Writer) error {
268269
if c.metadataShow.WasSet {
269270
c.build.MetadataShow = c.metadataShow.Value
270271
}
271-
272+
if c.projectDir != "" {
273+
c.build.SkipChangeDir = true // we've already changed directory
274+
}
272275
return c.build.Exec(in, out)
273276
}
274277

0 commit comments

Comments
 (0)