Skip to content

Commit e9983fa

Browse files
authored
Fix Windows Gradle manifests incorrectly retained (#1553)
This change fixes a bug that is specific to Windows systems. When legacy Gradle (prior to v7.0.0) lockfiles exist (e.g., `gradle/dependency-locks/*.lockfile`) along with a manifest at a higher level, the manifest is meant to be ignored when finding dependency files to analyze. However, on Windows systems this was not the case before this change. Instead, both the manifest and the lockfiles were retained. After this change, only the lockfiles are retained when both are present. A failing test was added first to confirm the incorrect behavior. Then, a change was made to handle paths as `Path` objects instead of strings. That way, the generic `/` path separator character will be used correctly regardless of the runtime system. Additionally, an error message was updated to reflect the correct dependency file language for Gradle parsing errors.
1 parent 0341dd8 commit e9983fa

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1313
- Support for C#'s `packages.*.config` lockfile type
1414
- `phylum firewall log` command to browse firewall activity log
1515

16+
### Fixed
17+
18+
- Gradle manifests incorrectly retained on Windows
19+
1620
## 7.1.5 - 2024-11-26
1721

1822
### Fixed

lockfile/src/java.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Parse for GradleLock {
2626
let (_, entries) = gradle_dep::parse(data)
2727
.finish()
2828
.map_err(|e| anyhow!(convert_error(data, e)))
29-
.context("Failed to parse requirements file")?;
29+
.context("Failed to parse gradle lockfile")?;
3030
Ok(entries)
3131
}
3232

lockfile/src/lib.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,8 @@ pub fn find_depfiles_at(root: impl AsRef<Path>) -> Vec<(PathBuf, LockfileFormat)
372372
// Legacy Gradle (before v7) lockfiles are in a subdirectory,
373373
// so we truncate these directories to get the effective
374374
// directory these lockfiles were created for.
375-
let dir_str = lockfile_dir.to_string_lossy();
376375
if lockfile_format == &LockfileFormat::Gradle
377-
&& dir_str.ends_with("/gradle/dependency-locks")
376+
&& lockfile_dir.ends_with("gradle/dependency-locks")
378377
{
379378
lockfile_dir = lockfile_dir.parent().unwrap().parent().unwrap();
380379
}
@@ -764,4 +763,42 @@ mod tests {
764763
let expected = vec![(tempdir.path().join("go.mod"), LockfileFormat::GoMod)];
765764
assert_eq!(lockable_files, expected);
766765
}
766+
767+
#[test]
768+
fn skip_build_gradle_with_legacy_lockfiles() {
769+
// Create desired directory structure.
770+
let tempdir = tempfile::tempdir().unwrap();
771+
let files = [
772+
tempdir.path().join("build.gradle"),
773+
tempdir.path().join("gradle/dependency-locks/compile.lockfile"),
774+
tempdir.path().join("gradle/dependency-locks/default.lockfile"),
775+
tempdir.path().join("gradle/dependency-locks/runtime.lockfile"),
776+
];
777+
for file in &files {
778+
let dir = file.parent().unwrap();
779+
fs::create_dir_all(dir).unwrap();
780+
File::create(file).unwrap();
781+
}
782+
783+
// Find lockfiles.
784+
let mut lockfiles = find_depfiles_at(tempdir.path());
785+
786+
// Compare results.
787+
lockfiles.sort_unstable();
788+
let expected = vec![
789+
(
790+
tempdir.path().join("gradle/dependency-locks/compile.lockfile"),
791+
LockfileFormat::Gradle,
792+
),
793+
(
794+
tempdir.path().join("gradle/dependency-locks/default.lockfile"),
795+
LockfileFormat::Gradle,
796+
),
797+
(
798+
tempdir.path().join("gradle/dependency-locks/runtime.lockfile"),
799+
LockfileFormat::Gradle,
800+
),
801+
];
802+
assert_eq!(lockfiles, expected);
803+
}
767804
}

0 commit comments

Comments
 (0)