Unverified Commit 78bfa744 authored by MatteoPologruto's avatar MatteoPologruto Committed by GitHub

[skip-changelog] Handle repeated builds of a sketch when `--build-path` target...

[skip-changelog] Handle repeated builds of a sketch when `--build-path` 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
parent e7ba99f4
......@@ -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}
......@@ -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
}
......@@ -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)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment