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

Fix msbuild parser allowing missing versions #1559

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- `phylum exception` subcommand for managing suppressions

### Fixed

- `msbuild` lockfile parser allowing missing names and versions

## 7.2.0 - 2024-12-10

### Added
Expand Down
24 changes: 15 additions & 9 deletions lockfile/src/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ pub struct CSProj;
#[derive(Debug, Deserialize, PartialEq, Eq)]
pub struct PackageReference {
#[serde(alias = "@Include", default)]
pub name: String,
pub name: Option<String>,

#[serde(alias = "@Version", alias = "@version", alias = "Version", default)]
pub version: String,
pub version: Option<String>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
Expand All @@ -113,13 +113,15 @@ struct Project {
pub item_groups: Vec<ItemGroup>,
}

impl From<PackageReference> for Package {
fn from(pkg_ref: PackageReference) -> Self {
Self {
name: pkg_ref.name,
version: PackageVersion::FirstParty(pkg_ref.version),
impl TryFrom<PackageReference> for Package {
type Error = ();

fn try_from(pkg_ref: PackageReference) -> Result<Self, Self::Error> {
Ok(Self {
name: pkg_ref.name.ok_or(())?,
version: PackageVersion::FirstParty(pkg_ref.version.ok_or(())?),
package_type: PackageType::Nuget,
}
})
}
}

Expand All @@ -130,7 +132,11 @@ impl From<Project> for Vec<Package> {
for item_group in proj.item_groups {
if !item_group.dependencies.is_empty() {
deps.extend(
item_group.dependencies.into_iter().map(Package::from).collect::<Vec<_>>(),
item_group
.dependencies
.into_iter()
.filter_map(|pkg| Package::try_from(pkg).ok())
.collect::<Vec<_>>(),
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/sample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
Expand All @@ -20,6 +21,7 @@
<PackageReference Include="System.Collections.Immutable">
<version>1.5.0</version>
</PackageReference>
<PackageReference Include="Newtonsoft.Json"/>
</ItemGroup>

<ItemGroup>
Expand Down
Loading