Skip to content

Commit 154aef7

Browse files
authored
Add cargo-audit to build (also track in cgmanifest.json) (#3403)
Fixes #1647
1 parent 7415778 commit 154aef7

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

eng/cgmanifest.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88
"cargo": { "name": "cargo-semver-checks", "version": "0.45.0" }
99
},
1010
"developmentDependency": true
11+
},
12+
{
13+
"component": {
14+
"type": "cargo",
15+
"cargo": { "name": "cargo-audit", "version": "0.22.0" }
16+
},
17+
"developmentDependency": true
1118
}
1219
]
1320
}

eng/scripts/Analyze-Code.ps1

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,62 @@ $ErrorActionPreference = 'Stop'
1313
Set-StrictMode -Version 2.0
1414

1515
. (Join-Path $PSScriptRoot '..' 'common' 'scripts' 'common.ps1')
16+
. ([System.IO.Path]::Combine($PSScriptRoot, 'shared', 'Cargo.ps1'))
1617

1718
Write-Host @"
1819
Analyzing code with
1920
RUSTFLAGS: '${env:RUSTFLAGS}'
2021
RUSTDOCFLAGS: '${env:RUSTDOCFLAGS}'
22+
RUST_LOG: '${env:RUST_LOG}'
2123
"@
2224

2325
if ($CheckWasm) {
24-
Invoke-LoggedCommand "rustup target add wasm32-unknown-unknown"
26+
Invoke-LoggedCommand "rustup target add wasm32-unknown-unknown" -GroupOutput
2527
}
2628

2729
if ($Deny) {
28-
Invoke-LoggedCommand "cargo install cargo-deny --locked"
30+
Invoke-LoggedCommand "cargo install cargo-deny --locked" -GroupOutput
2931
}
3032

31-
Invoke-LoggedCommand "cargo check --package azure_core --all-features --all-targets --keep-going"
33+
$cargoAuditVersionParams = Get-VersionParamsFromCgManifest cargo-audit
34+
Invoke-LoggedCommand "cargo install cargo-audit --locked $($cargoAuditVersionParams -join ' ')" -GroupOutput
35+
Invoke-LoggedCommand "cargo audit" -GroupOutput
3236

33-
Invoke-LoggedCommand "cargo fmt --all -- --check"
37+
Invoke-LoggedCommand "cargo check --package azure_core --all-features --all-targets --keep-going" -GroupOutput
3438

35-
Invoke-LoggedCommand "cargo clippy --workspace --all-features --all-targets --keep-going --no-deps"
39+
Invoke-LoggedCommand "cargo fmt --all -- --check" -GroupOutput
40+
41+
Invoke-LoggedCommand "cargo clippy --workspace --all-features --all-targets --keep-going --no-deps" -GroupOutput
3642

3743
if ($CheckWasm) {
3844
# Save the original RUSTFLAGS to restore later
3945
$OriginalRustFlags = $env:RUSTFLAGS
4046
# This is needed to ensure that the `getrandom` crate uses the `wasm_js` backend
4147
$env:RUSTFLAGS = ${env:RUSTFLAGS} + ' --cfg getrandom_backend="wasm_js"'
4248

43-
Invoke-LoggedCommand "cargo clippy --target=wasm32-unknown-unknown --workspace --keep-going --no-deps"
49+
Invoke-LoggedCommand "cargo clippy --target=wasm32-unknown-unknown --workspace --keep-going --no-deps" -GroupOutput
4450

4551
# Restore the original RUSTFLAGS, since the getrandom config option can only be set for wasm32-unknown-unknown builds.
4652
$env:RUSTFLAGS = $OriginalRustFlags
4753
}
4854

4955
if ($Deny) {
50-
Invoke-LoggedCommand "cargo deny --all-features check"
56+
Invoke-LoggedCommand "cargo deny --all-features check" -GroupOutput
5157
}
5258

53-
Invoke-LoggedCommand "cargo doc --workspace --no-deps --all-features"
59+
Invoke-LoggedCommand "cargo doc --workspace --no-deps --all-features" -GroupOutput
5460

