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

Allow Pipeline functions with float/double parameters

    • Icon: Bug Bug
    • Resolution: Not A Defect
    • Icon: Minor Minor
    • workflow-cps-plugin
    • None
    • Jenkins 2.46.3, pipeline 2.5, pipeline.groovy 2.35

      For this pipeline:

      node {
        checkout scm
        utils = load 'pipelines/utils.groovy
        stage("myStage") {
          utils.myFunction(0.5)
      }
      }

      And the contents of utils.groovy:

      def myFunction(float x) {
        // do stuff
      }

      I'm seeing the following error:

      java.lang.NoSuchMethodError: No such DSL method 'myFunction' found among steps [archive, bat, build, catchError, checkout, deleteDir, dir, dockerFingerprintFrom, dockerFingerprintRun, echo, envVarsForTool, error, fileExists, getContext, git, githubNotify, hipchatSend, input, isUnix, jiraComment, jiraIssueSelector, jiraSearch, library, libraryResource, load, mail, milestone, node, parallel, properties, pwd, readFile, readTrusted, resolveScm, retry, script, setGitHubPullRequestStatus, sh, sleep, sshagent, stage, stash, step, svn, timeout, timestamps, tool, unarchive, unstash, validateDeclarativePipeline, waitUntil, withContext, withCredentials, withDockerContainer, withDockerRegistry, withDockerServer, withEnv, wrap, writeFile, ws] or symbols [all, allOf, always, ant, antFromApache, antOutcome, antTarget, any, anyOf, apiToken, architecture, archiveArtifacts, artifactManager, batchFile, booleanParam, branch, buildButton, buildDiscarder, caseInsensitive, caseSensitive, choice, choiceParam, clock, cloud, command, cron, crumb, defaultView, demand, disableConcurrentBuilds, docker, dockerfile, downloadSettings, downstream, dumb, envVars, environment, expression, file, fileParam, filePath, fingerprint, frameOptions, freeStyle, freeStyleJob, git, github, githubPush, hyperlink, hyperlinkToModels, installSource, jdk, jdkInstaller, jgit, jgitapache, jnlp, jobName, junit, label, lastDuration, lastFailure, lastGrantedAuthorities, lastStable, lastSuccess, legacy, legacySCM, list, local, location, logRotator, loggedInUsersCanDoAnything, masterBuild, maven, maven3Mojos, mavenErrors, mavenMojos, mavenWarnings, modernSCM, myView, node, nodeProperties, nonStoredPasswordParam, none, not, overrideIndexTriggers, paneStatus, parameters, password, pattern, pipeline-model, pipelineTriggers, plainText, plugin, pollSCM, projectNamingStrategy, proxy, queueItemAuthenticator, quietPeriod, remotingCLI, run, runParam, schedule, scmRetryCount, search, security, shell, skipDefaultCheckout, skipStagesAfterUnstable, slave, stackTrace, standard, status, string, stringParam, swapSpace, text, textParam, tmpSpace, toolLocation, unsecured, upstream, usernameColonPassword, usernamePassword, viewsTabBar, weather, zfs, zip] or globals [credentials, currentBuild, docker, env, manager, params, pipeline, scm]

      This bug was particularly frustrating for me because error message did not give me any hints what the issue was. I worked around the issue by treating the float as a String. I also observed the same issue with types double and Double.

          [JENKINS-45949] Allow Pipeline functions with float/double parameters

          Andrew Bayer added a comment -

          Need to investigate further, but I think this would be an issue in workflow-cps dispatching.

          Andrew Bayer added a comment - Need to investigate further, but I think this would be an issue in workflow-cps dispatching.

          Andrew Bayer added a comment -

          First investigation results: with my test case of test.groovy being

          def func(float x) {
            return x * x
          }
          

          and my Pipeline being

          node {
            writeFile text: (the code above), file: 'test.groovy'
            def loaded = load 'test.groovy'
            echo "result: ${loaded.func(0.5)}"
          }
          

          I got the same error you did. Switching to loaded.func(0.5f), though, fixed it. Why? Because 0.5 is actually a java.math.BigDecimal, while 0.5f is a java.lang.Float. Theoretically, changing to func(Number x) would do the trick regardless of whether it was a Float, a Double, an Integer, a BigDecimal, etc, but there is a caveat there in that BigDecimal methods like multiply aren't whitelisted by default, but you could easily do something like float f = x.floatValue() in your method and then you're good to go.

          Andrew Bayer added a comment - First investigation results: with my test case of test.groovy being def func( float x) { return x * x } and my Pipeline being node { writeFile text: (the code above), file: 'test.groovy' def loaded = load 'test.groovy' echo "result: ${loaded.func(0.5)}" } I got the same error you did. Switching to loaded.func(0.5f) , though, fixed it. Why? Because 0.5 is actually a java.math.BigDecimal , while 0.5f is a java.lang.Float . Theoretically, changing to func(Number x) would do the trick regardless of whether it was a Float , a Double , an Integer , a BigDecimal , etc, but there is a caveat there in that BigDecimal methods like multiply aren't whitelisted by default, but you could easily do something like float f = x.floatValue() in your method and then you're good to go.

          Sean McIntyre added a comment -

          Thanks abayer for the follow-up.

          Sean McIntyre added a comment - Thanks abayer for the follow-up.

            abayer Andrew Bayer
            seanwp Sean McIntyre
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: