Skip to content

Fix empty copy of compiled binaries when --build-path equals to --output-dir #2320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,37 +305,35 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
return r, err
}

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

// Copy all "sketch.ino.*" artifacts to the export directory
baseName, ok := sketchBuilder.GetBuildProperties().GetOk("build.project_name") // == "sketch.ino"
if !ok {
return r, &arduino.MissingPlatformPropertyError{Property: "build.project_name"}
}
buildFiles, err := sketchBuilder.GetBuildPath().ReadDir()
if err != nil {
return r, &arduino.PermissionDeniedError{Message: tr("Error reading build directory"), Cause: err}
}
buildFiles.FilterPrefix(baseName)
for _, buildFile := range buildFiles {
exportedFile := exportPath.Join(buildFile.Base())
logrus.
WithField("src", buildFile).
WithField("dest", exportedFile).
Trace("Copying artifact.")
if err = buildFile.CopyTo(exportedFile); err != nil {
return r, &arduino.PermissionDeniedError{Message: tr("Error copying output file %s", buildFile), Cause: err}
if !buildPath.EqualsTo(exportPath) {
logrus.WithField("path", exportPath).Trace("Saving sketch to export path.")
if err := exportPath.MkdirAll(); err != nil {
return r, &arduino.PermissionDeniedError{Message: tr("Error creating output dir"), Cause: err}
}

baseName, ok := sketchBuilder.GetBuildProperties().GetOk("build.project_name") // == "sketch.ino"
if !ok {
return r, &arduino.MissingPlatformPropertyError{Property: "build.project_name"}
}
buildFiles, err := sketchBuilder.GetBuildPath().ReadDir()
if err != nil {
return r, &arduino.PermissionDeniedError{Message: tr("Error reading build directory"), Cause: err}
}
buildFiles.FilterPrefix(baseName)
for _, buildFile := range buildFiles {
exportedFile := exportPath.Join(buildFile.Base())
logrus.WithField("src", buildFile).WithField("dest", exportedFile).Trace("Copying artifact.")
if err = buildFile.CopyTo(exportedFile); err != nil {
return r, &arduino.PermissionDeniedError{Message: tr("Error copying output file %s", buildFile), Cause: err}
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions internal/integrationtest/compile_1/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestCompile(t *testing.T) {
{"WithFakeSecureBootCore", compileWithFakeSecureBootCore},
{"PreprocessFlagDoNotMessUpWithOutput", preprocessFlagDoNotMessUpWithOutput},
{"WithCustomBuildPath", buildWithCustomBuildPath},
{"WithCustomBuildPathAndOUtputDirFlag", buildWithCustomBuildPathAndOUtputDirFlag},
}.Run(t, env, cli)
}

Expand Down Expand Up @@ -1227,3 +1228,34 @@ func buildWithCustomBuildPath(t *testing.T, env *integrationtest.Environment, cl
require.Error(t, err)
})
}

func buildWithCustomBuildPathAndOUtputDirFlag(t *testing.T, env *integrationtest.Environment, cli *integrationtest.ArduinoCLI) {
fqbn := "arduino:avr:uno"
sketchName := "bare_minimum"
sketchPath := cli.SketchbookDir().Join(sketchName)
defer sketchPath.RemoveAll()

// Create a test sketch
_, _, err := cli.Run("sketch", "new", sketchPath.String())
require.NoError(t, err)

buildPath := cli.DataDir().Join("test_dir", "build_dir")
outputDirPath := buildPath
_, _, err = cli.Run("compile", "-b", fqbn, sketchPath.String(), "--build-path", buildPath.String(), "--output-dir", outputDirPath.String())
require.NoError(t, err)

// Verifies that output binaries are not empty.
require.DirExists(t, buildPath.String())
files := []*paths.Path{
buildPath.Join(sketchName + ".ino.eep"),
buildPath.Join(sketchName + ".ino.elf"),
buildPath.Join(sketchName + ".ino.hex"),
buildPath.Join(sketchName + ".ino.with_bootloader.bin"),
buildPath.Join(sketchName + ".ino.with_bootloader.hex"),
}
for _, file := range files {
content, err := file.ReadFile()
require.NoError(t, err)
require.NotEmpty(t, content)
}
}