fix(java): skip comment lines in gradle.lockfile (#5372)

Signed-off-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
Co-authored-by: Pujitha Paladugu <10557236+pujitha24@users.noreply.github.com>
This commit is contained in:
Pujitha Paladugu
2026-10-26 10:46:54 -04:00
committed by GitHub
co-authored by Pujitha Paladugu
parent 4c500520d0
commit 722df28ed1
2 changed files with 21 additions and 0 deletions
@@ -35,6 +35,11 @@ func parseGradleLockfile(_ context.Context, _ file.Resolver, _ *generic.Environm
// Trim leading and trailing whitespace from the line
line = strings.TrimSpace(line)
// skip comment lines (e.g. the header gradle writes), which may contain ':' and '=' characters
if strings.HasPrefix(line, "#") {
continue
}
groupNameVersion := line
groupNameVersion = strings.Split(groupNameVersion, "=")[0]
parts := strings.Split(groupNameVersion, ":")
@@ -87,3 +87,19 @@ func Test_parserGradleLockfile_lineTooLong(t *testing.T) {
require.Len(t, pkgs, 1)
assert.Equal(t, "before", pkgs[0].Name)
}
func Test_parserGradleLockfile_skipsComments(t *testing.T) {
contents := `# This is a Gradle generated file for dependency locking.
# To regenerate this file, run: ./gradlew :dependencies --write-locks
# indented: comment=too
com.squareup.okhttp3:okhttp:4.12.0=compileClasspath,runtimeClasspath
empty=annotationProcessor
`
reader := file.NewLocationReadCloser(file.NewLocation("gradle.lockfile"), io.NopCloser(strings.NewReader(contents)))
pkgs, _, err := parseGradleLockfile(context.Background(), nil, nil, reader)
require.NoError(t, err)
require.Len(t, pkgs, 1)
assert.Equal(t, "okhttp", pkgs[0].Name)
assert.Equal(t, "4.12.0", pkgs[0].Version)
}