Songkeys

Songkeys

Hello. Songkeys here~ :D
twitter

A Mysterious Case Solved - After I typed `??` in the Terminal, the Golang CLI popped up

This article also has a GPT-translated Chinese version. 这篇文章同时提供 GPT 翻译的中文版本,可以在此阅读

Not an April Fool's joke!

If you have installed Golang, you can now try opening your Terminal and typing  ??. You will find that it magically transforms into the Golang CLI.

image

Yes! It is the go command! Guaranteed to be genuine!

image

Why???

Beginning of the Story: GitHub Copilot CLI#

A month ago, I received an invitation to use GitHub Copilot CLI, which was in technical preview. It uses GPT to help us translate natural language to shell commands.

image

As we can see in the demo GIF above, we just need to type ?? <instruction> to ask GPT to write down the expected command for us.

Pretty cool.

So I installed it and came across this section in its documentation about how to set up the ?? alias for the sake of convenience:

...we recommend you install the ??git?, and gh? commands in your shell... To install them, run the following command:

eval "$(github-copilot-cli alias -- "$0")"

OK.

I was about to do this, but I just wanted to make sure the ?? is safe to alias. So I typed it and pressed enter:

➜ ~ ??

Go is a tool for managing Go source code.

Usage:

	go <command> [arguments]
...

Wait a minute....

Why is the ?? command linked to go?

Searching and Asking#

So I asked in the discord group:

image

But unfortunately, no one knows the answer.

Could it be that I misconfigured my .zshrc file? After checking for a while, the answer is no. My configuration looks good.

Then I searched across the Golang Repo, but couldn't find anything about the ?? alias. And no issues were posted.

Next, I tried to google it. But it became hard to describe the case. It looked like the "??" keyword query can not even be prioritized by the search engine. The search result is almost irrelevant.

image

It looks like I was the first one coming across this issue on the whole Internet!

...

I gave up. And I typed the eval "$(github-copilot-cli alias -- "$0")" to alias my ?? to the github-copilot-cli so that things looks good as long as we ignore the weird part. The only thing that was still not perfect was that which ?? still gives me the location of the go binary. That's because the function command initialization only ran on every start of sessions. This is even more confusing.

image

More People Joined the Mystery#

In the following month, many more people have got access to the GitHub Copilot. And some of them has encountered the same issue as mine.

image image

I'm glad that at least I'm not alone.

The Answer Was Found#

Finally, today (a month later) the case was solved by someone from the GitHub Next team:

image

The ?? command is being expanded by the shell to match any two-character binary entry in the current working directory.

What does that even mean?

He did point out the general mechanism, but made one mistake - it's matching filenames and directories instead of binaries. To clarify, it means when we use ??, we are looking for filenames or directories in the current folder that consist of exactly two characters.

No matter how many ? we type, the same rule applies!

image

In the example above, echo ?????? lists us all the possible directories or filenames with the length of 6 characters; cd ????? brings us to the only directory with the length of 5 characters.

That is:

  • ?????? -> GitHub Movies Public
  • ????? -> Music

So, in our case, the ?? in the home directory resolves to the only directory go with the length of 2 characters.

  • ?? -> go

If we are in another directory without the go directory in it, it won't work:

image

Wait.... One More Question#

Didn't we just say "directory or filename"? Why does it bring out the go binary instead!?

Yes! I know, right? The expected behavior should be entering the go directory (for that in bash <dirname> is equivalent to cd <dirname>).

This is because the global binary file location resolving has a higher priority than the local filename or directories. For example:

image

Here we make a file named go then type the go command, it won't execute the local go file but will execute the global go binary. We need to specifically type the relative path to the local file to execute it:

image

When it comes to directory, it has a similar behavior, but directory allows us to cd without the explict cd command.

So, if there is a file or directory named go in the current directory, and when we type ??, it will expand to this go filename or directory name, then try to execute it.

We can achieve the same behavior with npm:

image

Even More Interesting#

When we know that the ??.. command executes the expanded filenames or directories, something interesting can be done.

For example, echo some words:

image

Even better, why not execute some npm scripts?

image

Just ensure that every word of the script should be in the same length and the initial letter of each word should follow the alphabet order.

Alright. I hope you enjoy this April Fool's Article. Now, change the "wow" script to rm -rf / and send it to your best friend, tell they to execute ???. Enjoy! (Just kidding! Don't do this!)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.