{"record":{"id":"0f8384b8a946d975","repo":"microsoft/aspire","slug":"failed-to-connect-to-apphost-w","errorCode":null,"errorMessage":"failed to connect to AppHost: %w","messagePattern":"failed to connect to AppHost: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"src/Aspire.Hosting.CodeGeneration.Go/Resources/transport.go","lineNumber":626,"sourceCode":"\t\t<-token.ctx.Done()\n\t\tc.cancelToken(id)\n\t}()\n\treturn id\n}\n\n// connect establishes the connection to the AppHost server and starts the\n// background reader and writer goroutines.\nfunc (c *client) connect(ctx context.Context, timeout time.Duration) error {\n\tc.mu.Lock()\n\tif c.conn != nil {\n\t\tc.mu.Unlock()\n\t\treturn nil\n\t}\n\n\trawConn, err := openConnection(c.socketPath, timeout)\n\tif err != nil {\n\t\tc.mu.Unlock()\n\t\treturn fmt.Errorf(\"failed to connect to AppHost: %w\", err)\n\t}\n\n\tauthToken := os.Getenv(\"ASPIRE_REMOTE_APPHOST_TOKEN\")\n\tif authToken == \"\" {\n\t\tcErr := rawConn.Close()\n\t\tc.mu.Unlock()\n\t\treturn errors.Join(errors.New(\"ASPIRE_REMOTE_APPHOST_TOKEN environment variable is not set\"), cErr)\n\t}\n\n\tconn := newConnection(rawConn, c, c.onConnectionClose)\n\tc.conn = conn\n\tc.mu.Unlock()\n\n\tconn.start()\n\n\tif err := c.authenticate(ctx, authToken); err != nil {\n\t\tc.disconnect()\n\t\treturn fmt.Errorf(\"failed to authenticate to AppHost: %w\", err)","sourceCodeStart":608,"sourceCodeEnd":644,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.CodeGeneration.Go/Resources/transport.go#L608-L644","documentation":"CreateBuilder → connect attempts a socket connection to the Aspire AppHost (openConnection on c.socketPath). When the socket connection cannot be established, the underlying error is wrapped as \"failed to connect to AppHost: %w\". This is the Go-codegen equivalent of \"AppHost not running or unreachable\".","triggerScenarios":"Running the generated app with `aspire run` missing (no AppHost process listening on the unix/named-pipe socket), wrong ASPIRE socket path env var, socket file removed, or connection timeout expiring because the AppHost is slow to start.","commonSituations":"Running the Go project directly (go run / IDE) instead of through the AppHost; AppHost crashed; permission denied on the socket file; Docker/WSL path mismatch; stale socket from a previous run.","solutions":["Start the app through `aspire run` (or start the AppHost) so the socket exists before connecting.","Check the ASPIRE socket path environment variable is set and points to the live socket; print the wrapped err for details (permission refused vs no such file vs timeout).","If the AppHost is slow, increase the connection timeout.","Delete stale socket files from previous crashed runs and retry."],"exampleFix":"// before\n// go run ./api  ->  failed to connect to AppHost: dial unix /tmp/aspire.sock: connect: no such file or directory\n// after\nif os.Getenv(\"ASPIRE_APPHOST_ENDPOINT\") == \"\" && os.Getenv(\"ASPIRE_REMOTE_APPHOST_TOKEN\") == \"\" {\n    log.Fatal(\"not launched by AppHost; use `aspire run` instead of running this project directly\")\n}","handlingStrategy":"try-catch","validationCode":"sock := os.Getenv(\"ASPIRE_APPHOST_ENDPOINT\") // or the socket path var your runtime uses\nif sock == \"\" {\n    log.Fatal(\"not launched via `aspire run`; AppHost socket unavailable\")\n}\nif _, err := os.Stat(sock); err != nil {\n    log.Fatalf(\"AppHost socket %q not present: %v\", sock, err)\n}","typeGuard":null,"tryCatchPattern":"app, err := aspire.CreateBuilder(ctx)\nif err != nil {\n    log.Fatalf(\"cannot reach AppHost (is `aspire run` active?): %v\", err)\n}","preventionTips":["Always run the project via `aspire run`, not go run or IDE launch configs.","Retry with backoff if the AppHost may still be starting.","Check socket file existence/permissions before connecting.","Remove stale sockets from crashed runs."],"tags":["go","aspire","network","socket","apphost"],"backgroundTag":"connection-refused","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}