CircleCI-Public / circleci-config-sdk-ts

Generate CircleCI Configuration YAML from JavaScript or TypeScript. Use Dynamic Configuration and the Config SDK together for live generative config.
https://circleci-public.github.io/circleci-config-sdk-ts/
Apache License 2.0
82 stars 29 forks source link

Bug: Seemingly impossible to get a single `\` to appear in generated command text. #187

Open joethompson14 opened 1 year ago

joethompson14 commented 1 year ago

Is there an existing issue for this?

Current behavior

Code Snippet

const BACKSLASH = `\\';
 new CircleCI.Job(
    jobName,
    new CircleCI.executors.DockerExecutor('cimg/python:3.11'),
    [
      BeanstalkOrb.setupCommand({}),
      new CircleCI.commands.Run({
        command: `
          set -ex
          cat${BACKSLASH}<<EOF>options.json
            [
              { "Namespace": "aws:autoscaling:asg", "OptionName": "MinSize",
                "Value": "${minInstances}" },
              { "Namespace": "aws:autoscaling:asg", "OptionName": "MaxSize",
                "Value": "${maxInstances}" }
            ]
          EOF`
      })
  ]);

When inside of a workflow will only ever generate an even number - 0, 2, 4, ... - of backslashes in the generated configuration - no matter the amount of backslashes included in BACKSLASH. This is making it impossible to generate the above command to execute during CI as it complains there is an unclosed << tag as that is a protected char/string by CircleCI.

Minimum reproduction code

See above.

Steps to reproduce

See above.

Expected behavior

It should be possible to get a single backslash to intentionally appear in generated text.

CircleCI Config SDK version

0.12.3

Node.js version

16.13.0

In which operating systems have you tested?

Other

No response

joethompson14 commented 1 year ago

This appears to be a limitation of the included yaml package - https://eemeli.org/yaml/#yaml. It seems to be impossible to get it to generate a single \ inside of a quoted string. I am also not having luck trying to force the processor to write the string as a block literal instead of a quoted string.

One solution could be to provide some sort of option to force escaping of << substrings for command literals.