5561
# Verify package dependencies
5662
$verifyDependenciesScript = Join-Path $RepoRoot 'eng' 'scripts' 'verify-dependencies.rs' -Resolve
5763

5864
if (!$SkipPackageAnalysis) {
5965
if (!(Test-Path $PackageInfoDirectory)) {
6066
Write-Host "Analyzing workspace`n"
61-
return Invoke-LoggedCommand "&$verifyDependenciesScript $RepoRoot/Cargo.toml"
67+
return Invoke-LoggedCommand "&$verifyDependenciesScript $RepoRoot/Cargo.toml" -GroupOutput
6268
}
6369

6470
if ($Toolchain -eq 'nightly') {
65-
Invoke-LoggedCommand "cargo install --locked cargo-docs-rs"
71+
Invoke-LoggedCommand "cargo install --locked cargo-docs-rs" -GroupOutput
6672
}
6773

6874
$packagesToTest = Get-ChildItem $PackageInfoDirectory -Filter "*.json" -Recurse
@@ -71,10 +77,10 @@ if (!$SkipPackageAnalysis) {
7177

7278
foreach ($package in $packagesToTest) {
7379
Write-Host "Analyzing package '$($package.Name)' in directory '$($package.DirectoryPath)'`n"
74-
Invoke-LoggedCommand "&$verifyDependenciesScript $($package.DirectoryPath)/Cargo.toml"
80+
Invoke-LoggedCommand "&$verifyDependenciesScript $($package.DirectoryPath)/Cargo.toml" -GroupOutput
7581

7682
if ($Toolchain -eq 'nightly') {
77-
Invoke-LoggedCommand "cargo +nightly docs-rs --package $($package.Name)"
83+
Invoke-LoggedCommand "cargo +nightly docs-rs --package $($package.Name)" -GroupOutput
7884
}
7985
}
8086
}

eng/scripts/Test-Semver.ps1

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,9 @@ function Get-OutputPackageNames($workspacePackages) {
4343
$packages = Get-CargoPackages
4444
$outputPackageNames = Get-OutputPackageNames $packages
4545

46-
# Read version from cgmanifest.json. If ignored the currently installed or
47-
# "latest" version is used.
4846
$versionParams = @()
4947
if (!$IgnoreCgManifestVersion) {
50-
$versionParams += '--version'
51-
$cgManifest = Get-Content ([System.IO.Path]::Combine($PSScriptRoot, '..', 'cgmanifest.json')) `
52-
| ConvertFrom-Json
53-
$versionParams += $cgManifest.
54-
registrations.
55-
Where({ $_.component.type -eq 'cargo' -and $_.component.cargo.name -eq 'cargo-semver-checks' }).
56-
component.cargo.version
48+
$versionParams = Get-VersionParamsFromCgManifest cargo-semver-checks
5749
}
5850

5951
LogGroupStart "cargo install cargo-semver-checks --locked $($versionParams -join ' ')"

eng/scripts/shared/Cargo.ps1

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,25 @@ function Get-PackageNamesFromPackageInfo($packageInfoDirectory) {
3131

3232
return $names
3333
}
34+
35+
function Get-VersionParamsFromCgManifest(
36+
$packageName,
37+
$cgManifestPath = ([System.IO.Path]::Combine($PSScriptRoot, '..', '..', 'cgmanifest.json'))
38+
) {
39+
$cgManifest = Get-Content $cgManifestPath `
40+
| ConvertFrom-Json
41+
$versions = $cgManifest.
42+
registrations.
43+
Where({ $_.component.type -eq 'cargo' -and $_.component.cargo.name -eq $packageName }).
44+
component.cargo.version
45+
46+
if (!$versions) {
47+
Write-Error "No versions found for package '$packageName' in cgmanifest.json"
48+
}
49+
50+
if ($versions -is [Array] -and $versions.Count -ne 1) {
51+
Write-Error "Multiple versions found for package '$packageName' in cgmanifest.json"
52+
}
53+
54+
return @('--version', $versions)
55+
}

0 commit comments

Comments
 (0)