( ctx context.Context, srcInst dagql.ObjectResult[*core.ModuleSource], )
| 2365 | } |
| 2366 | |
| 2367 | func (s *moduleSourceSchema) runCodegen( |
| 2368 | ctx context.Context, |
| 2369 | srcInst dagql.ObjectResult[*core.ModuleSource], |
| 2370 | ) (res dagql.ObjectResult[*core.Directory], _ error) { |
| 2371 | dag, err := core.CurrentDagqlServer(ctx) |
| 2372 | if err != nil { |
| 2373 | return res, fmt.Errorf("failed to get current dag: %w", err) |
| 2374 | } |
| 2375 | |
| 2376 | // load the deps as actual Modules |
| 2377 | deps, err := s.loadDependencyModules(ctx, srcInst, srcInst) |
| 2378 | if err != nil { |
| 2379 | return res, fmt.Errorf("failed to load dependencies as modules: %w", err) |
| 2380 | } |
| 2381 | |
| 2382 | generatedCodeImpl, ok := srcInst.Self().SDKImpl.AsCodeGenerator() |
| 2383 | if !ok { |
| 2384 | return res, ErrSDKCodegenNotImplemented{SDK: srcInst.Self().SDK.Source} |
| 2385 | } |
| 2386 | |
| 2387 | // If possible, add the types defined by the module itself to the "deps" so that they can be |
| 2388 | // part of the code generation. |
| 2389 | // This is not really a dependency as it's the module itself, but that will allow to generate |
| 2390 | // the types. |
| 2391 | // |
| 2392 | // This is gated on the explicit SELF_CALLS flag rather than SelfCallsEnabled: |
| 2393 | // it transforms the source into a module (eagerly type-checking it) purely to |
| 2394 | // feed code generation, so it only applies to SDKs that opt in for codegen. |
| 2395 | // SDKs that always enable self calls only for their runtime (e.g. Dang, whose |
| 2396 | // codegen is a no-op) must not pay this cost — doing so would fail when the |
| 2397 | // source is uninitialized or references not-yet-installed dependencies. |
| 2398 | if srcInst.Self().SDK != nil { |
| 2399 | // Only if the SDK implements a specific function to get module type definitions. |
| 2400 | // If not, we will have circular dependency issues. |
| 2401 | if _, ok := srcInst.Self().SDKImpl.AsModuleTypes(); ok && |
| 2402 | srcInst.Self().SDK.ExperimentalFeatureEnabled(core.ModuleSourceExperimentalFeatureSelfCalls) { |
| 2403 | var mod dagql.ObjectResult[*core.Module] |
| 2404 | err = dag.Select(ctx, srcInst, &mod, dagql.Selector{ |
| 2405 | Field: "asModule", |
| 2406 | }) |
| 2407 | if err != nil { |
| 2408 | return res, fmt.Errorf("failed to transform module source into module: %w", err) |
| 2409 | } |
| 2410 | |
| 2411 | deps = mod.Self().Deps.Append(core.NewUserMod(mod)) |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | // run codegen to get the generated context directory |
| 2416 | generatedCode, err := generatedCodeImpl.Codegen(ctx, deps, srcInst) |
| 2417 | if err != nil { |
| 2418 | return res, fmt.Errorf("failed to generate code: %w", err) |
| 2419 | } |
| 2420 | genDirInst := generatedCode.Code |
| 2421 | |
| 2422 | // update .gitattributes in the generated context directory |
| 2423 | // (linter thinks this chunk of code is too similar to the below, but not clear abstraction is worth it) |
| 2424 | //nolint:dupl |
no test coverage detected