Skip to content

Commit

Permalink
[skip-changelog] Handle repeated builds of a sketch when `--build-pat…
Browse files Browse the repository at this point in the history
…h` target is in the sketch directory (#2084)

* Handle repeated builds of a sketch when the build path is inside the sketch directory

* Add TestCompileBuildPathInsideSketch to compile_test.go
  • Loading branch information
MatteoPologruto committed Mar 1, 2023
1 parent e7ba99f commit 78bfa74
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
28 changes: 28 additions & 0 deletions commands/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
builderCtx.BuildPath = sk.DefaultBuildPath()
} else {
builderCtx.BuildPath = paths.New(req.GetBuildPath()).Canonical()
if in, err := builderCtx.BuildPath.IsInsideDir(sk.FullPath); err != nil {
return nil, &arduino.NotFoundError{Message: tr("Cannot find build path"), Cause: err}
} else if in && builderCtx.BuildPath.IsDir() {
if sk.AdditionalFiles, err = removeBuildFromSketchFiles(sk.AdditionalFiles, builderCtx.BuildPath); err != nil {
return nil, err
}
}
}
if err = builderCtx.BuildPath.MkdirAll(); err != nil {
return nil, &arduino.PermissionDeniedError{Message: tr("Cannot create build directory"), Cause: err}
Expand Down Expand Up @@ -315,3 +322,24 @@ func maybePurgeBuildCache() {
buildcache.New(paths.TempDir().Join("arduino", "cores")).Purge(cacheTTL)
buildcache.New(paths.TempDir().Join("arduino", "sketches")).Purge(cacheTTL)
}

// removeBuildFromSketchFiles removes the files contained in the build directory from
// the list of the sketch files
func removeBuildFromSketchFiles(files paths.PathList, build *paths.Path) (paths.PathList, error) {
var res paths.PathList
ignored := false
for _, file := range files {
if in, err := file.IsInsideDir(build); err != nil {
return nil, &arduino.NotFoundError{Message: tr("Cannot find build path"), Cause: err}
} else if !in {
res = append(res, file)
} else if !ignored {
ignored = true
}
}
// log only if at least a file is ignored
if ignored {
logrus.Tracef("Build path %s is a child of sketch path and it is ignored for additional files.", build.String())
}
return res, nil
}
24 changes: 24 additions & 0 deletions internal/integrationtest/compile_3/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ func TestRuntimeToolPropertiesGeneration(t *testing.T) {
require.True(t, res.GetPath("runtime.tools.avrdude.path").EquivalentTo(hardwareDir.Join("arduino", "tools", "avrdude", "6.3.0-arduino17")))
}
}

func TestCompileBuildPathInsideSketch(t *testing.T) {
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
defer env.CleanUp()

_, _, err := cli.Run("core", "update-index")
require.NoError(t, err)

_, _, err = cli.Run("core", "install", "arduino:avr")
require.NoError(t, err)

sketch := "sketchSimple"
_, _, err = cli.Run("sketch", "new", sketch)
require.NoError(t, err)

cli.SetWorkingDir(cli.WorkingDir().Join(sketch))
// Compile the sketch creating the build directory inside the sketch directory
_, _, err = cli.Run("compile", "-b", "arduino:avr:mega", "--build-path", "build-mega")
require.NoError(t, err)

// Compile again using the same build path
_, _, err = cli.Run("compile", "-b", "arduino:avr:mega", "--build-path", "build-mega")
require.NoError(t, err)
}

0 comments on commit 78bfa74

Please sign in to comment.