-
Notifications
You must be signed in to change notification settings - Fork 883
cmd: Generate packages in parallel #2026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,12 @@ import ( | |
"io" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"runtime/trace" | ||
"strings" | ||
"sync" | ||
|
||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/kyleconroy/sqlc/internal/codegen/golang" | ||
"github.com/kyleconroy/sqlc/internal/codegen/json" | ||
|
@@ -159,71 +163,94 @@ func Generate(ctx context.Context, e Env, dir, filename string, stderr io.Writer | |
} | ||
} | ||
|
||
for _, sql := range pairs { | ||
combo := config.Combine(*conf, sql.SQL) | ||
if sql.Plugin != nil { | ||
combo.Codegen = *sql.Plugin | ||
} | ||
var m sync.Mutex | ||
grp, gctx := errgroup.WithContext(ctx) | ||
grp.SetLimit(runtime.GOMAXPROCS(0)) | ||
|
||
// TODO: This feels like a hack that will bite us later | ||
joined := make([]string, 0, len(sql.Schema)) | ||
for _, s := range sql.Schema { | ||
joined = append(joined, filepath.Join(dir, s)) | ||
} | ||
sql.Schema = joined | ||
stderrs := make([]bytes.Buffer, len(pairs)) | ||
|
||
joined = make([]string, 0, len(sql.Queries)) | ||
for _, q := range sql.Queries { | ||
joined = append(joined, filepath.Join(dir, q)) | ||
} | ||
sql.Queries = joined | ||
for i, pair := range pairs { | ||
sql := pair | ||
errout := &stderrs[i] | ||
|
||
var name, lang string | ||
parseOpts := opts.Parser{ | ||
Debug: debug.Debug, | ||
} | ||
grp.Go(func() error { | ||
combo := config.Combine(*conf, sql.SQL) | ||
if sql.Plugin != nil { | ||
combo.Codegen = *sql.Plugin | ||
} | ||
|
||
switch { | ||
case sql.Gen.Go != nil: | ||
name = combo.Go.Package | ||
lang = "golang" | ||
// TODO: This feels like a hack that will bite us later | ||
joined := make([]string, 0, len(sql.Schema)) | ||
for _, s := range sql.Schema { | ||
joined = append(joined, filepath.Join(dir, s)) | ||
} | ||
sql.Schema = joined | ||
|
||
case sql.Plugin != nil: | ||
lang = fmt.Sprintf("process:%s", sql.Plugin.Plugin) | ||
name = sql.Plugin.Plugin | ||
} | ||
joined = make([]string, 0, len(sql.Queries)) | ||
for _, q := range sql.Queries { | ||
joined = append(joined, filepath.Join(dir, q)) | ||
} | ||
sql.Queries = joined | ||
|
||
packageRegion := trace.StartRegion(ctx, "package") | ||
trace.Logf(ctx, "", "name=%s dir=%s plugin=%s", name, dir, lang) | ||
var name, lang string | ||
parseOpts := opts.Parser{ | ||
Debug: debug.Debug, | ||
} | ||
|
||
result, failed := parse(ctx, name, dir, sql.SQL, combo, parseOpts, stderr) | ||
if failed { | ||
packageRegion.End() | ||
errored = true | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If parsing failed, we'd return early. Instead, return errors from every package. |
||
} | ||
switch { | ||
case sql.Gen.Go != nil: | ||
name = combo.Go.Package | ||
lang = "golang" | ||
|
||
out, resp, err := codegen(ctx, combo, sql, result) | ||
if err != nil { | ||
fmt.Fprintf(stderr, "# package %s\n", name) | ||
fmt.Fprintf(stderr, "error generating code: %s\n", err) | ||
errored = true | ||
packageRegion.End() | ||
continue | ||
} | ||
case sql.Plugin != nil: | ||
lang = fmt.Sprintf("process:%s", sql.Plugin.Plugin) | ||
name = sql.Plugin.Plugin | ||
} | ||
|
||
files := map[string]string{} | ||
for _, file := range resp.Files { | ||
files[file.Name] = string(file.Contents) | ||
} | ||
for n, source := range files { | ||
filename := filepath.Join(dir, out, n) | ||
output[filename] = source | ||
} | ||
packageRegion.End() | ||
} | ||
packageRegion := trace.StartRegion(gctx, "package") | ||
trace.Logf(gctx, "", "name=%s dir=%s plugin=%s", name, dir, lang) | ||
|
||
result, failed := parse(gctx, name, dir, sql.SQL, combo, parseOpts, errout) | ||
if failed { | ||
packageRegion.End() | ||
errored = true | ||
return nil | ||
} | ||
|
||
out, resp, err := codegen(gctx, combo, sql, result) | ||
if err != nil { | ||
fmt.Fprintf(errout, "# package %s\n", name) | ||
fmt.Fprintf(errout, "error generating code: %s\n", err) | ||
errored = true | ||
packageRegion.End() | ||
return nil | ||
} | ||
|
||
files := map[string]string{} | ||
for _, file := range resp.Files { | ||
files[file.Name] = string(file.Contents) | ||
} | ||
|
||
m.Lock() | ||
for n, source := range files { | ||
filename := filepath.Join(dir, out, n) | ||
output[filename] = source | ||
} | ||
m.Unlock() | ||
Comment on lines
+234
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Writing to a map isn't threadsafe, so put this in mutex. In the future might just use |
||
|
||
packageRegion.End() | ||
return nil | ||
}) | ||
} | ||
if err := grp.Wait(); err != nil { | ||
return nil, err | ||
} | ||
if errored { | ||
for i, _ := range stderrs { | ||
if _, err := io.Copy(stderr, &stderrs[i]); err != nil { | ||
return nil, err | ||
} | ||
} | ||
return nil, fmt.Errorf("errored") | ||
} | ||
return output, nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we moved to
RunE
, we accidentally added some additional error output. Turning that off for now.