Skip to content

Commit d145b2f

Browse files
authored
fix(compute/serve): skip build if --file set (#1200)
* feat(compute/serve): display viceroy command * fix(compute/serve): skip build if --file set
1 parent da16dfa commit d145b2f

8 files changed

+30
-47
lines changed

pkg/commands/compute/build.go

-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ type BuildCommand struct {
6666
MetadataDisable bool
6767
MetadataFilterEnvVars string
6868
MetadataShow bool
69-
ServeFile bool // skip checking for `binWasmPath` after build as --file might indicate a non-standard named wasm binary
7069
SkipChangeDir bool // set by parent composite commands (e.g. serve, publish)
7170
}
7271

pkg/commands/compute/language_assemblyscript.go

-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func NewAssemblyScript(
5353
metadataFilterEnvVars: c.MetadataFilterEnvVars,
5454
output: out,
5555
postBuild: c.Globals.Manifest.File.Scripts.PostBuild,
56-
serveFile: c.ServeFile,
5756
spinner: spinner,
5857
timeout: c.Flags.Timeout,
5958
verbose: c.Globals.Verbose(),
@@ -85,8 +84,6 @@ type AssemblyScript struct {
8584
// postBuild is a custom script executed after the build but before the Wasm
8685
// binary is added to the .tar.gz archive.
8786
postBuild string
88-
// serveFile indicates if --file was passed as part of `compute serve`.
89-
serveFile bool
9087
// spinner is a terminal progress status indicator.
9188
spinner text.Spinner
9289
// timeout is the build execution threshold.
@@ -159,7 +156,6 @@ func (a *AssemblyScript) Build() error {
159156
nonInteractive: a.nonInteractive,
160157
out: a.output,
161158
postBuild: a.postBuild,
162-
serveFile: a.serveFile,
163159
spinner: a.spinner,
164160
timeout: a.timeout,
165161
verbose: a.verbose,

pkg/commands/compute/language_go.go

-4
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func NewGo(
5353
nonInteractive: c.Globals.Flags.NonInteractive,
5454
output: out,
5555
postBuild: c.Globals.Manifest.File.Scripts.PostBuild,
56-
serveFile: c.ServeFile,
5756
spinner: spinner,
5857
timeout: c.Flags.Timeout,
5958
verbose: c.Globals.Verbose(),
@@ -94,8 +93,6 @@ type Go struct {
9493
// postBuild is a custom script executed after the build but before the Wasm
9594
// binary is added to the .tar.gz archive.
9695
postBuild string
97-
// serveFile indicates if --file was passed as part of `compute serve`.
98-
serveFile bool
9996
// spinner is a terminal progress status indicator.
10097
spinner text.Spinner
10198
// timeout is the build execution threshold.
@@ -190,7 +187,6 @@ func (g *Go) Build() error {
190187
nonInteractive: g.nonInteractive,
191188
out: g.output,
192189
postBuild: g.postBuild,
193-
serveFile: g.serveFile,
194190
spinner: g.spinner,
195191
timeout: g.timeout,
196192
verbose: g.verbose,

pkg/commands/compute/language_javascript.go

-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ func NewJavaScript(
5757
nonInteractive: c.Globals.Flags.NonInteractive,
5858
output: out,
5959
postBuild: c.Globals.Manifest.File.Scripts.PostBuild,
60-
serveFile: c.ServeFile,
6160
spinner: spinner,
6261
timeout: c.Flags.Timeout,
6362
verbose: c.Globals.Verbose(),
@@ -91,8 +90,6 @@ type JavaScript struct {
9190
// postBuild is a custom script executed after the build but before the Wasm
9291
// binary is added to the .tar.gz archive.
9392
postBuild string
94-
// serveFile indicates if --file was passed as part of `compute serve`.
95-
serveFile bool
9693
// spinner is a terminal progress status indicator.
9794
spinner text.Spinner
9895
// timeout is the build execution threshold.
@@ -171,7 +168,6 @@ func (j *JavaScript) Build() error {
171168
nonInteractive: j.nonInteractive,
172169
out: j.output,
173170
postBuild: j.postBuild,
174-
serveFile: j.serveFile,
175171
spinner: j.spinner,
176172
timeout: j.timeout,
177173
verbose: j.verbose,

pkg/commands/compute/language_other.go

-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ func NewOther(
2929
nonInteractive: c.Globals.Flags.NonInteractive,
3030
output: out,
3131
postBuild: c.Globals.Manifest.File.Scripts.PostBuild,
32-
serveFile: c.ServeFile,
3332
spinner: spinner,
3433
timeout: c.Flags.Timeout,
3534
verbose: c.Globals.Verbose(),
@@ -63,8 +62,6 @@ type Other struct {
6362
// postBuild is a custom script executed after the build but before the Wasm
6463
// binary is added to the .tar.gz archive.
6564
postBuild string
66-
// serveFile indicates if --file was passed as part of `compute serve`.
67-
serveFile bool
6865
// spinner is a terminal progress status indicator.
6966
spinner text.Spinner
7067
// timeout is the build execution threshold.
@@ -99,7 +96,6 @@ func (o Other) Build() error {
9996
nonInteractive: o.nonInteractive,
10097
out: o.output,
10198
postBuild: o.postBuild,
102-
serveFile: o.serveFile,
10399
spinner: o.spinner,
104100
timeout: o.timeout,
105101
verbose: o.verbose,

pkg/commands/compute/language_rust.go

-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func NewRust(
6262
nonInteractive: c.Globals.Flags.NonInteractive,
6363
output: out,
6464
postBuild: c.Globals.Manifest.File.Scripts.PostBuild,
65-
serveFile: c.ServeFile,
6665
spinner: spinner,
6766
timeout: c.Flags.Timeout,
6867
verbose: c.Globals.Verbose(),
@@ -102,8 +101,6 @@ type Rust struct {
102101
postBuild string
103102
// projectRoot is the root directory where the Cargo.toml is located.
104103
projectRoot string
105-
// serveFile indicates if --file was passed as part of `compute serve`.
106-
serveFile bool
107104
// spinner is a terminal progress status indicator.
108105
spinner text.Spinner
109106
// timeout is the build execution threshold.
@@ -175,7 +172,6 @@ func (r *Rust) Build() error {
175172
nonInteractive: r.nonInteractive,
176173
out: r.output,
177174
postBuild: r.postBuild,
178-
serveFile: r.serveFile,
179175
spinner: r.spinner,
180176
timeout: r.timeout,
181177
verbose: r.verbose,

pkg/commands/compute/language_toolchain.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ type BuildToolchain struct {
7676
// postBuild is a custom script executed after the build but before the Wasm
7777
// binary is added to the .tar.gz archive.
7878
postBuild string
79-
// serveFile indicates if --file was passed as part of `compute serve`.
80-
serveFile bool
8179
// spinner is a terminal progress status indicator.
8280
spinner text.Spinner
8381
// timeout is the build execution threshold.
@@ -177,19 +175,15 @@ func (bt BuildToolchain) Build() error {
177175
}
178176
}
179177

180-
// We only validate the build output if --file isn't specified in the
181-
// `compute serve` command.
182-
if !bt.serveFile {
183-
// IMPORTANT: The stat check MUST come after the internalPostBuildCallback.
184-
// This is because for Rust it needs to move the binary first.
185-
_, err = os.Stat(binWasmPath)
186-
if err != nil {
187-
return bt.handleError(err)
188-
}
189-
// NOTE: The logic for checking the Wasm binary is 'valid' is not exhaustive.
190-
if err := bt.validateWasm(); err != nil {
191-
return err
192-
}
178+
// IMPORTANT: The stat check MUST come after the internalPostBuildCallback.
179+
// This is because for Rust it needs to move the binary first.
180+
_, err = os.Stat(binWasmPath)
181+
if err != nil {
182+
return bt.handleError(err)
183+
}
184+
// NOTE: The logic for checking the Wasm binary is 'valid' is not exhaustive.
185+
if err := bt.validateWasm(); err != nil {
186+
return err
193187
}
194188

195189
if bt.postBuild != "" {

pkg/commands/compute/serve.go

+21-11
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ type ServeCommand struct {
6464
addr string
6565
debug bool
6666
env argparser.OptionalString
67-
file string
67+
file argparser.OptionalString
6868
profileGuest bool
6969
profileGuestDir argparser.OptionalString
7070
projectDir string
@@ -85,7 +85,7 @@ func NewServeCommand(parent argparser.Registerer, g *global.Data, build *BuildCo
8585
c.CmdClause.Flag("debug", "Run the server in Debug Adapter mode").Hidden().BoolVar(&c.debug)
8686
c.CmdClause.Flag("dir", "Project directory to build (default: current directory)").Short('C').Action(c.dir.Set).StringVar(&c.dir.Value)
8787
c.CmdClause.Flag("env", "The manifest environment config to use (e.g. 'stage' will attempt to read 'fastly.stage.toml')").Action(c.env.Set).StringVar(&c.env.Value)
88-
c.CmdClause.Flag("file", fmt.Sprintf("The Wasm file to run (causes validation checks for %s to be skipped)", binWasmPath)).Default(binWasmPath).StringVar(&c.file)
88+
c.CmdClause.Flag("file", "The Wasm file to run (causes build process to be skipped)").Action(c.file.Set).StringVar(&c.file.Value)
8989
c.CmdClause.Flag("include-source", "Include source code in built package").Action(c.includeSrc.Set).BoolVar(&c.includeSrc.Value)
9090
c.CmdClause.Flag("language", "Language type").Action(c.lang.Set).StringVar(&c.lang.Value)
9191
c.CmdClause.Flag("metadata-disable", "Disable Wasm binary metadata annotations").Action(c.metadataDisable.Set).BoolVar(&c.metadataDisable.Value)
@@ -145,7 +145,16 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
145145
manifestPath = filepath.Join(c.projectDir, manifestFilename)
146146
}
147147

148-
if !c.skipBuild {
148+
wasmBinaryToRun := binWasmPath
149+
if c.file.WasSet {
150+
wasmBinaryToRun = c.file.Value
151+
}
152+
153+
// We skip the build if explicitly told to with --skip-build but also when the
154+
// user sets --file to specify their own wasm binary to pass to Viceroy. This
155+
// is typically for users who compile a Wasm binary using an unsupported
156+
// programming language for the Fastly Compute platform.
157+
if !c.skipBuild && !c.file.WasSet {
149158
err = c.Build(in, out)
150159
if err != nil {
151160
return err
@@ -169,13 +178,16 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
169178
// If the user doesn't set --skip-build then `compute serve` will call
170179
// `compute build` and the logic there will update the manifest in-memory data
171180
// with the relevant project directory and environment manifest content.
172-
if c.skipBuild {
181+
if c.skipBuild || c.file.WasSet {
173182
err := c.Globals.Manifest.File.Read(manifestPath)
174183
if err != nil {
175184
return fmt.Errorf("failed to parse manifest '%s': %w", manifestPath, err)
176185
}
177186
c.ViceroyVersioner.SetRequestedVersion(c.Globals.Manifest.File.LocalServer.ViceroyVersion)
178187
if c.Globals.Verbose() {
188+
if c.skipBuild || c.file.WasSet {
189+
text.Break(out)
190+
}
179191
text.Info(out, "Fastly manifest set to: %s\n\n", manifestPath)
180192
}
181193
}
@@ -210,13 +222,13 @@ func (c *ServeCommand) Exec(in io.Reader, out io.Writer) (err error) {
210222
debug: c.debug,
211223
errLog: c.Globals.ErrLog,
212224
extraArgs: c.ViceroyBinExtraArgs,
213-
file: c.file,
214225
manifestPath: manifestPath,
215226
out: out,
216227
profileGuest: c.profileGuest,
217228
profileGuestDir: c.profileGuestDir,
218229
restarted: restart,
219230
verbose: c.Globals.Verbose(),
231+
wasmBinPath: wasmBinaryToRun,
220232
watch: c.watch,
221233
watchDir: c.watchDir,
222234
})
@@ -276,9 +288,6 @@ func (c *ServeCommand) Build(in io.Reader, out io.Writer) error {
276288
if c.projectDir != "" {
277289
c.build.SkipChangeDir = true // we've already changed directory
278290
}
279-
if c.file != "" {
280-
c.build.ServeFile = true
281-
}
282291
return c.build.Exec(in, out)
283292
}
284293

@@ -548,13 +557,13 @@ type localOpts struct {
548557
debug bool
549558
errLog fsterr.LogInterface
550559
extraArgs string
551-
file string
552560
manifestPath string
553561
out io.Writer
554562
profileGuest bool
555563
profileGuestDir argparser.OptionalString
556564
restarted bool
557565
verbose bool
566+
wasmBinPath string
558567
watch bool
559568
watchDir argparser.OptionalString
560569
}
@@ -564,7 +573,7 @@ func local(opts localOpts) error {
564573
// NOTE: Viceroy no longer displays errors unless in verbose mode.
565574
// This can cause confusion for customers: https://github.com/fastly/cli/issues/913
566575
// So regardless of CLI --verbose flag we'll always set verbose for Viceroy.
567-
args := []string{"-v", "-C", opts.manifestPath, "--addr", opts.addr, opts.file}
576+
args := []string{"-v", "-C", opts.manifestPath, "--addr", opts.addr, opts.wasmBinPath}
568577

569578
if opts.debug {
570579
args = append(args, "--debug")
@@ -591,7 +600,8 @@ func local(opts localOpts) error {
591600
text.Break(opts.out)
592601
}
593602
text.Output(opts.out, "%s: %s", text.BoldYellow("Manifest"), opts.manifestPath)
594-
text.Output(opts.out, "%s: %s", text.BoldYellow("Wasm binary"), opts.file)
603+
text.Output(opts.out, "%s: %s", text.BoldYellow("Wasm binary"), opts.wasmBinPath)
604+
text.Output(opts.out, "%s: %s", text.BoldYellow("Viceroy command"), strings.Join(args, " "))
595605
text.Output(opts.out, "%s: %s", text.BoldYellow("Viceroy binary"), opts.bin)
596606

597607
// gosec flagged this:

0 commit comments

Comments
 (0)