Groovy .with {...} seems to need @NonCPS

This issue is archived. You can view it, but you can't modify it. Learn more

XMLWordPrintable

      The following function demonstrates the problem:

      Object func1() {
          Object m = [:];
          m.with { x = 1 };
          return m;
      }
      assert func1() == [ : ]; // wrong, should be [x:1]
      

      This is supposed run the "x=1" in the context of m. However, func1() returns an empty map.

      If func is marked @NonCPS, it returns map [x:1].

      @NonCPS
      Object func2() {
          Object m = [:];
          m.with { x = 1 };
          return m;
      }
      assert func2() == [ x:1 ];
      

      Running these in a Jenkinsfile.  When run in the script console, the @NonCPS is not needed (behaves as expected).
      My expectation is that func1 should work without needing a @NonCPS annotation.

            Assignee:
            Unassigned
            Reporter:
            Justin Vallon
            Archiver:
            Jenkins Service Account

              Created:
              Updated:
              Archived: