diff --git a/app/src/processing/app/Sketch.java b/app/src/processing/app/Sketch.java index aa2c8c71948..444888c907d 100644 --- a/app/src/processing/app/Sketch.java +++ b/app/src/processing/app/Sketch.java @@ -26,7 +26,6 @@ import cc.arduino.Compiler; import cc.arduino.CompilerProgressListener; import cc.arduino.UploaderUtils; -import cc.arduino.files.DeleteFilesOnShutdown; import cc.arduino.packages.Uploader; import org.apache.commons.codec.digest.DigestUtils; import processing.app.debug.RunnerException; @@ -463,7 +462,7 @@ public void handleDeleteCode() throws IOException { } else { // delete the file - if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath(), Paths.get(System.getProperty("java.io.tmpdir"), "arduino_" + DigestUtils.md5Hex(getMainFilePath())))) { + if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath())) { Base.showMessage(tr("Couldn't do it"), I18n.format(tr("Could not delete \"{0}\"."), current.getCode().getFileName())); return; @@ -1100,17 +1099,27 @@ private String build(String buildPath, boolean verbose, boolean save) throws Run CompilerProgressListener progressListener = editor.status::progressUpdate; + boolean deleteTemp = false; String pathToSketch = data.getMainFilePath(); if (isModified()) { + // If any files are modified, make a copy of the sketch with the changes + // saved, so arduino-builder will see the modifications. pathToSketch = saveSketchInTempFolder(); + deleteTemp = true; } - return new Compiler(pathToSketch, data, buildPath).build(progressListener, save); + try { + return new Compiler(pathToSketch, data, buildPath).build(progressListener, + save); + } finally { + // Make sure we clean up any temporary sketch copy + if (deleteTemp) + FileUtils.recursiveDelete(new File(pathToSketch).getParentFile()); + } } private String saveSketchInTempFolder() throws IOException { - File tempFolder = FileUtils.createTempFolder("arduino_", DigestUtils.md5Hex(data.getMainFilePath())); - DeleteFilesOnShutdown.add(tempFolder); + File tempFolder = FileUtils.createTempFolder("arduino_modified_sketch_"); FileUtils.copy(getFolder(), tempFolder); for (SketchCode sc : Stream.of(data.getCodes()).filter(SketchCode::isModified).collect(Collectors.toList())) { diff --git a/arduino-core/src/processing/app/SketchCode.java b/arduino-core/src/processing/app/SketchCode.java index 88d2a51965e..19c91d70ffe 100644 --- a/arduino-core/src/processing/app/SketchCode.java +++ b/arduino-core/src/processing/app/SketchCode.java @@ -91,14 +91,13 @@ protected boolean fileReadOnly() { } - protected boolean deleteFile(Path tempBuildFolder, Path tempUnsavedSketchPath) throws IOException { + protected boolean deleteFile(Path tempBuildFolder) throws IOException { if (!file.delete()) { return false; } - List tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch"), tempUnsavedSketchPath) - .filter(path -> Files.exists(path)) - .collect(Collectors.toList()); + List tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch")) + .filter(path -> Files.exists(path)).collect(Collectors.toList()); for (Path folder : tempBuildFolders) { if (!deleteCompiledFilesFrom(folder)) {