Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-59674

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • workflow-cps-plugin
    • Jenkins 2.198
      workflow-cps ("Pipeline: Groovy") 2.74
      Debian 9
      OpenJDK 11.0.4

      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.

            Unassigned Unassigned
            vallon Justin Vallon
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: