Has the split helper function been broken forever?
# 🌱|help-and-getting-started
f
Copy code
kind: Run
name: print-username
description: Print the local username
type: exec
spec:
  command: ["sh", "-c", "echo 'The local username is: ${command.params.env}'"]
The above action works well! Output:
Copy code
The local username is: jack.dev
Now this is broken:
Copy code
kind: Run
name: print-username
description: Print the local username
type: exec
spec:
  command: ["sh", "-c", "echo 'The local username is: ${split(command.params.env, '.')[0]}'"]
Copy code
Invalid template string (echo 'The local username is: ${split(command.params.env, '.')[0]}') at path spec.command.2: Unable to parse as valid template string.
I remember bringing this up a couple of years ago and was returning to the workaround I had in place to see if I could fix it.
Just running it like this seems to print the unsplit string:
Copy code
command: ['sh', '-c', "echo '${split(command.params.env, \",\")}'"]
Copy code
command: ['sh', '-c', "echo '${slice(split(command.params.env, \",\"), 0, 1)[0]}'"]
Also invalid
c
Hi @fresh-yak-35965 Thanks for reporitng this. We have a GH issue for this problem: https://github.com/garden-io/garden/issues/3001 Could you please try a workaround? If you can store the result of the
split
function call to a standalone variable (e,g,
result
), then you can use index-based access on that, it should work fine:
result[0]
. Please let us know if it works.
f
Sorry for the late reply. I went on vacation for a week. Thanks @curved-intern-91221 I will give this a try and let you know.
Alright, this sort of works.
Copy code
yaml
kind: Project
apiVersion: garden.io/v1
name: kraken
defaultEnvironment: dev
scan:
  exclude:
    - "node_modules/**/*"
variables:
  env: ${split(command.params.env, ".")}
environments:
  - name: dev
    production: false
    defaultNamespace: dev-${local.env.CHANGE_ID || local.username}
    variables:
      mode: ${local.env.MODE || ""}
      baseHostName: ${var.env[0]}.kraken.scalecomputing.dev
Unfortunately, I get a nonsense error about it failing to resolve project configuration (as in depicted), which isn't legitimate because it
deploy --sync
subsequently works find with the properly resolved variable. https://cdn.discordapp.com/attachments/1243341282236043294/1247908474751422588/image.png?ex=6661bcd5&is=66606b55&hm=224857a4df78bec676ff5c4eaa0699adef5afb3462d5093ec03c2eb6a403696b&
The ideal here would be for me to be able to pass an arbitrary flag. Like:
Copy code
garden deploy --prefix jack
Or something. Maybe I can define this in a custom command.
c
@fresh-yak-35965 I wasn't able to reproduce this. Here is the full config that I tried:
Copy code
kind: Project
apiVersion: garden.io/v1
name: kraken
defaultEnvironment: dev
scan:
  exclude:
    - "node_modules/**/*"
variables:
  env: ${split(command.params.env, ".")}
environments:
  - name: dev
    production: false
    defaultNamespace: dev-${local.env.CHANGE_ID || local.username}
    variables:
      mode: ${local.env.MODE || ""}
      baseHostName: ${var.env[0]}.kraken.scalecomputing.dev

---

kind: Run
name: print-all
description: Print original input and variables
type: exec
spec:
  command: ["sh", "-c", "echo 'The local username is: ${command.params.env};\nThe var.env is ${var.env};\nThe var.env[0] is ${var.env[0]};\nThe resolved value of enviromemnts.dev.variables.baseHostName is ${var.baseHostName}'"]
I executed
Copy code
garden run print-all --env jack.dev --force-refresh
and the output was:
Copy code
The local username is: jack.dev;
The var.env is jack,dev;
The var.env[0] is jack;
The resolved value of enviromemnts.dev.variables.baseHostName is jack.kraken.scalecomputing.dev
So, it worked well. Could you please share the full config of your example and the exact command that you tried?
What Garden version do you use?
Ah, I got it! 🙂 When I omit the
--env
option (i.ee. trying
garden run print-all
), it fails as you mentioned above
Copy code
Invalid template string (${split(command.params.env, ".")}) at path variables.env: Error validating argument 'string' for split helper function:

value must be a string
So, the error message is not that informative and user-friendly, it seems to be too low-level and technical. But the problem is that the template string resolution engine tries to resolve the non-existing value. We might need to improve that part by checking if the value is
undefined
or
null
before parsing it, and to print a meaninigful error. I'll create a ticket for that.
You can fix it by passing a value to
--env
option.
@fresh-yak-35965 I've created a GH issue https://github.com/garden-io/garden/issues/6150 - we'll consider it in the planning session next week.
f
@curved-intern-91221 Thanks for testing that. Just to clarify this did occur for me when passing the
--env
when using
garden dev
. So
Copy code
garden dev --env jack.dev
Is the specific command that acted as if I hadn't passed the env flag, but then the deploy worked recognizing the arguments.
c
Ok, thank you! We'll take a closer look. Can you please share a minimal reproducible example? I need the Deploy action config with its sync mode configuration.
Ah, I see. Reproduced it with my config from the example above.
@fresh-yak-35965 does the sync mode crashes when you see the error above? Or does it just print a confusing error message and wrk normally?
f
It seems for me to just produce the confusing error and proceed to work normally after that.