Skip to content

Commit aff5fee

Browse files
Delete temporary sketch copy after build
When a sketch has unsaved changes, a temporary copy of the sketch is made with those changes applied. This copy is then passed to arduino-builder. Previously, this temporary copy was kept around and only deleted when the IDE was closed. However, all files were written to it again on every build, so keeping the old files around did not serve any real purpose. When a file was renamed in the IDE, the original name would still be present in the temporary copy, and could cause linker errors when both were compiled. This commit makes sure the temporary copy is deleted after every build, instead of at IDE exit, which fixes this problem with renames. When a file is deleted from the sketch, the file would also be deleted from the temporary problem, presumably to fix this same problem for deletes (but renames were forgotten). With this commit, this special handling for deleting files is no longer needed, so it is removed. This fixes arduino#4335
1 parent a73d393 commit aff5fee

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

Diff for: app/src/processing/app/Sketch.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import cc.arduino.Compiler;
2727
import cc.arduino.CompilerProgressListener;
2828
import cc.arduino.UploaderUtils;
29-
import cc.arduino.files.DeleteFilesOnShutdown;
3029
import cc.arduino.packages.Uploader;
3130
import org.apache.commons.codec.digest.DigestUtils;
3231
import processing.app.debug.RunnerException;
@@ -463,7 +462,7 @@ public void handleDeleteCode() throws IOException {
463462

464463
} else {
465464
// delete the file
466-
if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath(), Paths.get(System.getProperty("java.io.tmpdir"), "arduino_" + DigestUtils.md5Hex(getMainFilePath())))) {
465+
if (!current.getCode().deleteFile(BaseNoGui.getBuildFolder(data).toPath())) {
467466
Base.showMessage(tr("Couldn't do it"),
468467
I18n.format(tr("Could not delete \"{0}\"."), current.getCode().getFileName()));
469468
return;
@@ -1100,17 +1099,27 @@ private String build(String buildPath, boolean verbose, boolean save) throws Run
11001099

11011100
CompilerProgressListener progressListener = editor.status::progressUpdate;
11021101

1102+
boolean deleteTemp = false;
11031103
String pathToSketch = data.getMainFilePath();
11041104
if (isModified()) {
1105+
// If any files are modified, make a copy of the sketch with the changes
1106+
// saved, so arduino-builder will see the modifications.
11051107
pathToSketch = saveSketchInTempFolder();
1108+
deleteTemp = true;
11061109
}
11071110

1108-
return new Compiler(pathToSketch, data, buildPath).build(progressListener, save);
1111+
try {
1112+
return new Compiler(pathToSketch, data, buildPath).build(progressListener,
1113+
save);
1114+
} finally {
1115+
// Make sure we clean up any temporary sketch copy
1116+
if (deleteTemp)
1117+
FileUtils.recursiveDelete(new File(pathToSketch).getParentFile());
1118+
}
11091119
}
11101120

11111121
private String saveSketchInTempFolder() throws IOException {
11121122
File tempFolder = FileUtils.createTempFolder("arduino_", DigestUtils.md5Hex(data.getMainFilePath()));
1113-
DeleteFilesOnShutdown.add(tempFolder);
11141123
FileUtils.copy(getFolder(), tempFolder);
11151124

11161125
for (SketchCode sc : Stream.of(data.getCodes()).filter(SketchCode::isModified).collect(Collectors.toList())) {

Diff for: arduino-core/src/processing/app/SketchCode.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,13 @@ protected boolean fileReadOnly() {
9191
}
9292

9393

94-
protected boolean deleteFile(Path tempBuildFolder, Path tempUnsavedSketchPath) throws IOException {
94+
protected boolean deleteFile(Path tempBuildFolder) throws IOException {
9595
if (!file.delete()) {
9696
return false;
9797
}
9898

99-
List<Path> tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch"), tempUnsavedSketchPath)
100-
.filter(path -> Files.exists(path))
101-
.collect(Collectors.toList());
99+
List<Path> tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch"))
100+
.filter(path -> Files.exists(path)).collect(Collectors.toList());
102101

103102
for (Path folder : tempBuildFolders) {
104103
if (!deleteCompiledFilesFrom(folder)) {

0 commit comments

Comments
 (0)