MCPcopy Index your code
hub / github.com/dagger/dagger / innerEnvFile

Method innerEnvFile

core/modulesource.go:907–976  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

905}
906
907func (src *ModuleSource) innerEnvFile(ctx context.Context) (*EnvFile, string, error) {
908 // We only allow loading an env file from local modules, for safety
909 if src.Kind != ModuleSourceKindLocal {
910 return nil, "", nil
911 }
912
913 dag, err := CurrentDagqlServer(ctx)
914 if err != nil {
915 return nil, "", err
916 }
917
918 // FIXME: .env must be at the root of the module directory
919 // If the user calls dagger from a subdirectory of the module, and that subdirectory contains a more
920 // specialized .env, that will be ignored. To fix this, we need access to current workdir on the host,
921 // so that we can findup from there.
922 moduleDirPath := path.Join(
923 src.Local.ContextDirectoryPath, // path of the module's git root, on the host
924 src.SourceRootSubpath, // path of the module directory, relative to its git root
925 )
926 // Check if the env file exists
927 var envFileExists bool
928 if err := dag.Select(ctx, dag.Root(), &envFileExists,
929 dagql.Selector{Field: "host"},
930 dagql.Selector{
931 Field: "directory",
932 Args: []dagql.NamedInput{
933 {Name: "path", Value: dagql.String(moduleDirPath)},
934 {Name: "include", Value: dagql.ArrayInput[dagql.String]{".env"}},
935 },
936 },
937 dagql.Selector{
938 Field: "exists",
939 Args: []dagql.NamedInput{
940 {Name: "path", Value: dagql.String(".env")},
941 {Name: "expectedType", Value: dagql.Opt(ExistsTypeRegular)},
942 },
943 },
944 ); status.Code(err) == codes.NotFound {
945 // It's possible that the module directory *doesn't exist yet*
946 // (ie. we are called from `dagger init ./FOO` and `FOO` will be populated after we return)
947 // Therefore: if parent directory doesn't exist, just return "no result" without error
948 return nil, "", nil
949 } else if err != nil {
950 return nil, "", fmt.Errorf("failed to check for inner env file in %s: %w",
951 moduleDirPath, err)
952 }
953 if !envFileExists {
954 return nil, "", nil
955 }
956 envFilePath := path.Join(moduleDirPath, ".env")
957 var envFile *EnvFile
958 if err := dag.Select(ctx, dag.Root(), &envFile,
959 dagql.Selector{Field: "host"},
960 dagql.Selector{
961 Field: "file",
962 Args: []dagql.NamedInput{
963 {Name: "path", Value: dagql.String(envFilePath)},
964 },

Callers 1

LoadUserDefaultsMethod · 0.95

Calls 7

StringTypeAlias · 0.92
OptFunction · 0.92
NewBooleanFunction · 0.92
CurrentDagqlServerFunction · 0.70
SelectMethod · 0.65
RootMethod · 0.45
CodeMethod · 0.45

Tested by

no test coverage detected