esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Support for context.Context

Open pjebs opened this issue 1 year ago • 4 comments

It would be good if the API can support ctx context.Context to cancel builds.

pjebs avatar Jan 31 '25 10:01 pjebs

The API for canceling a build is here: https://esbuild.github.io/api/#cancel

evanw avatar Jan 31 '25 17:01 evanw

@evanw

As per your code in your docs: https://esbuild.github.io/api/#cancel

  ctx, err := api.Context(api.BuildOptions{...})
  if err != nil { <------------------------------Doesn't work in the way of idiomatic Go 
    return err
  }

Maybe api.Context should return a standard untyped error instead of *ContextError (that implements error)?

The workaround doesn't look nice:

ctx, err := api.Context(api.BuildOptions{...})
if err.(*api.ContextError) != nil {

}

pjebs avatar Jan 31 '25 21:01 pjebs

Sorry, I'm confused. Your first example with err != nil already compiles and works correctly, and your second "workaround" example with err.(*api.ContextError) != nil doesn't compile or work. This is the compile error:

invalid operation: err (variable of type *api.ContextError) is not an interface

evanw avatar Feb 06 '25 19:02 evanw

See: https://github.com/romance-dev/go-bundler/blob/main/bundler.go#L139 (unlicensed for the time being)

pjebs avatar Feb 06 '25 21:02 pjebs