cylc-flow icon indicating copy to clipboard operation
cylc-flow copied to clipboard

Variables defined in environment cannot have trailing spaces

Open ColemanTom opened this issue 2 years ago • 3 comments

Describe the bug

If you define a variable like follows, the result in the job script is _="". I would expect it to be _=" ".

[environment]
    _ = " "

I posted this in the Cylc discourse

NOTE: Depending on what you are doing, you could work around it by defining _ = " \0", but that isn't ok in all situations (it was in mine).

Release version(s) and/or repository branch(es) affected?

7.8.4

Steps to reproduce the bug

Create a suite and add _ = " " to the environment section for any task.

Expected behavior

Resultant job script to have _=" "

Screenshots

N/A

Additional context

Not a common need, but it should be allowed.

ColemanTom avatar Aug 17 '22 06:08 ColemanTom

This whitespace stripping is a parsec feature (the configuration file parser Cylc uses), I believe it is applied to all settings.

I don't think we can change this as a large number of workflows are relying on this behaviour to strip arbitrary whitespace from environment variables.

This is also used to allow environment variable definitions (which may be sub-shells including line breaks) to be written over multiple lines e.g:

[environment]
    FOO = """
        bar
    """

oliver-sanders avatar Aug 17 '22 07:08 oliver-sanders

It would only be if the variable is enclosed in quotes, like in my example that I think it would make sense. In my view, if you have trailing spaces and have intentionally put double or single quotes around it, it must be intentional.

ColemanTom avatar Aug 17 '22 07:08 ColemanTom

I don't think we can change this as a large number of workflows are relying on this behaviour to strip arbitrary whitespace from environment variables.

Presumably you mean this sort of thing (using "_" to mean whitespace):

DRINK =___beer________

where the value should end up as beer (no spaces).

I think it's not so difficult because we can distinguish quoted/unquoted single-line values, and multi-line (triple-quoted) values. The space inside quoted values is actually not stripped until the final validation stage. I'll put up a small PR for consideration...

I'll post a small PR for consideration.

hjoliver avatar Aug 18 '22 03:08 hjoliver

TLDR; The current pattern of stripping leading/trailing whitespace from env vars goes back a long way, we now have a substantial body of workflows which have grown into this pattern, for several of them, changing tack now would result in breaking changes.

https://github.com/cylc/cylc-flow/pull/5085#issuecomment-1637809624

Removing the bug label as this is an intentional design choice (will try do do a better job of documenting this).

The workarounds are:

  • Use escape characters where possible.
  • Refactor script logic.
  • Set the environment variable within a *script setting.

Leaving this issue open for now, should close going forward unless there is further demand for this functionality.

oliver-sanders avatar Jul 17 '23 10:07 oliver-sanders