Skip to content

[skip-changelog] Handle repeated builds of a sketch when --build-path target is in the sketch directory #2084

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
merged 2 commits into from
Mar 1, 2023
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
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)
}