Skip to content

Commit 1b3ef43

Browse files
fix empty build files exported if specified --build-path equals to --output-dir
When compiling with --output-dir pointing to the same directory of --build-path do not copy file
1 parent f6645a8 commit 1b3ef43

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

Diff for: commands/compile/compile.go

+23-25
Original file line numberDiff line numberDiff line change
@@ -305,37 +305,35 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
305305
return r, err
306306
}
307307

308-
var exportPath *paths.Path
309-
if exportDir := req.GetExportDir(); exportDir != "" {
310-
exportPath = paths.New(exportDir)
311-
} else {
308+
exportPath := paths.New(req.GetExportDir())
309+
if exportPath == nil {
312310
// Add FQBN (without configs part) to export path
313311
fqbnSuffix := strings.ReplaceAll(fqbn.StringWithoutConfig(), ":", ".")
314312
exportPath = sk.FullPath.Join("build", fqbnSuffix)
315313
}
316-
logrus.WithField("path", exportPath).Trace("Saving sketch to export path.")
317-
if err := exportPath.MkdirAll(); err != nil {
318-
return r, &arduino.PermissionDeniedError{Message: tr("Error creating output dir"), Cause: err}
319-
}
320314

321315
// Copy all "sketch.ino.*" artifacts to the export directory
322-
baseName, ok := sketchBuilder.GetBuildProperties().GetOk("build.project_name") // == "sketch.ino"
323-
if !ok {
324-
return r, &arduino.MissingPlatformPropertyError{Property: "build.project_name"}
325-
}
326-
buildFiles, err := sketchBuilder.GetBuildPath().ReadDir()
327-
if err != nil {
328-
return r, &arduino.PermissionDeniedError{Message: tr("Error reading build directory"), Cause: err}
329-
}
330-
buildFiles.FilterPrefix(baseName)
331-
for _, buildFile := range buildFiles {
332-
exportedFile := exportPath.Join(buildFile.Base())
333-
logrus.
334-
WithField("src", buildFile).
335-
WithField("dest", exportedFile).
336-
Trace("Copying artifact.")
337-
if err = buildFile.CopyTo(exportedFile); err != nil {
338-
return r, &arduino.PermissionDeniedError{Message: tr("Error copying output file %s", buildFile), Cause: err}
316+
if !buildPath.EqualsTo(exportPath) {
317+
logrus.WithField("path", exportPath).Trace("Saving sketch to export path.")
318+
if err := exportPath.MkdirAll(); err != nil {
319+
return r, &arduino.PermissionDeniedError{Message: tr("Error creating output dir"), Cause: err}
320+
}
321+
322+
baseName, ok := sketchBuilder.GetBuildProperties().GetOk("build.project_name") // == "sketch.ino"
323+
if !ok {
324+
return r, &arduino.MissingPlatformPropertyError{Property: "build.project_name"}
325+
}
326+
buildFiles, err := sketchBuilder.GetBuildPath().ReadDir()
327+
if err != nil {
328+
return r, &arduino.PermissionDeniedError{Message: tr("Error reading build directory"), Cause: err}
329+
}
330+
buildFiles.FilterPrefix(baseName)
331+
for _, buildFile := range buildFiles {
332+
exportedFile := exportPath.Join(buildFile.Base())
333+
logrus.WithField("src", buildFile).WithField("dest", exportedFile).Trace("Copying artifact.")
334+
if err = buildFile.CopyTo(exportedFile); err != nil {
335+
return r, &arduino.PermissionDeniedError{Message: tr("Error copying output file %s", buildFile), Cause: err}
336+
}
339337
}
340338
}
341339

Diff for: internal/integrationtest/compile_1/compile_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func TestCompile(t *testing.T) {
7373
{"WithFakeSecureBootCore", compileWithFakeSecureBootCore},
7474
{"PreprocessFlagDoNotMessUpWithOutput", preprocessFlagDoNotMessUpWithOutput},
7575
{"WithCustomBuildPath", buildWithCustomBuildPath},
76+
{"WithCustomBuildPathAndOUtputDirFlag", buildWithCustomBuildPathAndOUtputDirFlag},
7677
}.Run(t, env, cli)
7778
}
7879

@@ -1227,3 +1228,34 @@ func buildWithCustomBuildPath(t *testing.T, env *integrationtest.Environment, cl
12271228
require.Error(t, err)
12281229
})
12291230
}
1231+
1232+
func buildWithCustomBuildPathAndOUtputDirFlag(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
1233+
fqbn := "arduino:avr:uno"
1234+
sketchName := "bare_minimum"
1235+
sketchPath := cli.SketchbookDir().Join(sketchName)
1236+
defer sketchPath.RemoveAll()
1237+
1238+
// Create a test sketch
1239+
_, _, err := cli.Run("sketch", "new", sketchPath.String())
1240+
require.NoError(t, err)
1241+
1242+
buildPath := cli.DataDir().Join("test_dir", "build_dir")
1243+
outputDirPath := buildPath
1244+
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--build-path", buildPath.String(), "--output-dir", outputDirPath.String())
1245+
require.NoError(t, err)
1246+
1247+
// Verifies that output binaries are not empty.
1248+
require.DirExists(t, buildPath.String())
1249+
files := []*paths.Path{
1250+
buildPath.Join(sketchName + ".ino.eep"),
1251+
buildPath.Join(sketchName + ".ino.elf"),
1252+
buildPath.Join(sketchName + ".ino.hex"),
1253+
buildPath.Join(sketchName + ".ino.with_bootloader.bin"),
1254+
buildPath.Join(sketchName + ".ino.with_bootloader.hex"),
1255+
}
1256+
for _, file := range files {
1257+
content, err := file.ReadFile()
1258+
require.NoError(t, err)
1259+
require.NotEmpty(t, content)
1260+
}
1261+
}

0 commit comments

Comments
 (0)