internal/{docs,testrunner/runners/system,internal/testrunner/runners/pipeline}: make json files end with newlines#2087
Conversation
…pipeline}: make json files end with newlines
chrisberkhout
left a comment
There was a problem hiding this comment.
Maybe
elastic-package/internal/formatter/json.go
Lines 22 to 78 in ff78679
If that's going impact too many things here seem okay.
|
I looked at doing it there, but the convolution of behaviours made it harder to reason about, so I put it where it was easier to understand. |
|
Hey @efd6, could you add a description for this change? Is it avoiding the need to reformat the files after generating them? 🤔 |
|
test integrations |
|
Created or updated PR in integrations repository to test this version. Check elastic/integrations#11055 |
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
This change needs to be applied to all packages as found out in https://buildkite.com/elastic/integrations/builds/15696
This makes the application of this change quite inconvenient. We would need to find some mechanism that removes this line only on new files or when explicitly regenerating. Do you think this could be an option?
There was a problem hiding this comment.
ISTM that a separate PR in integrations that does
parallel "perl -p -i -e 'chomp if eof'" {} ::: $(rg -l -U -g 'sample_event.json' '^}\n')
for d in $(git diff --name-only|cut -f1,2 -d/|sort|uniq); do (cd $d; elastic-package build); done
would fix all the cases to pass in both versions of elastic-package. This would be done prior to bumping the ep version for integrations.
There was a problem hiding this comment.
would fix all the cases to pass in both versions of elastic-package. This would be done prior to bumping the ep version for integrations.
Would this be compatible with the current version of elastic-package?
There was a problem hiding this comment.
Yes, that mutation takes the state of the packages to what they would be without human intervention.
There was a problem hiding this comment.
I mean, this change breaks linting on current integrations, and applying the fix in current integrations with current elastic-package will also break linting.
We should find a way of keeping compatibility with current integrations. When we needed to do this kind of changes we have done them in a way that depend on a spec version, so updating the spec version is what triggers the new behaviour. Or we have kept compatibility with both formats.
There was a problem hiding this comment.
This is the change elastic/integrations#11174.
There was a problem hiding this comment.
Do we need to have changelog entries for packages if we are just fixing these test defects?
No need for this (unless some team wants this change to be published).
This is the change elastic/integrations#11174.
Thanks, this helps.
Looking at the needed changes, what would you think about applying the fix in elastic-package only to the methods that write the events, and not to renderSampleEvent? This way we would keep compatibility with new and old packages.
There was a problem hiding this comment.
Looking at the needed changes, what would you think about applying the fix in elastic-package only to the methods that write the events, and not to renderSampleEvent? This way we would keep compatibility with new and old packages.
No, this doesn't make sense as we are adding the newline in one place and removing in the other.
There was a problem hiding this comment.
What I think confuses me from elastic/integrations#11174 is that we are removing the newlines, but here we are adding them, looks like doing the opposite of what we are intending here.
There was a problem hiding this comment.
This is code repair. The ultimate goal is to have a state of code that is agnostic to the presence of final new lines in the sample_event.json files. This does that. The change in elastic/integrations#11174 works around (by fixing the packages to conform exactly to what ep emits) the weaknesses in the linting code in ep. The new code here does not care about whether the sample_event.json file has a final new line, it will always render the event with a final newline, and when rendering it remove any trailing whitespace and add the necessary newline when required.
The deeper background for this (and some of the correctness issues) can be see here.
| } | ||
| builder.WriteString("```json\n") | ||
| builder.Write(formatted) | ||
| builder.Write(bytes.TrimSpace(formatted)) |
There was a problem hiding this comment.
To avoid having different results in existing packages, wdyt about making this dependant on the spec version? This way the change in packages is controlled by a change in the spec. This is already done for other cases now.
| builder.Write(bytes.TrimSpace(formatted)) | |
| if !specVersion.LessThan(semver.MustParse("3.2.3")) { | |
| builder.Write(bytes.TrimSpace(formatted)) | |
| } |
There was a problem hiding this comment.
This is not necessary.
There was a problem hiding this comment.
How can we make this change compatible with existing packages then?
Even if we merge elastic/integrations#11174, older packages, and older versions of elastic-package will be incompatible.
There was a problem hiding this comment.
It is already compatible, if the package conforms to the ep generated format. This is the point of the preparatory change. Once we get everything into the canonical form, the two states of this code (this change and the previous version) both produce the same README files and so the checks will pass. The sample files will differ, but they are not checked, which is essentially why the preparatory change is required.
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
What I think confuses me from elastic/integrations#11174 is that we are removing the newlines, but here we are adding them, looks like doing the opposite of what we are intending here.
jsoriano
left a comment
There was a problem hiding this comment.
Discussed over slack, let's go with this change but please merge elastic/integrations#11174 first.
|
That is the intention. |
|
Converted to draft to avoid accidental merge. |
|
test integrations |
|
Created or updated PR in integrations repository to test this version. Check elastic/integrations#11055 |
|
test integrations |
|
Created or updated PR in integrations repository to test this version. Check elastic/integrations#11055 |
|
@jsoriano The test failures in the test for this are unrelated to lint checks, so I think this is good to merge. PTAL |
Currently test expectations and sample events are rendered to files without a
trailing space. This causes unnecessary diff noise as various people add or
remove the trailing character. In the case of sample events, it also results
in the addition of blank lines in the sample events rendered in to the
readme.
This change ensures that all test expectations and sample events are rendered
with a final new line and that sample events are white-space trimmed before
insertion into readme files.