diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportJob.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportJob.java index c7bf3059348..6b0596e9ef1 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportJob.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/wizards/datatransfer/SmartImportJob.java @@ -168,13 +168,11 @@ public void setListener(RecursiveImportListener listener) { @Override public IStatus run(IProgressMonitor monitor) { + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + boolean isAutoBuilding = workspace.isAutoBuilding(); try { - IWorkspace workspace = ResourcesPlugin.getWorkspace(); - IWorkspaceDescription description = workspace.getDescription(); - boolean isAutoBuilding = workspace.isAutoBuilding(); if (isAutoBuilding) { - description.setAutoBuilding(false); - workspace.setDescription(description); + setAutoBuilding(workspace, false); } if (directoriesToImport != null) { @@ -270,21 +268,32 @@ protected IStatus run(IProgressMonitor aMonitor) { try { project.close(monitor); } catch (CoreException e) { - listener.errorHappened(project.getLocation(), e); + if (listener != null) { + listener.errorHappened(project.getLocation(), e); + } } }); } - - if (isAutoBuilding) { - description.setAutoBuilding(true); - workspace.setDescription(description); - } } catch (Exception ex) { return new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, ex.getMessage(), ex); + } finally { + if (isAutoBuilding) { + try { + setAutoBuilding(workspace, true); + } catch (CoreException ex) { + IDEWorkbenchPlugin.log("Could not restore auto-building after import", ex); //$NON-NLS-1$ + } + } } return Status.OK_STATUS; } + private static void setAutoBuilding(IWorkspace workspace, boolean autoBuilding) throws CoreException { + IWorkspaceDescription description = workspace.getDescription(); + description.setAutoBuilding(autoBuilding); + workspace.setDescription(description); + } + protected boolean rootProjectWorthBeingRemoved() { if (this.report.size() == 1) { return false; diff --git a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java index 9c26d87df35..b15869f9f12 100644 --- a/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java +++ b/tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/datatransfer/SmartImportTests.java @@ -572,4 +572,20 @@ public void testSmartImportJobSkipsDotFoldersInProposals() throws Exception { org.eclipse.core.tests.harness.FileSystemHelper.clear(tempDir.toFile()); } } + + @Test + public void testAutoBuildingRestoredAfterFailedImport() throws Exception { + assertTrue("Test expects auto-building to be enabled", ResourcesPlugin.getWorkspace().isAutoBuilding()); + // a regular file as import root makes the project creation fail + java.nio.file.Path notADirectory = Files.createTempFile("smartImportFailure", ".txt"); + try { + SmartImportJob job = new SmartImportJob(notADirectory.toFile(), Collections.emptySet(), true, true); + IStatus status = job.run(new NullProgressMonitor()); + assertEquals("Import was expected to fail", IStatus.ERROR, status.getSeverity()); + assertTrue("Auto-building must be restored after a failed import", + ResourcesPlugin.getWorkspace().isAutoBuilding()); + } finally { + Files.deleteIfExists(notADirectory); + } + } }