gosig for VS Code
Change the signature of a Go function or method, and update every declaration
and call site that must change with it: the interfaces the function has to
satisfy, the other implementations of those interfaces, and every caller in
the workspace.
The extension is a front end for the gosig command line tool, which does all
the analysis and rewriting. The extension only collects the form, shows the
preview and writes the result.
Install
Installed from the Marketplace: nothing further to do. Each platform's
.vsix carries its own gosig binary, and the extension finds it
automatically. The gosig.path setting only needs to be set if you want to
point at a different binary, for example one you built yourself.
Building from source:
Either install the Go tool yourself:
go install ./cmd/gosig
go install puts the binary in $(go env GOPATH)/bin. Either add that
directory to PATH, or set the gosig.path setting to the full path of
the binary.
Or build the bundled binaries for all platforms with
editors/vscode/build.sh (see "Packaging", below); the extension then
finds the one for your platform on its own.
Build the extension:
cd editors/vscode
npm install
npm run compile
Run it. Open editors/vscode in VS Code and press F5; that opens a second
VS Code window with the extension loaded. To install it permanently
instead, package it with npx vsce package and install the resulting
.vsix from the Extensions view.
Packaging
./build.sh
Cross-compiles the gosig binary for each supported platform
(linux-x64, linux-arm64, darwin-x64, darwin-arm64, win32-x64) and
packages one .vsix per platform into dist/, each carrying only that
platform's binary. See ../../docs/publishing.md for the sizes measured,
what was checked about the executable bit, and the vsce publish commands
once a Marketplace account exists.
Use
Put the cursor on the name of a function or method in a .go file, then use
any of:
- Ctrl+F6, the default key binding, the same one GoLand uses. Change it in
the Keyboard Shortcuts editor under
gosig.changeSignature.
- Refactor… (Ctrl+Shift+R) → Change Signature (gosig). The action is
offered under the code action kind
refactor.rewrite.changeSignature,
beside gopls's own refactor.rewrite.moveParamLeft and moveParamRight.
- The editor's right-click menu, or Go: Change Signature (gosig) in the
command palette.
The Refactor menu offers the action anywhere in a Go file, not only on a
function name: deciding whether the cursor is on one means spawning gosig and
loading the whole package, and VS Code asks for code actions on every cursor
move. The command reports it plainly if the cursor is somewhere else.
A panel opens with the current signature:
- Edit the function name.
- Edit the name or type of any parameter or result.
- Move a row up or down to reorder it.
- Delete a row to remove that parameter or result.
- Add a row. A new parameter needs a default expression, which is what gets
inserted at every call site. A new result does not take one.
- Tick Group consecutive parameters of the same type to write
func f(a, b int) rather than func f(a int, b int).
Press Preview. The panel then lists every place the change would touch,
plus any warnings. Press Apply to write the files.
If gosig refuses the change, the panel shows the reasons and nothing is
written. A refusal is a normal answer, not a failure: it means the change
cannot be made safely, for example because a parameter you asked to remove is
still used in the body.
Saving and staleness
gosig plan reads the files from disk and returns byte offsets into them. The
offsets are only valid while those files stay exactly as they were, so:
- The command saves the active file before running
gosig describe. If it
cannot be saved, the command stops.
- If any file in the plan is edited after the preview, the panel drops the
plan and asks you to preview again.
- If a file somehow changes anyway,
gosig apply compares the SHA-256 of each
file against the digest recorded at plan time and refuses the whole request,
writing nothing. A half-applied refactor is worse than none.
Settings
| Setting |
Default |
Meaning |
gosig.path |
"gosig" |
Path to the gosig binary. An explicit, non-empty value always wins. Left at the default, the extension uses the binary bundled for your platform if there is one, then falls back to looking up the bare name gosig on PATH. |
If no binary can be found at all, the error notification offers a button
that opens this setting.
Layout
| File |
Contents |
src/protocol.ts |
The JSON request and response types, mirroring internal/protocol/protocol.go. Types only. |
src/gosig.ts |
Runs the binary with execFile, no shell, 30 second timeout. Parses the responses. Resolves which binary to run (setting, bundled, or PATH) and restores the bundled binary's executable bit at activation. Does not import the vscode API. |
src/form.ts |
The form state and the rules that turn it into a plan request. Does not import the vscode API. |
src/html.ts |
HTML escaping and the script nonce. |
src/panel.ts |
The webview panel: the form, the preview, and the apply step. |
src/extension.ts |
The command: cursor position, save, describe, open the panel. |
The webview runs under a Content-Security-Policy that admits one script by
nonce and nothing else. No data from a Go source file is interpolated into the
page's HTML: the form's contents travel over postMessage and are written
into the DOM as text, so a type or an identifier cannot become markup.
Tests
npm test
That compiles first, then runs the node:test suites in src/test/. They
cover the 0-based to 1-based position conversion, building a plan request from
a form that reorders, renames, adds and deletes fields, parsing both ok and
refused responses, the wrapper's handling of a missing binary and of exit
code 1, and which binary gets picked (explicit setting, bundled binary per
platform, or the bare name on PATH). None of them need the vscode API or the
real binary; the few that run a process use a stand-in script or file.
npm test needs Node 18 or newer (the engines field's minimum); running
vsce for packaging needs Node 20.18.1 or newer, which can be a different
Node than the one npm test runs under.