-
Notifications
You must be signed in to change notification settings - Fork 72
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
feat: add support for pkl files #164
base: main
Are you sure you want to change the base?
Conversation
@kehoecj @ccoVeille The CI is failing due to this issue with We could address this by adding a step in the workflow to install |
I'm not sure I want to introduce a binary dependency for the validator for a single config type - especially because our release system relies on downloading statically-linked binaries or running |
I agree. Is there another pkl validation only in Go? |
But wait, do you need the code generation? As documented it's the only use case when the binary is required https://pkl-lang.org/go/current/quickstart.html#1-install-dependencies |
Agreed |
There are 2 binaries:
|
Then pkl format is a pain This project could help https://github.com/Drafteame/pkl-linter But for now, it only supports CLI. But we could test the tool and ask developer (or help him) to provide a library |
@olunusib @ccoVeille Trying to think of a way to keep this work without negatively impacting the installation experience for users who have no need for PKL and don't have the binary installed. What if PKL was opt-in only (at least for now) where if go can't find the pkl executable on the path then it won't try to lint it? If it finds a For the Github Action and AUR/Scoop/WinGet packages I can add PKL as a dependency so that shouldn't be a problem for that type of distribution. What do you think? |
I like your suggestion |
@olunusib What do you think? Would you want to take a shot at making PKL optional as described above? |
Sounds like a good plan @kehoecj. I'll find some time this weekend to look into it |
@kehoecj @ccoVeille this should be ready for review. I'd like to see the workflow run at least. |
// Save the original function before mocking and restore it after the test | ||
originalIsPklBinaryPresent := isPklBinaryPresent | ||
isPklBinaryPresent = func() bool { | ||
return false | ||
} | ||
defer func() { isPklBinaryPresent = originalIsPklBinaryPresent }() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm afraid this could lead to data race, when other tests are running
Using a lock mechanism to solve this would be over complicated as it would be applied even with the CLI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests within the same package run sequentially, so there shouldn't be a risk of a data race here. Lmk if that's not the case.
If we decide to make these parallel later, I think adding locking wouldn't be a bad idea. Can you explain what you mean by "it would be applied even with the CLI"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The locking I was suggesting would have been a mutex.Read/Write + defer unlock
to use it in the tests, I think it would require to use it in the code too, so when trying to check if isPklBinaryPresent here, when using it in the CLI, the lock would have to be present
The test are ran sequentially, but using a -race help you to spot such issues.
// Pkl validation requires the 'pkl' binary to be present | ||
if fileToValidate.FileType.Name == "pkl" { | ||
if !isPklBinaryPresent() { | ||
fmt.Printf("Warning: 'pkl' binary not found, file %s will be ignored.\n", fileToValidate.Path) | ||
continue | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this in the pkl validator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same response as below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's go back to the reason I raised the point of doing something else that what is in this PR
I see a problem in having a loop, then adding a dirty if hack in the loop.
The validator should be in charge of validating, it's not the loop that should do a big if
.
// Check if the 'pkl' binary is present in the PATH | ||
var isPklBinaryPresent = func() bool { | ||
_, err := exec.LookPath("pkl") | ||
return err == nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move this as a method of pkl validator
This would help you to validate it's behavior, abdband would prevent race in tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I briefly contemplated this but decided against it for 2 reasons:
- The intended behavior is to skip the files entirely. If we handle this within the pkl validator, we'd have to return a
true
orfalse
value, which implies that we've performed the validation and determined whether the file is good or bad. This feels like a breach of contract IMO and it goes against our intention of skipping the files altogether. - We would lose the ability to log the exact files that were skipped since validate only takes the content of the files, not the file paths
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with the second point, it would need to rewrite a part of the code
About the idea of perf, I don't mind here. If there is a pkl file, going to the pkl code to see if the file is valid, would require to validate its content. So it would be slow. Of course, when the pkl binary is not present, the code might have to go through the code and finally do not execute the code
Please note you could consider simulating the binary presence, error, absence by calling arbitrary binary name:
- true, always return 0
- false, always return 1
- a random string, the binary will be absent
It would be better than overloading the isPklBinaryPresent by something else
I'm unsure it's a good idea, but why not, it may help
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @ccoVeille and feel like the validator should be able to validate if its enabled or not rather than having the check in the CLI. The validator will see the file, realize that it is not set up properly (no PKL) and then skip it and provide that feedback to the report.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Aadd the PKL binary to the Dockerfile. Instructions
- Update the README to point to the PKL Installation Instructions so users know how to install it
- If the PKL files are skipped because the PKL binary is not available I'd like to have it show in the report summary as skipped:
"summary": { "passed": 37, "failed": 12, "skipped": 2 }
@@ -2,7 +2,7 @@ | |||
Validator recursively scans a directory to search for configuration files and | |||
validates them using the go package for each configuration type. | |||
|
|||
Currently Apple PList XML, CSV, HCL, HOCON, INI, JSON, Properties, TOML, XML, and YAML. | |||
Currently Apple PList XML, PKL, CSV, HCL, HOCON, INI, JSON, Properties, TOML, XML, and YAML. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alphabetize please
ctx := context.Background() | ||
|
||
// Convert the byte slice to a ModuleSource using TextSource | ||
source := pkl.TextSource(string(b)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there conditions where pkl.TextSource
will return an err? Do we need to handle the err?
@@ -0,0 +1,7 @@ | |||
name = "Swallow" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would prefer a more complete example
@olunusib Are you waiting on me for anything on this? |
Adds support for PKL files (https://pkl-lang.org/). Note that
pkl-go
requires Go 1.22.1. This might be considered a breaking change for users of this tool who are on earlier Go versions.Closes #141.