Skip to content
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

CompilerConfiguration.targetDirectory is null in AST tranformation #575

Closed
ZacharyKlein opened this issue Apr 25, 2018 · 8 comments
Closed
Assignees

Comments

@ZacharyKlein
Copy link

ZacharyKlein commented Apr 25, 2018

I'm doing some work with an AST transformation, and it appears that when using the Groovy-Eclipse compiler, the targetDirectory property is not being set correctly when the transform is executed. The property is set correctly when the same transform is executed from a Gradle project. The sample project/s attached demonstrates the issue.

Sample project: https://github.com/ZacharyKlein/groovy-ast-demo
AST lib (JAR included in the above project): https://github.com/ZacharyKlein/groovy-ast-lib

@CompileStatic
@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
class GroovyAST implements ASTTransformation, CompilationUnitAware {
    CompilationUnit unit
    @Override
    void visit(ASTNode[] nodes, SourceUnit source) {
        println "targetDirectory: ${source.configuration.targetDirectory}" // null when executed by Groovy-Eclipse compiler
    }

    @Override
    void setCompilationUnit(CompilationUnit unit) {
        this.unit = unit
    }
}
@eric-milles
Copy link
Member

As long as I am in there, are there any other properties on CompilerConfiguration you'd expect to be set? Here is the list of available properties:
cc

And here is the current initialization method:

    public static CompilerConfiguration newCompilerConfiguration(CompilerOptions compilerOptions, ProblemReporter problemReporter) {
        CompilerConfiguration config = new CompilerConfiguration();

        if (compilerOptions.buildGroovyFiles > 1 && compilerOptions.groovyCompilerConfigScript != null) {
            Binding binding = new Binding();
            binding.setVariable("configuration", config);

            CompilerConfiguration configuratorConfig = new CompilerConfiguration();
            org.osgi.framework.Version v = GroovyUtils.getGroovyVersion();
            if ((v.getMajor() == 2 && v.getMinor() >= 1) || v.getMajor() > 2) {
                ImportCustomizer customizer = new ImportCustomizer();
                customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
                configuratorConfig.addCompilationCustomizers(customizer);
            }

            GroovyShell shell = new GroovyShell(binding, configuratorConfig);
            try {
                File configScript = new File(compilerOptions.groovyCompilerConfigScript);
                if (!configScript.isAbsolute() && compilerOptions.groovyProjectName != null) {
                    IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(compilerOptions.groovyProjectName);
                    configScript = new File(project.getLocation().append(configScript.getPath()).toOSString());
                }
                shell.evaluate(configScript);
            } catch (Exception e) {
                throw new RuntimeException("Failed to process Groovy config script: " + compilerOptions.groovyCompilerConfigScript, e);
            }
        }

        if ((compilerOptions.groovyFlags & CompilerUtils.InvokeDynamic) != 0) {
            config.getOptimizationOptions().put(CompilerConfiguration.INVOKEDYNAMIC, Boolean.TRUE);
        }

        return config;
    }

@eric-milles eric-milles self-assigned this Apr 28, 2018
@eric-milles
Copy link
Member

Also, you can set the target directory by setting a system property. This is an excerpt from CompilerConfiguration's default constructor.

            String target = System.getProperty("groovy.target.directory");
            if (target != null) {
                setTargetDirectory(target);
            }

@ZacharyKlein
Copy link
Author

@eric-milles interesting, we are actually using a custom system property to work around the issue.

As far as what other properties to set, I’m only really interested in target directory at the moment.

Thanks for looking into this!

@eric-milles
Copy link
Member

Do you need this property for building from Eclipse (UI) or Maven (CLI)? I have the Eclipse case figured out, but the CLI mode is a bit different.

@ZacharyKlein
Copy link
Author

@eric-milles I'm using a Maven build.

@eric-milles
Copy link
Member

Can you give this a try? groovy-eclipse-batch-2.4.15-SNAPSHOT.zip

@ZacharyKlein
Copy link
Author

@eric-milles Yep, this seems work! 👍

@eric-milles
Copy link
Member

Next release of the batch compiler will contain this fix. Hopefully the snapshot will be enough for you in the interim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants