garden icon indicating copy to clipboard operation
garden copied to clipboard

Foreach statement not working in generateFiles in module templates

Open carolinapc opened this issue 3 years ago • 2 comments

Bug

Foreach statement not working

Current Behavior

Sample

kind: ModuleTemplate
name: my-template
inputsSchemaPath: rest.schema.json
modules:
  - type: exec
    name: ${parent.name}-module
    description: common to all REST services
    local: true
    variables:
      filesToGenerate:
        pulumi: 
          sourcePath: ../pulumi/Pulumi.yaml
          targetPath: ${inputs.deploymentFolder}/pulumi/Pulumi.yaml
          resolveTemplates: true
          generate: true
        global:
          sourcePath: ../pulumi/config/global.yaml
          targetPath: ${inputs.deploymentFolder}/config/global.yaml
          resolveTemplates: true
          generate: true
        defaultIndex:
          sourcePath: ../pulumi/index.ts
          targetPath: ${inputs.deploymentFolder}/pulumi/default-index.ts
          resolveTemplates: true
          generate: ${!inputs.customEntryPoint}

    generateFiles: 
      $forEach: ${var.filesToGenerate}
      $filter: ${item.value.generate}
      $return: 
        sourcePath: ${item.value.sourcePath}
        targetPath: ${item.value.targetPath}
        resolveTemplates: ${item.value.resolveTemplates}

Expected behavior

Files be copied as per the condition in the filter

Reproducible example

Error being thrown:

[2021-12-10T17:35:31.694Z] TypeError: undefined is not a function
    at Object.<anonymous> (/snapshot/project/tmp/pkg/cli/node_modules/@garden-io/core/build/src/config/module-template.js:0)
    at Generator.next (<anonymous>)
    at /snapshot/project/tmp/pkg/cli/node_modules/@garden-io/core/build/src/config/module-template.js:0
    at new Promise (<anonymous>)
    at /snapshot/project/tmp/pkg/cli/node_modules/@garden-io/core/build/src/config/module-template.js:0
    at /snapshot/project/tmp/pkg/cli/node_modules/@garden-io/core/build/src/config/module-template.js:0
    at tryCatcher (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/util.js:16:23)
    at MappingPromiseArray._promiseFulfilled (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/map.js:68:38)
    at MappingPromiseArray.<anonymous> (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/promise_array.js:115:31)
    at MappingPromiseArray.init (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/promise_array.js:79:10)
    at MappingPromiseArray._asyncInit (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/map.js:37:10)
    at _drainQueueStep (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/async.js:97:12)
    at _drainQueue (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/async.js:86:9)
    at Async._drainQueues (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/async.js:102:5)
    at Immediate.<anonymous> (/snapshot/project/tmp/pkg/cli/node_modules/bluebird/js/release/async.js:15:14)
    at processImmediate (internal/timers.js:456:21)
    at process.topLevelDomainCallback (domain.js:137:15)

Workaround

Suggested solution(s)

Additional context

Your environment

  • OS: Windows
  • How I'm running Kubernetes: GKE

garden version 0.12.32-edge-72937979

carolinapc avatar Dec 10 '21 21:12 carolinapc

I can confirm the issue. Basically it is specific to the generateFiles field in this context, so I've updated the description accordingly. It's not as quick a fix as I'd hoped, but we'll try and address it soon.

edvald avatar Jan 12 '22 15:01 edvald

It seems a similar issue happens to build.dependencies (of a exec module inside a ModuleTemplate) I tried to concatenate the dependencies from the varfile with the ${parent.name}-version like this:

build:
      dependencies: 
      - "${parent.name}-version"
      - $concat: ${var.dependencies}

but I'm getting errors like this: Error validating Module 'common-lib-parent-module' (common-lib): key .build.dependencies[1][name] must be a string

Also var.dependencies could be an empty array [] , so I tried this just to check if that could be the cause:

build:
      dependencies: 
      - "${parent.name}-version"
      - $concat: ${var.dependencies} || ["${parent.name}-version"]

no success

carolinapc avatar Mar 17 '22 22:03 carolinapc

We deprecated the generateFiles field, so closing this.

10ko avatar Jan 31 '24 10:01 10ko