fix(cmd): Fixed various bugs in connect.go#393
Conversation
…t:port does not work and neither does SURGE_HOST
…n to clarify the priority order
… targets is broken
| func NewRemoteDownloadService(baseURL string, token string, opts HTTPClientOptions) (*RemoteDownloadService, error) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| client, err := NewHTTPClient(opts) | ||
| if err != nil { | ||
| cancel() | ||
| return nil, err | ||
| } | ||
| sseClient, err := NewStreamingHTTPClient(opts) | ||
| if err != nil { | ||
| cancel() | ||
| return nil, err | ||
| } | ||
| return &RemoteDownloadService{ | ||
| BaseURL: baseURL, | ||
| Token: token, | ||
| Client: &http.Client{Timeout: 30 * time.Second}, | ||
| SSEClient: &http.Client{}, | ||
| Client: client, | ||
| SSEClient: sseClient, | ||
| ctx: ctx, | ||
| cancel: cancel, | ||
| } | ||
| }, nil | ||
| } |
There was a problem hiding this comment.
Missing type and function definitions — code will not compile
NewHTTPClient, NewStreamingHTTPClient, and HTTPClientOptions are called/used in NewRemoteDownloadService but are not defined anywhere in the internal/core package or any other package in the repository (confirmed via exhaustive search). Similarly, in cmd/, currentRemoteClientConfig, newRemoteAPIHTTPClient, and newRemoteDownloadService are invoked from connect.go and utils.go without a corresponding definition. The variables globalInsecureHTTP, globalInsecureTLS, and globalTLSCAFile referenced in root.go are also undeclared.
The PR appears to be missing a file (e.g., cmd/remote_client.go and/or internal/core/http_client.go) that should contain these declarations. As submitted, the package will not compile.
Prompt To Fix With AI
This is a comment left during a code review.
Path: internal/core/remote_service.go
Line: 31-51
Comment:
**Missing type and function definitions — code will not compile**
`NewHTTPClient`, `NewStreamingHTTPClient`, and `HTTPClientOptions` are called/used in `NewRemoteDownloadService` but are not defined anywhere in the `internal/core` package or any other package in the repository (confirmed via exhaustive search). Similarly, in `cmd/`, `currentRemoteClientConfig`, `newRemoteAPIHTTPClient`, and `newRemoteDownloadService` are invoked from `connect.go` and `utils.go` without a corresponding definition. The variables `globalInsecureHTTP`, `globalInsecureTLS`, and `globalTLSCAFile` referenced in `root.go` are also undeclared.
The PR appears to be missing a file (e.g., `cmd/remote_client.go` and/or `internal/core/http_client.go`) that should contain these declarations. As submitted, the package will not compile.
How can I resolve this? If you propose a fix, please make it concise.
Binary Size Analysis✅ Size Decreased or Stable
|
Greptile Summary
This PR fixes several bugs in the remote-connect flow: the most impactful is that
StreamEventspreviously returned an emptyfunc(){}cleanup, causing the SSE goroutine to be unrecoverable after use. The PR also extracts TLS/CA configuration into a dedicatedhttp_client.go+remote_client.golayer, renamesresolveConnectBaseURL→parseConnectTarget(returning a struct for extensibility), adds IPv6 address support, and wires--insecure-http/--insecure-tls/--tls-ca-fileflags end-to-end. Tests are comprehensive and the newmergeContextshelper is clean.Confidence Score: 5/5
Safe to merge — all findings are P2 style suggestions with no blocking defects.
The primary bugs (no-op StreamEvents cleanup, missing TLS support, missing IPv6 handling) are correctly fixed with well-covered tests. The two remaining comments are minor style suggestions that do not affect correctness at the current call sites.
No files require special attention for merge safety.
Important Files Changed
resolveConnectBaseURL→parseConnectTargetreturning aconnectTargetstruct; added IPv6 and scheme-validation logic,isLocalHostfor auto-token resolution. Directservice.Client.Timeoutmutation during probe is a minor fragility.remoteClientConfigstruct; providesnewRemoteDownloadServiceandnewRemoteAPIHTTPClientfactory helpers.resolveHostTarget,resolveAPIConnection, and updateddoAPIRequestto use a TLS-aware client.doAPIRequestcreates a newhttp.Clientper call and useshttp.NewRequestwithout context.HTTPClientOptions,NewHTTPClient,NewStreamingHTTPClient,NewHTTPTransport, andnewTLSConfigwith custom CA/InsecureSkipVerifysupport. Implementation is clean and correct.StreamEventspreviously returnedfunc(){}as cleanup; now returns a real cancel viamergeContexts. AddedmergeContextshelper, removed duplicates.ctxchecks fromstreamWithReconnect.TestConnectCmd_HostSourcesBypassLocalAutodetectverifies--hostandSURGE_HOSTbypass local autodetection. Good integration coverage.mergeContextsbehaviour, and service shutdown. Good coverage of the new context-merging logic.Sequence Diagram
sequenceDiagram participant CLI as surge connect participant Utils as cmd/utils.go participant RC as cmd/remote_client.go participant Core as core/remote_service.go participant HTTP as core/http_client.go participant SSE as Remote SSE /events CLI->>Utils: resolveHostTarget() / parseConnectTarget() Utils->>RC: currentRemoteClientConfig() RC-->>Utils: remoteClientConfig (TLS opts) Utils->>RC: newRemoteDownloadService(baseURL, token) RC->>HTTP: NewHTTPClient(opts) RC->>HTTP: NewStreamingHTTPClient(opts) HTTP-->>RC: *http.Client (with TLS/CA) RC->>Core: NewRemoteDownloadService(baseURL, token, opts) Core-->>CLI: *RemoteDownloadService CLI->>Core: service.List() [probe with ConnectTimeout] Core-->>CLI: ok CLI->>Core: service.StreamEvents(streamCtx) Core->>Core: mergeContexts(s.ctx, streamCtx) Core-->>CLI: ch, cancelFn loop SSE reconnect loop Core->>SSE: GET /events (Bearer token) SSE-->>Core: event stream Core-->>CLI: ch <- decoded event end CLI->>Core: cancelFn() / service.Shutdown() Core->>Core: merged context cancelled, goroutine exits, ch closedComments Outside Diff (1)
internal/core/remote_service.go, line 248-258 (link)cancelcalled twice on normal stream exitStreamEventsreturnscancelas the cleanup function and callsdefer cancel()inside the goroutine. When the stream ends naturally (goroutine exits), it cancels the merged context; if the caller then calls the returned cleanup,cancelruns a second time.context.WithCancelcancels are idempotent and themergeContextsstop-funcs are also safe to call repeatedly, so this is not a correctness bug — but it can confuse readers. Consider returning a one-shot wrapper that no-ops after the first call, or document that the returned cleanup is idempotent.Prompt To Fix With AI
Prompt To Fix All With AI
Reviews (8): Last reviewed commit: "Merge branch 'main' into connect-fix" | Re-trigger Greptile