Skip to content
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

Improves project file detection #220

Merged
merged 6 commits into from
Feb 20, 2023

Conversation

DavidSeptimus-Klotho
Copy link
Contributor

This PR supports #105

  • Renamed "package" to "project" in ReadDir()to move away from NodeJS terminology
  • Replaced fixed project file names with predicate matching + a description for logging
  • Implemented .csproj file detection for C#
  • Implemented error handling for handling in the event multiple project files of the same language are detected in the same directory

Standard checks

  • Unit tests: Any special considerations?
  • Docs: Do we need to update any docs, internal or public?
  • Backwards compatibility: Will this break existing apps? If so, what would be the extra work required to keep them working?

- Renamed "package" to "project" to move away from NodeJS terminology
- Replaced fixed project file names with predicate matching + a description for logging
- Implemented .csproj file detection for C#
f, err = addFile(fsys, path, relPath, lang.projectFileOpener)
lang.foundProjectFile = true
isProjectFile = true
projectDirs[lang.name] = append(projectDirs[lang.name], prjDir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this also enabling the ability to have multiple languages in the same klotho compilation? Or what is projectDirs alluding to?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

projectDirs refers to directories in which we've detected project files for each supported language. For the moment it's used to enable language-specific file/directory exclusions, and to ensure that compilation fails if two project files exist for the same language in the same directory.

Support for multiple languages in a single compilation is not specifically handled here. The executable plugins don't currently support multiple languages in a single execution unit.

@jhsinger-klotho
Copy link
Contributor

just some lint errors as well

Copy link
Contributor

@gordon-klotho gordon-klotho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linter caught some things that need to be fixed.

}
}

func hasSuffix(expected string) predicate.Predicate[string] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this should probably be hasExtension and use the filepath.Ext(name) to test. In this case it's close to functionally the same, but I think it makes the intention clearer. There's a few edge cases that it'd catch too (like the file who's name is .csproj)


case "bin", "obj":
dir, _ := filepath.Split(info.Name())
for _, projectDir := range projectDirs[CSharp] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is susceptible to iteration order problems. What if the csproj was found after the bin/obj directories?

Copy link
Contributor Author

@DavidSeptimus-Klotho DavidSeptimus-Klotho Feb 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. I got the wrong impression regarding iteration order since these directories are lowercase and the .csproj file I was testing had an uppercase name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative might be to instead just look at the peers of bin/obj and see if there's a *.csproj file. So you don't need to read it beforehand.

lang.foundPackageFile = true
isPackageFile = true
if lang.projectFilePredicate != nil && lang.projectFilePredicate(info.Name()) {
prjDir, _ := filepath.Split(info.Name())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prjDir, _ := filepath.Split(info.Name())
prjDir := filepath.Dir(info.Name())

func hasExtension(expected string) predicate.Predicate[string] {
extension := "." + expected
return func(name string) bool {
return extension != name && strings.HasSuffix(name, extension)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return extension != name && strings.HasSuffix(name, extension)
return filepath.Ext(name) == extension

}

func hasExtension(expected string) predicate.Predicate[string] {
extension := "." + expected
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO require the caller to include the . which matches prior art with filepath.Ext

}
yamlLang := &languageFiles{name: Yaml}
dockerfileLang := &languageFiles{name: DockerFile}
allLangs := []*languageFiles{jsLang, pyLang, goLang, yamlLang}
allLangs := []*languageFiles{jsLang, pyLang, goLang, yamlLang, csLang}
projectDirs := map[languageName][]string{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's projectDirs used for? Seems like it's only used to skip folders. Seems like a lot of complication compared to just processing them normally. At least for now seems like premature optimization unless there's something you've already run into (in which case, can you document those cases in the comments?).


case "bin", "obj":
dir, _ := filepath.Split(info.Name())
for _, projectDir := range projectDirs[CSharp] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative might be to instead just look at the peers of bin/obj and see if there's a *.csproj file. So you don't need to read it beforehand.

pkg/input/readdir.go Show resolved Hide resolved
Comment on lines 334 to 337
projectFile, err = func() (core.File, error) {
defer f.Close()
return lang.projectFileOpener(prjFilePath, f)
}()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really idiomatic for Go. Something more like

projectFile, err := lang.projectFileOpener(prjFilePath, f)
f.Close()
if err != nil {
// ...
}

@github-actions
Copy link

Package Line Rate Health
github.com/klothoplatform/klotho/pkg/analytics 2%
github.com/klothoplatform/klotho/pkg/annotation 23%
github.com/klothoplatform/klotho/pkg/cli 4%
github.com/klothoplatform/klotho/pkg/core 21%
github.com/klothoplatform/klotho/pkg/env_var 82%
github.com/klothoplatform/klotho/pkg/exec_unit 54%
github.com/klothoplatform/klotho/pkg/infra/kubernetes 59%
github.com/klothoplatform/klotho/pkg/infra/kubernetes/helm 39%
github.com/klothoplatform/klotho/pkg/input 72%
github.com/klothoplatform/klotho/pkg/lang 38%
github.com/klothoplatform/klotho/pkg/lang/dockerfile 0%
github.com/klothoplatform/klotho/pkg/lang/golang 33%
github.com/klothoplatform/klotho/pkg/lang/javascript 48%
github.com/klothoplatform/klotho/pkg/lang/python 61%
github.com/klothoplatform/klotho/pkg/lang/yaml 0%
github.com/klothoplatform/klotho/pkg/logging 6%
github.com/klothoplatform/klotho/pkg/multierr 95%
github.com/klothoplatform/klotho/pkg/provider/aws 60%
github.com/klothoplatform/klotho/pkg/runtime 78%
github.com/klothoplatform/klotho/pkg/static_unit 33%
github.com/klothoplatform/klotho/pkg/validation 73%
github.com/klothoplatform/klotho/pkg/yaml_util 79%
Summary 42% (4035 / 9716)

@DavidSeptimus-Klotho DavidSeptimus-Klotho merged commit 8bab35f into main Feb 20, 2023
@DavidSeptimus-Klotho DavidSeptimus-Klotho deleted the dynamic-project-file-detection branch February 20, 2023 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants