Skip to content

Commit 3f0aa1b

Browse files
fix(linting): use cross-platform path separators in gitignore pattern matching (#121)
## Description Updated the PowerShell linting helpers to use platform-appropriate path separators, enabling proper gitignore exclusion behavior in Linux-based dev container environments. - **fix**(_linting_): Updated `Get-GitIgnorePatterns` function to use `[System.IO.Path]::DirectorySeparatorChar` instead of hardcoded backslashes - Normalizes input patterns by replacing both `/` and `\` with the platform separator - Wildcard patterns now correctly match on both Windows and Unix systems - **refactor**(_linting_): Replaced inline gitignore parsing in `Validate-MarkdownFrontmatter.ps1` with call to shared `Get-GitIgnorePatterns` helper - Removed 24 lines of duplicate code - Ensures consistent gitignore behavior across all linting scripts ## Related Issue(s) Fixes #120 ## Type of Change **Code & Documentation:** - [x] Bug fix (non-breaking change fixing an issue) - [ ] New feature (non-breaking change adding functionality) - [ ] Breaking change (fix or feature causing existing functionality to change) - [ ] Documentation update **Infrastructure & Configuration:** - [ ] GitHub Actions workflow - [x] Linting configuration (markdown, PowerShell, etc.) - [ ] Security configuration - [ ] DevContainer configuration - [ ] Dependency update **Other:** - [x] Script/automation (`.ps1`, `.sh`, `.py`) ## Testing - Validated with PSScriptAnalyzer (exit code 0, no issues) - Cross-platform path separator logic verified using `[System.IO.Path]::DirectorySeparatorChar` ## Checklist ### Required Checks - [x] Documentation is updated (if applicable) - [x] Files follow existing naming conventions - [x] Changes are backwards compatible (if applicable) ### Required Automated Checks - [ ] Markdown linting: `npm run lint:md` - [ ] Spell checking: `npm run spell-check` - [ ] Frontmatter validation: `npm run lint:frontmatter` - [ ] Link validation: `npm run lint:md-links` - [x] PowerShell analysis: `npm run lint:ps` ## Security Considerations - [x] This PR does not contain any sensitive or NDA information - [x] Any new dependencies have been reviewed for security issues - [x] Security-related scripts follow the principle of least privilege ## Additional Notes This fix addresses an issue where gitignore patterns using hardcoded Windows backslashes (`\`) failed to match paths on Linux systems in the dev container. The solution uses .NET's `DirectorySeparatorChar` for platform detection and normalizes patterns accordingly. 🐛 - Generated by Copilot
1 parent 57ef20d commit 3f0aa1b

2 files changed

Lines changed: 12 additions & 29 deletions

File tree

scripts/linting/Modules/LintingHelpers.psm1

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ function Get-GitIgnorePatterns {
145145
Path to .gitignore file.
146146
147147
.OUTPUTS
148-
Array of wildcard patterns.
148+
Array of wildcard patterns using platform-appropriate separators.
149149
#>
150150
[CmdletBinding()]
151151
param(
@@ -157,19 +157,24 @@ function Get-GitIgnorePatterns {
157157
return @()
158158
}
159159

160+
$sep = [System.IO.Path]::DirectorySeparatorChar
161+
160162
$patterns = Get-Content $GitIgnorePath | Where-Object {
161163
$_ -and -not $_.StartsWith('#') -and $_.Trim() -ne ''
162164
} | ForEach-Object {
163165
$pattern = $_.Trim()
164166

167+
# Normalize to platform separator
168+
$normalizedPattern = $pattern.Replace('/', $sep).Replace('\', $sep)
169+
165170
if ($pattern.EndsWith('/')) {
166-
"*\$($pattern.TrimEnd('/'))\*"
171+
"*$sep$($normalizedPattern.TrimEnd($sep))$sep*"
167172
}
168-
elseif ($pattern.Contains('/')) {
169-
"*\$($pattern.Replace('/', '\'))*"
173+
elseif ($pattern.Contains('/') -or $pattern.Contains('\')) {
174+
"*$sep$normalizedPattern*"
170175
}
171176
else {
172-
"*\$pattern\*"
177+
"*$sep$normalizedPattern$sep*"
173178
}
174179
}
175180

scripts/linting/Validate-MarkdownFrontmatter.ps1

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -501,31 +501,9 @@ function Test-FrontmatterValidation {
501501
}
502502
}
503503

504-
# Parse .gitignore patterns
505-
$gitignorePatterns = @()
504+
# Parse .gitignore patterns using shared helper function
506505
$gitignorePath = Join-Path $repoRoot ".gitignore"
507-
if (Test-Path $gitignorePath) {
508-
$gitignorePatterns = Get-Content $gitignorePath | Where-Object {
509-
$_ -and
510-
-not $_.StartsWith('#') -and
511-
$_.Trim() -ne ''
512-
} | ForEach-Object {
513-
$pattern = $_.Trim()
514-
# Convert gitignore patterns to PowerShell wildcard patterns
515-
if ($pattern.EndsWith('/')) {
516-
# Directory pattern
517-
"*\$($pattern.TrimEnd('/'))\*"
518-
}
519-
elseif ($pattern.Contains('/')) {
520-
# Path pattern
521-
"*\$($pattern.Replace('/', '\'))*"
522-
}
523-
else {
524-
# Simple pattern
525-
"*\$pattern\*"
526-
}
527-
}
528-
}
506+
$gitignorePatterns = Get-GitIgnorePatterns -GitIgnorePath $gitignorePath
529507

530508
Write-Host "🔍 Validating frontmatter across markdown files..." -ForegroundColor Cyan
531509

0 commit comments

Comments
 (0)