Go R1 Day 61

progress

It was a dark and stormy night. The world was bleak. A command was sent to The Compiler.

The assumptions were flawed. The command was rejected.

You have given me an invalid command with -argument-that-shall-not-work 5 being invalid.

But I’ve provided --argument-that-shall-not-work, the indomitable wizard said.

Your command is unworthy.

Digging into esoteric tomes of knowledge, the wizard discovered others have had similar issues when calling external processes using the legendary wizardry of os/exec. However, none could shine light into the darkness of his failure.

Running in pwsh worked fine.

Next, the wizard tried a variety of escaping commands.

  • Using shellescape package.
  • Using back-ticks with the arguments to escape.
  • Using partially quoted arguments in the slice of the strings.
  • Using no quotes.
  • Went down the path of ancient texts describing similar issues.1

To the wizards dismay, copying the printed debug output worked fine in the terminal, but alas would not be executed by The Compiler.

It began to feel like the curse of dynamic SQL queries that had long plagued the wizard until PowerShell had been discovered.

The wizard ruminated on his plight. He thought:

At the end of the day, all things seem to come down to strings and the cursed interpretation of my textual commands to The Compiler. How many a day have I wasteth upon the fault of a single character. The root of all evil must be a string."

The wizard connected to a new remote instance, using the power of the Remote SSH plugin and began debugging in VSCode.

The debug breakpoint config that worked was set in stone.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
  {
      "name": "Run frustrating-test",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "program": "${workspaceFolder}/MyTestApp/test.go",
      "args": [
        "-count",
        "100",
        "-batch",
        "10",
        "-delay",
        "1",
        "-server",
        "MyCustomIPAddress",
        "-debug",
      ],
      "debugAdapter": "legacy" // newer version wouldn't work remote
    },

Consulting The Sage (aka Debugger), it advised the wizard of the message sent to The Compiler.

https://d33wubrfki0l68.cloudfront.net/96e108e4f4a5c838abf2a52bd3580c05b9949e0c/56b6d/images/2021-08-03-14.52.18-debug-variables.png
Debug Variables

The wizard had a revelation. A fellow wizard advised to break the appending into individual statements instead of trying to do so much in one step.

The incantation changed from:

1
command = append(command, fmt.Sprintf(`--header "user-id: %s"`, petname.Generate(petNameLength, "-"))

to the following:

1
2
3
command = append(command, "--header")
command = append(command, fmt.Sprintf(`user-id: %s`, petname.Generate(petNameLength, "-")))
command = append(command, "--max-retry-count", "5")

The foe vanquished, the The Blight of Strings was cast aside with malice. The wizard swore to never fall prey to this again.

Further Detail ( expand to read)

Josesh[^similar-issues] pointed towards: EscapeArgs. I did not find any equivalent for Darwin. The closest thing I could find was execveDarwin which I believe is the execution line, which gets the argument list from: SlicePtrFromStrings which is defined at here

I’ll have to re-examine in the future when I have more experience with Go, as it’s not a simple chain to follow.

1
2
3
4
[test --param 1]
strings.Join(a)... "test --param 1"
os.Command: ["test" "--param 1"]
echo "test --param 1\n"

This pointed towards a similar issue with the \n showing up.

 Playground

The Compiler’s heartless gaze felt nothing. In the shadows, The String Balrock bid its time, knowing that the wizard would suffer once again.

https://d33wubrfki0l68.cloudfront.net/d8f39551375b47c3277391e5bcf413c5bb46e6bd/33859/images/2021-08-03-goroutine.gif
It Works And Has Pterm Magic


  1. 2016 // Prevent Escaping exec.Command Arguments in Go | Joseph Spurrier ↩︎


Webmentions

Likes  (5)
  • The Developer Bot
  • Fun Bot
  • xael bot
  • HackerSpace Tech
  • Lj Miranda
Reposts  (2)
  • Very Proud Robot
  • Broni Nyarko