Skip to content

Commit e056381

Browse files
committed
Update the comments that describe how the plugin host and the plugin communicate.
1 parent 95e3f75 commit e056381

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

Sources/PackagePlugin/Plugin.swift

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,40 @@
1414
#endif
1515

1616
//
17-
// This source file contains the main entry point for all plugins. It decodes
18-
// input from SwiftPM, determines which protocol function to call, and finally
19-
// encodes output for SwiftPM.
17+
// This source file contains the main entry point for all kinds of plugins.
18+
// A plugin receives messages from the "plugin host" (either SwiftPM or some
19+
// IDE that uses libSwiftPM), and sends back messages in return based on its
20+
// actions and events. A plugin can also request services from the host.
2021
//
21-
// The specifics of how SwiftPM communicates with the plugin are implementation
22-
// details, but the way it currently works is that the plugin is compiled as an
23-
// executable and then run in a sandbox that blocks network access and prevents
24-
// changes to all except a few file system locations.
22+
// Exactly how the plugin host invokes a plugin is an implementation detail,
23+
// but the current approach is to compile the Swift source files that make up
24+
// the plugin into an executable for the host platform, and to then invoke the
25+
// executable in a sandbox that blocks network access and prevents changes to
26+
// all except for a few specific file system locations.
27+
//
28+
// The host process and the plugin communicate using messages in the form of
29+
// length-prefixed JSON-encoded Swift enums. The host sends messages to the
30+
// plugin through its standard-input pipe, and receives messages through the
31+
// plugin's standard-output pipe. All output received through the plugin's
32+
// standard-error pipe is considered to be free-form textual console output.
2533
//
26-
// The "plugin host" (SwiftPM or an IDE using libSwiftPM) sends a JSON-encoded
27-
// context struct to the plugin process on its original standard-input pipe, and
28-
// when finished, the plugin sends a JSON-encoded result struct back to the host
29-
// on its original standard-output pipe. The plugin host treats output on the
30-
// standard-error pipe as free-form output text from the plugin (for debugging
31-
// purposes, etc).
32-
3334
// Within the plugin process, `stdout` is redirected to `stderr` so that print
3435
// statements from the plugin are treated as plain-text output, and `stdin` is
35-
// closed so that attemps by the plugin logic to read from console input return
36-
// errors instead of blocking. The original `stdin` and `stdout` are duplicated
37-
// for use as messaging pipes, and are not directly used by the plugin logic.
36+
// closed so that any attemps by the plugin logic to read from console result
37+
// in errors instead of blocking the process. The original `stdin` and `stdout`
38+
// are duplicated for use as messaging pipes, and are not directly used by the
39+
// plugin logic.
3840
//
39-
// Using the standard input and output streams avoids having to make allowances
40-
// in the sandbox for other channels of communication, and seems a more portable
41-
// approach than many of the alternatives.
42-
//
43-
// The exit code of the plugin process determines whether the plugin invocation
41+
// The exit code of the plugin process indicates whether the plugin invocation
4442
// is considered successful. A failure result should also be accompanied by an
4543
// emitted error diagnostic, so that errors are understandable by the user.
4644
//
45+
// Using standard input and output streams for messaging avoids having to make
46+
// allowances in the sandbox for other channels of communication, and seems a
47+
// more portable approach than many of the alternatives. This is all somewhat
48+
// temporary in any case — in the long term, something like distributed actors
49+
// or something similar can hopefully replace the custom messaging.
50+
//
4751

4852
extension Plugin {
4953

0 commit comments

Comments
 (0)