Skip to content

Commit

Permalink
fix: don't exit PC on reload with parsing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
F1bonacc1 committed Sep 28, 2024
1 parent 74f786b commit 33435d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions src/app/project_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,8 +917,9 @@ func (p *ProjectRunner) UpdateProject(project *types.Project) (map[string]string

func (p *ProjectRunner) ReloadProject() (map[string]string, error) {
opts := &loader.LoaderOptions{
FileNames: p.project.FileNames,
EnvFileNames: p.project.EnvFileNames,
FileNames: p.project.FileNames,
EnvFileNames: p.project.EnvFileNames,
IsInternalLoader: true,
}
opts.WithTuiDisabled(p.disableDotenv)
opts.WithTuiDisabled(p.isTuiOn)
Expand Down
22 changes: 14 additions & 8 deletions src/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ func Load(opts *LoaderOptions) (*types.Project, error) {
}

for _, file := range opts.FileNames {
p := loadProjectFromFile(file, opts.disableDotenv, opts.EnvFileNames)
p, err := loadProjectFromFile(file, opts)
if err != nil {
return nil, err
}
opts.projects = append(opts.projects, p)
}
mergedProject, err := merge(opts)
Expand Down Expand Up @@ -79,7 +82,7 @@ func admitProcesses(opts *LoaderOptions, p *types.Project) *types.Project {
return p
}

func loadProjectFromFile(inputFile string, disableDotEnv bool, envFileNames []string) *types.Project {
func loadProjectFromFile(inputFile string, opts *LoaderOptions) (*types.Project, error) {
yamlFile, err := os.ReadFile(inputFile)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand All @@ -88,9 +91,9 @@ func loadProjectFromFile(inputFile string, disableDotEnv bool, envFileNames []st
log.Fatal().Err(err).Msgf("Failed to read %s", inputFile)
}

if !disableDotEnv {
if !opts.disableDotenv {
// .env is optional we don't care if it errors
_ = godotenv.Load(envFileNames...)
_ = godotenv.Load(opts.EnvFileNames...)
}

const envEscaped = "##PC_ENV_ESCAPED##"
Expand All @@ -104,20 +107,23 @@ func loadProjectFromFile(inputFile string, disableDotEnv bool, envFileNames []st
}
err = yaml.Unmarshal([]byte(temp), project)
if err != nil {
if opts.IsInternalLoader {
return nil, err
}
log.Fatal().Err(err).Msgf("Failed to parse %s", inputFile)
}
if project.DisableEnvExpansion {
err = yaml.Unmarshal(yamlFile, project)
if err != nil {
if opts.IsInternalLoader {
return nil, err
}
log.Fatal().Err(err).Msgf("Failed to parse %s", inputFile)
}
}

if err != nil {
log.Fatal().Err(err).Msgf("Failed to validate %s", inputFile)
}
log.Info().Msgf("Loaded project from %s", inputFile)
return project
return project, nil
}

func findFiles(names []string, pwd string) []string {
Expand Down
15 changes: 8 additions & 7 deletions src/loader/loader_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
)

type LoaderOptions struct {
workingDir string
FileNames []string
EnvFileNames []string
projects []*types.Project
admitters []admitter.Admitter
disableDotenv bool
isTuiDisabled bool
workingDir string
FileNames []string
EnvFileNames []string
IsInternalLoader bool
projects []*types.Project
admitters []admitter.Admitter
disableDotenv bool
isTuiDisabled bool
}

func (o *LoaderOptions) AddAdmitter(adm ...admitter.Admitter) {
Expand Down

0 comments on commit 33435d8

Please sign in to comment.