Skip to content

Commit f559528

Browse files
committed
[breaking] Fix export binaries binding not working in gRPC interface
1 parent a9b0c9d commit f559528

File tree

6 files changed

+117
-96
lines changed

6 files changed

+117
-96
lines changed

cli/compile/compile.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ var (
5555
optimizeForDebug bool // Optimize compile output for debug, not for release
5656
programmer string // Use the specified programmer to upload
5757
clean bool // Cleanup the build folder and do not use any cached build
58-
exportBinaries bool // Copies compiled binaries to sketch folder when true
5958
compilationDatabaseOnly bool // Only create compilation database without actually compiling
6059
sourceOverrides string // Path to a .json file that contains a set of replacements of the sketch source code.
6160
)
@@ -100,8 +99,7 @@ func NewCommand() *cobra.Command {
10099
command.Flags().StringVarP(&programmer, "programmer", "P", "", "Optional, use the specified programmer to upload.")
101100
command.Flags().BoolVar(&compilationDatabaseOnly, "only-compilation-database", false, "Just produce the compilation database, without actually compiling.")
102101
command.Flags().BoolVar(&clean, "clean", false, "Optional, cleanup the build folder and do not use any cached build.")
103-
// We must use the following syntax for this flag since it's also bound to settings, we could use the other one too
104-
// but it wouldn't make sense since we still must explicitly set the exportBinaries variable by reading from settings.
102+
// We must use the following syntax for this flag since it's also bound to settings.
105103
// This must be done because the value is set when the binding is accessed from viper. Accessing from cobra would only
106104
// read the value if the flag is set explicitly by the user.
107105
command.Flags().BoolP("export-binaries", "e", false, "If set built binaries will be exported to the sketch folder.")
@@ -137,11 +135,6 @@ func run(cmd *cobra.Command, args []string) {
137135
}
138136
}
139137

140-
// We must read this from settings since the value is set when the binding is accessed from viper,
141-
// accessing it from cobra would only read it if the flag is explicitly set by the user and ignore
142-
// the config file and the env vars.
143-
exportBinaries = configuration.Settings.GetBool("sketch.always_export_binaries")
144-
145138
var overrides map[string]string
146139
if sourceOverrides != "" {
147140
data, err := paths.New(sourceOverrides).ReadFile()
@@ -176,7 +169,6 @@ func run(cmd *cobra.Command, args []string) {
176169
Libraries: libraries,
177170
OptimizeForDebug: optimizeForDebug,
178171
Clean: clean,
179-
ExportBinaries: exportBinaries,
180172
CreateCompilationDatabaseOnly: compilationDatabaseOnly,
181173
SourceOverride: overrides,
182174
}

commands/compile/compile.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,19 @@ import (
4545
// Compile FIXMEDOC
4646
func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.Writer, debug bool) (r *rpc.CompileResp, e error) {
4747

48+
// There is a binding between the export binaries setting and the CLI flag to explicitly set it,
49+
// since we want this binding to work also for the gRPC interface we must read it here in this
50+
// package instead of the cli/compile one, otherwise we'd lose the binding.
51+
exportBinaries := configuration.Settings.GetBool("sketch.always_export_binaries")
52+
// If we'd just read the binding in any case, even if the request sets the export binaries setting,
53+
// the settings value would always overwrite the request one and it wouldn't have any effect
54+
// setting it for individual requests. To solve this we use a wrapper.BoolValue to handle
55+
// the optionality of this property, otherwise we would have no way of knowing if the property
56+
// was set in the request or it's just the default boolean value.
57+
if reqExportBinaries := req.GetExportBinaries(); reqExportBinaries != nil {
58+
exportBinaries = reqExportBinaries.Value
59+
}
60+
4861
tags := map[string]string{
4962
"fqbn": req.Fqbn,
5063
"sketchPath": metrics.Sanitize(req.SketchPath),
@@ -59,7 +72,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
5972
"jobs": strconv.FormatInt(int64(req.Jobs), 10),
6073
"libraries": strings.Join(req.Libraries, ","),
6174
"clean": strconv.FormatBool(req.GetClean()),
62-
"exportBinaries": strconv.FormatBool(req.GetExportBinaries()),
75+
"exportBinaries": strconv.FormatBool(exportBinaries),
6376
}
6477

6578
// Use defer func() to evaluate tags map when function returns
@@ -214,7 +227,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
214227
}
215228

216229
// If the export directory is set we assume you want to export the binaries
217-
if req.GetExportBinaries() || req.GetExportDir() != "" {
230+
if exportBinaries || req.GetExportDir() != "" {
218231
var exportPath *paths.Path
219232
if exportDir := req.GetExportDir(); exportDir != "" {
220233
exportPath = paths.New(exportDir)

docs/UPGRADING.md

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ Here you can find a list of migration guides to handle breaking changes between
44

55
## Unreleased
66

7+
### Change type of `CompileReq.ExportBinaries` message in gRPC interface
8+
9+
This change affects only the gRPC consumers.
10+
11+
In the `CompileReq` message the `export_binaries` property type has been changed from `bool` to
12+
`google.protobuf.BoolValue`. This has been done to handle settings bindings by gRPC consumers and the CLI in the same
13+
way so that they an identical behaviour.
14+
715
## 0.15.0
816

917
### Rename `telemetry` settings to `metrics`

0 commit comments

Comments
 (0)