{
"title": "A Mysterious Case Solved - After I typed `??` in the Terminal, the Golang CLI popped up",
"tags": [
"post"
],
"summary": "The two questions in terminal. (No pun intended.)",
"sources": [
"xlog"
],
"external_urls": [
"https://song.xlog.app/qq"
],
"date_published": "2023-04-01T07:00:00.000Z",
"content": "> This article also has a GPT-translated Chinese version. 这篇文章同时提供 GPT 翻译的中文版本,可以[在此阅读](https://song.xlog.app/qq-zh)。\n\n\n\nNot an April Fool's joke!\n\nIf you have installed Golang, you can now try opening your Terminal and typing `??`. You will find that it magically transforms into the Golang CLI.\n\n<img src=\"ipfs://bafkreibxalxn3mnannqqse4l23yk4jnojfwq6d4oybamg5dtpeu2bvfkku\" title=\"??-to-go.png\" width=\"100%\" />\n\nYes! It is the `go` command! Guaranteed to be genuine!\n\n<img src=\"ipfs://bafkreid6qeapawyznbi5ftwwgj4isian7v64wy5x3yuzjbqo5jqjncuvgq\" title=\"??-is-go.png\" width=\"100%\" />\n\nWhy???\n\n## Beginning of the Story: GitHub Copilot CLI\n\nA 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.\n\n<img src=\"https://camo.githubusercontent.com/6d433cccdbee34edeb204574e3ee65467b323e51f2eaa559114d7adf7033522e/68747470733a2f2f6769746875626e6578742e636f6d2f6173736574732f70726f6a656374732f636f70696c6f742d636c692f776861742d7468652d7368656c6c2e737667\" title=\"??-example.gif\" width=\"100%\" />\n\nAs 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.\n\nPretty cool.\n\nSo I installed it and came across this section in its [documentation](https://www.npmjs.com/package/@githubnext/github-copilot-cli) about how to set up the `??` alias for the sake of convenience:\n\n> ...we recommend you install the `??`, `git?`, and `gh?` commands in your shell... To install them, run the following command:\n> \n> ```bash\n> eval \"$(github-copilot-cli alias -- \"$0\")\"\n> ```\n\nOK.\n\nI was about to do this, but I just wanted to make sure the `??` is safe to alias. So I typed it and pressed enter:\n\n```\n➜ ~ ??\n\nGo is a tool for managing Go source code.\n\nUsage:\n\n\tgo <command> [arguments]\n...\n```\n\nWait a minute....\n\nWhy is the `??` command linked to `go`?\n\n## Searching and Asking\n\nSo I asked in the discord group:\n\n<img src=\"ipfs://bafkreieiuvo6lluvhenkzzra4qvceswytkamwa42b5ltponzuycgmsggoq\" title=\"??-q-in-discord.png\" width=\"100%\" />\n\nBut unfortunately, no one knows the answer.\n\nCould it be that I misconfigured my `.zshrc` file? After checking for a while, the answer is no. My configuration looks good.\n\nThen I searched across the [Golang Repo](https://github.com/search?q=repo%3Agolang%2Fgo+%3F%3F&type=code), but couldn't find anything about the `??` alias. And no issues were posted.\n\nNext, 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.\n\n<img src=\"ipfs://bafybeiddbv3o3hbh3rbr3ch6e4cadezrroh4ld2cq23zuj5s6idygqafay\" title=\"??-google.png\" width=\"100%\" />\n\nIt looks like I was the first one coming across this issue on the whole Internet!\n\n...\n\nI 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.\n\n<img src=\"ipfs://bafkreiayu4skityy4r5qwgd2a7bouzhowrcuidfxz7mq6vrwdfwj4ubvki\" title=\"??-mis-alias.png\" width=\"100%\" />\n\n## More People Joined the Mystery\n\nIn the following month, many more people have got access to the GitHub Copilot. And some of them has encountered the same issue as mine.\n\n<img src=\"ipfs://bafkreie65uy6mwi2n5yffcqmkwth3eh4hk7qiwvwo343x3eeftsgdzu444\" title=\"??-they-had-the-same-issue-1.png\" width=\"100%\" />\n\n<img src=\"ipfs://bafkreib4yxycu4jwqjx7bsvnlkf6t5t22vlintfnsrp3hlwk7j25ryzrlu\" title=\"??-they-had-the-same-issue-2.png\" width=\"100%\" />\n\nI'm glad that at least I'm not alone.\n\n## The Answer Was Found\n\nFinally, today (a month later) the case was solved by someone from the GitHub Next team:\n\n<img src=\"ipfs://bafybeigw6f5pciaewoimzssgu7xbn6phdzoowjaztcekfaghtahb5nscie\" title=\"??-case-solved.png\" width=\"100%\" />\n\n> The `??` command is being expanded by the shell to match any two-character binary entry in the current working directory.\n\nWhat does that even mean?\n\nHe 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.**\n\nNo matter how many `?` we type, the same rule applies!\n\n<img src=\"ipfs://bafkreibgfv6mikiuib7triosa6nrnuen3skyrfgs5h4grf7gg2sazwkqfy\" title=\"??-showcase.png\" width=\"100%\" />\n\nIn 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.\n\nThat is:\n\n- `??????` -> `GitHub Movies Public`\n- `?????` -> `Music`\n\nSo, in our case, the `??` in the home directory resolves to the only directory `go` with the length of 2 characters.\n\n- `??` -> `go`\n\nIf we are in another directory without the `go` directory in it, it won't work:\n\n<img src=\"ipfs://bafkreibr7mhhkhgdjlcptsfzspuflrmcsquy6wzc7jdux4q5qu7k66euby\" title=\"??-in-another-directory.png\" width=\"100%\" />\n\n## Wait.... One More Question\n\nDidn't we just say \"directory or filename\"? **Why does it bring out the `go` binary instead**!?\n\nYes! I know, right? The expected behavior should be entering the `go` directory (for that in bash `<dirname>` is equivalent to `cd <dirname>`).\n\nThis is because the global binary file location resolving has a higher priority than the local filename or directories. For example:\n\n<img src=\"ipfs://bafkreigpjzdtmbs7ehavmgwf7e2paa3vqidn3ptyptuxc7omttx5h3dlrq\" title=\"??-touch-go-then-execute.png\" width=\"100%\" />\n\nHere 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:\n\n<img src=\"ipfs://bafkreihm4aalmffyaqkziuz2hgnjezffwewyzzp4qkw7iza6rzjgblh7sy\" title=\"??-local-go.png\" width=\"100%\" />\n\nWhen it comes to directory, it has a similar behavior, but directory allows us to `cd` without the explict `cd` command.\n\nSo, 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**.\n\nWe can achieve the same behavior with `npm`:\n\n<img src=\"ipfs://bafkreidmci2v7veydm2ar5xmvc7s7vb2azidbskxzxypg5dlz7rgsyussm\" title=\"??-npm.png\" width=\"100%\" />\n\n## Even More Interesting\n\nWhen we know that the `??..` command **executes** the expanded filenames or directories, something interesting can be done.\n\nFor example, `echo` some words:\n\n<img src=\"ipfs://bafkreifkbcpqcspny4jztryiibjlfw2rbxc4eqxsinc5dxzxlsy626cvqu\" title=\"??-echo.png\" width=\"100%\" />\n\nEven better, why not execute some `npm` scripts?\n\n<img src=\"ipfs://bafkreiapisjtsappwnz24ztsvfzoyhpijmx5t27q6bryc3ba7mai6ityxe\" title=\"??-npm-run.png\" width=\"100%\" />\n\n> 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.\n\nAlright. 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!)",
"attributes": [
{
"value": "qq",
"trait_type": "xlog_slug"
}
]
}