groovy
  1. groovy
  2. GROOVY-4521

Inconsistency between CompilerConfiguration, groovy -c and groovysh ?

    Details

    • Number of attachments :
      0

      Description

      Given the following script, saved as enc_test.groovy

      import org.codehaus.groovy.control.CompilerConfiguration
      println "File source encoding is probably set to " + CompilerConfiguration.DEFAULT.getSourceEncoding()
      

      I would assume all three executions below to print "UTF-8", but they don't:

      $ groovy -c UTF-8 enc_test.groovy
      File source encoding is probably set to MacRoman
      
      $ groovy -Dgroovy.source.encoding=UTF-8 enc_test.groovy
      File source encoding is probably set to MacRoman
      
      $ groovysh -Dgroovy.source.encoding=UTF-8
      groovy:000> org.codehaus.groovy.control.CompilerConfiguration.DEFAULT.getSourceEncoding()
      ===> UTF-8
      

      The reason I was printing this info was to track down an encoding issue I had; turns out the -c flag actually works for me (i.e my sources are properly decoded) (while the -Dgroovy.source.encoding option doesn't), but I was looking for a way to ensure my scripts are executed with -c UTF-8, but the above snippet obviously doesn't help.

        Activity

        Hide
        Roshan Dawrani added a comment -

        The consistency between groovy and groovysh is fixed now.

        Source encoding on CompilerConfiguration.DEFAULT is manipulatable using the system property groovy.source.encoding and it now works with both groovy/groovysh as shown below.

        $groovy -Dgroovy.source.encoding=UTF-8 enc_test.groovy
        File source encoding is probably set to UTF-8
        
        $groovysh -Dgroovy.source.encoding=UTF-8
        groovy:000> org.codehaus.groovy.control.CompilerConfiguration.DEFAULT.getSourceEncoding()
        ===> UTF-8
        

        "groovy -c " allows you to override the CompilerConfiguration that is made-up from system properties. So, it also works correctly (as you already noticed), but it won't modify the source encoding on CompilerConfiguration.DEFAULT configuration.

        Show
        Roshan Dawrani added a comment - The consistency between groovy and groovysh is fixed now. Source encoding on CompilerConfiguration.DEFAULT is manipulatable using the system property groovy.source.encoding and it now works with both groovy/groovysh as shown below. $groovy -Dgroovy.source.encoding=UTF-8 enc_test.groovy File source encoding is probably set to UTF-8 $groovysh -Dgroovy.source.encoding=UTF-8 groovy:000> org.codehaus.groovy.control.CompilerConfiguration.DEFAULT.getSourceEncoding() ===> UTF-8 "groovy -c " allows you to override the CompilerConfiguration that is made-up from system properties. So, it also works correctly (as you already noticed), but it won't modify the source encoding on CompilerConfiguration.DEFAULT configuration.
        Hide
        Grégory Joseph added a comment -

        Sounds good, thanks!

        Now is there a safe way to get the current source encoding at runtime? Can I access the CompilerConfiguration instance that was used to compile/execute my script ?

        Show
        Grégory Joseph added a comment - Sounds good, thanks! Now is there a safe way to get the current source encoding at runtime? Can I access the CompilerConfiguration instance that was used to compile/execute my script ?
        Hide
        Roshan Dawrani added a comment -

        No, as far as I know, that particular CompilerConfiguration instance is not exposed.

        Show
        Roshan Dawrani added a comment - No, as far as I know, that particular CompilerConfiguration instance is not exposed.

          People

          • Assignee:
            Roshan Dawrani
            Reporter:
            Grégory Joseph
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: