4

I have created a plug-in to hook into the save action and create a minified javascript file of an edited javascript file. You can see the full code in this question: eclipse plugin does not work after update to juno (eclipse 4)

Problem is that since Juno this plug-in creates endless loops in the workspace building process. It first starts to minify a file I did not change at all. This file creates an endless loop in the build. When it finished minifing the file it starts a new workspace build and minifies the file again and so on. But this gets even worse after some time, especially on a new eclipse start. Suddenly there are a dozen of files it minifies I have never touched. If I uninstall my plugin, then let eclipse build the workspace, reinstall my plug-in it works again. But after a while this starts all over.

I think it is related to the way I handle the job to create the file, see below. Maybe something has changed here with Juno? But I fail to find any information about that.

Job compileJob = new Job("Compile .min.js") {
 public IStatus run(IProgressMonitor monitor) {
        public IStatus run(IProgressMonitor monitor) {
            byte[] bytes = null;
            try {
                bytes = CallCompiler.compile(fullLocation.toString(), CallCompiler.SIMPLE_OPTIMIZATION).getBytes();

                InputStream source = new ByteArrayInputStream(bytes);
                if (!newFile.exists()) {
                    newFile.create(source, IResource.NONE, null);
                } else {
                    newFile.setContents(source, IResource.NONE, null);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (CoreException e) {
                e.printStackTrace();
            }
            return Status.OK_STATUS;
        }
};
compileJob.setRule(newFile.getProject());
compileJob.schedule();
Community
  • 1
  • 1
steros
  • 1,794
  • 2
  • 26
  • 60
  • It works fine if I deactivate "Project -> Build Automatically". Manually building the project does not generate this error. It seems only to occur when the workspace is automatically build. – steros Apr 17 '13 at 10:37
  • I'm guessing it's a typo that you have a nested run() method declaration. But more importantly, where is newFile located? if it is not in an output folder, then any change to the file will trigger an incremental build. If you don't want newFile triggering a build, then set it to be derived. – Andrew Eisenberg Apr 17 '13 at 20:33
  • Yes I think that is a typo. NewFile is located in the workspace just in the same folder the saved file is in. That a build is triggered is fine. But sometimes it results in a loop of building over and over again. What do you mean to set it to "derived"? How would I do that? – steros Apr 18 '13 at 21:18
  • See my answer below... – Andrew Eisenberg Apr 18 '13 at 22:03
  • I might be close to an answer. It seems that I have installed a plugin to eclipse named "JavaScript Development Tools". PDT, which I have installed too, also has an option to add JavaScript support for a project. So maybe that conflicts itself, probably those two are just the same things only once included in PDT. If I uninstall "JDT" it still does not work, but Validators ie. are not removed. I now setup a blank eclipse installation with only PDT and my plugin and a first test leaves me optimistic... – steros Apr 22 '13 at 17:12

1 Answers1

2

You need to set newFile to derived. A derived file is one that is created implicitly by the workspace during a build and it should be wiped away during a clean (since it can be recovered during the next build).

You can call the setDerived method on IResource:

org.eclipse.core.resources.IResource.setDerived(boolean, IProgressMonitor)

or when you create the file, it can be created as derived, though a call like this:

newFile.create(stream, IResource.DERIVED, monitor);

But, you cannot set the DERIVED flag through setContents, you must explicitly call setDerived(true) in that case.

From the docs:

A derived resource is a regular file or folder that is created in the course of translating, compiling, copying, or otherwise processing other files. Derived resources are not original data, and can be recreated from other resources. It is commonplace to exclude derived resources from version and configuration management because they would otherwise clutter the team repository with version of these ever-changing files as each user regenerates them.

Andrew Eisenberg
  • 28,387
  • 9
  • 92
  • 148