MCPcopy
hub / github.com/dagger/dagger / runGeneratedContext

Method runGeneratedContext

core/schema/modulesource.go:2656–2715  ·  view source on GitHub ↗

runGeneratedContext runs codegen, client generation, and dagger.json writing for the given module source, returning the original context directory and the fully-generated context directory. Callers can use these two directories to produce either a diff or a Changeset.

(
	ctx context.Context,
	srcInst dagql.ObjectResult[*core.ModuleSource],
)

Source from the content-addressed store, hash-verified

2654// module source, returning the original context directory and the fully-generated context
2655// directory. Callers can use these two directories to produce either a diff or a Changeset.
2656func (s *moduleSourceSchema) runGeneratedContext(
2657 ctx context.Context,
2658 srcInst dagql.ObjectResult[*core.ModuleSource],
2659) (originalCtxDir dagql.ObjectResult[*core.Directory], genDirInst dagql.ObjectResult[*core.Directory], _ error) {
2660 dag, err := core.CurrentDagqlServer(ctx)
2661 if err != nil {
2662 return originalCtxDir, genDirInst, fmt.Errorf("failed to get dag server: %w", err)
2663 }
2664
2665 modCfg, err := s.loadModuleSourceConfig(srcInst.Self())
2666 if err != nil {
2667 return originalCtxDir, genDirInst, fmt.Errorf("failed to load module source config: %w", err)
2668 }
2669
2670 originalCtxDir = srcInst.Self().ContextDirectory
2671
2672 // run codegen too if we have a name and SDK
2673 genDirInst = originalCtxDir
2674 if modCfg.Name != "" && modCfg.SDK != nil && modCfg.SDK.Source != "" {
2675 updatedGenDirInst, err := s.runCodegen(ctx, srcInst)
2676 var missingImplErr ErrSDKCodegenNotImplemented
2677 if err != nil && !errors.As(err, &missingImplErr) {
2678 return originalCtxDir, genDirInst, fmt.Errorf("failed to run codegen: %w", err)
2679 }
2680 if err == nil {
2681 genDirInst = updatedGenDirInst
2682 }
2683 }
2684
2685 // Generate clients
2686 for _, client := range modCfg.Clients {
2687 genDirInst, err = s.runClientGenerator(ctx, srcInst, genDirInst, client)
2688 if err != nil {
2689 return originalCtxDir, genDirInst, fmt.Errorf("failed to generate client %s: %w", client.Generator, err)
2690 }
2691 }
2692
2693 // write dagger.json to the generated context directory
2694 modCfgBytes, err := json.MarshalIndent(modCfg, "", " ")
2695 if err != nil {
2696 return originalCtxDir, genDirInst, fmt.Errorf("failed to encode module config: %w", err)
2697 }
2698 modCfgBytes = append(modCfgBytes, '\n')
2699 modCfgPath := filepath.Join(srcInst.Self().SourceRootSubpath, modules.Filename)
2700 err = dag.Select(ctx, genDirInst, &genDirInst,
2701 dagql.Selector{
2702 Field: "withNewFile",
2703 Args: []dagql.NamedInput{
2704 {Name: "path", Value: dagql.String(modCfgPath)},
2705 {Name: "contents", Value: dagql.String(modCfgBytes)},
2706 {Name: "permissions", Value: dagql.Int(0o644)},
2707 },
2708 },
2709 )
2710 if err != nil {
2711 return originalCtxDir, genDirInst, fmt.Errorf("failed to add updated dagger.json to context dir: %w", err)
2712 }
2713

Calls 8

runCodegenMethod · 0.95
runClientGeneratorMethod · 0.95
CurrentDagqlServerFunction · 0.92
StringTypeAlias · 0.92
IntTypeAlias · 0.92
SelfMethod · 0.80
SelectMethod · 0.65

Tested by

no test coverage detected