Skip to content

Commit e9a6810

Browse files
Fix default logPath when no user profile is available (#283)
In Azure Function Apps, the special `MyDocuments` folder resolves to an empty string, which means that the default `logPath` gets stored as an empty string. https://github.com/microsoft/PowerShellForGitHub/blob/3094c9789caf00382134a0703344f655b13a1e33/GitHubConfiguration.ps1#L650-L657 https://github.com/microsoft/PowerShellForGitHub/blob/3094c9789caf00382134a0703344f655b13a1e33/GitHubConfiguration.ps1#L671 This causes problems when `Write-Log` is called by any other function, because we directly assign the current configuration value for `LogPath` as the parameter's default, which throws an exception when an empty string is assigned. https://github.com/microsoft/PowerShellForGitHub/blob/3094c9789caf00382134a0703344f655b13a1e33/Helpers.ps1#L142 In this scenario, we'll fall back to using the `LocalApplicationDataFolder` folder and writing a verbose message to the user letting them know this has happened. I've opted for a verbose message as opposed to a warning to avoid a constant annoying warning for users. The alternate path is documented in the updated USAGE.md, and it's also retrievable by calling `Get-GitHubConfiguration -Name LogPath`. Documentation has also been updated to reflect this behavior change. Fixes #282
1 parent 5ee0365 commit e9a6810

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

GitHubConfiguration.ps1

+21-4
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
# The location of the file that we'll store any settings that can/should roam with the user.
88
[string] $script:configurationFilePath = [System.IO.Path]::Combine(
9-
[Environment]::GetFolderPath('ApplicationData'),
9+
[System.Environment]::GetFolderPath('ApplicationData'),
1010
'Microsoft',
1111
'PowerShellForGitHub',
1212
'config.json')
1313

1414
# The location of the file that we'll store the Access Token SecureString
1515
# which cannot/should not roam with the user.
1616
[string] $script:accessTokenFilePath = [System.IO.Path]::Combine(
17-
[Environment]::GetFolderPath('LocalApplicationData'),
17+
[System.Environment]::GetFolderPath('LocalApplicationData'),
1818
'Microsoft',
1919
'PowerShellForGitHub',
2020
'accessToken.txt')
@@ -650,10 +650,16 @@ function Import-GitHubConfiguration
650650
# Create a configuration object with all the default values. We can then update the values
651651
# with any that we find on disk.
652652
$logPath = [String]::Empty
653+
$logName = 'PowerShellForGitHub.log'
653654
$documentsFolder = [System.Environment]::GetFolderPath('MyDocuments')
654-
if (-not [System.String]::IsNullOrEmpty($documentsFolder))
655+
$logToLocalAppDataFolder = [System.String]::IsNullOrEmpty($documentsFolder)
656+
if ($logToLocalAppDataFolder)
655657
{
656-
$logPath = Join-Path -Path $documentsFolder -ChildPath 'PowerShellForGitHub.log'
658+
$logPath = Join-Path -Path ([System.Environment]::GetFolderPath('LocalApplicationData')) -ChildPath $logName
659+
}
660+
else
661+
{
662+
$logPath = Join-Path -Path $documentsFolder -ChildPath $logName
657663
}
658664

659665
$config = [PSCustomObject]@{
@@ -697,6 +703,17 @@ function Import-GitHubConfiguration
697703
$config.$name = Resolve-PropertyValue -InputObject $jsonObject -Name $name -Type $type -DefaultValue $config.$name
698704
}
699705

706+
# Let the user know when we had to revert to using the LocalApplicationData folder for the
707+
# log location (if they haven't already changed its path in their local config).
708+
$configuredLogPath = $config.logPath
709+
if ($logToLocalAppDataFolder -and ($logPath -eq $configuredLogPath))
710+
{
711+
# Limited instance where we write the warning directly instead of using Write-Log, since
712+
# Write-Log won't yet be configured.
713+
$message = "Storing log at non-default location: [$logPath] (no user profile path was found). You can change this location by calling Set-GitHubConfiguration -LogPath <desiredPathToLogFile>"
714+
Write-Verbose -Message $message
715+
}
716+
700717
return $config
701718
}
702719

USAGE.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ The logging is affected by configuration properties (which can be checked with
137137
`Get-GitHubConfiguration` and changed with `Set-GitHubConfiguration`).
138138

139139
**`LogPath`** [string] The logfile. Defaults to
140-
`$env:USERPROFILE\Documents\PowerShellForGitHub.log`
140+
`([System.Environment]::GetFolderPath('MyDocuments'))\PowerShellForGitHub.log`. Will default to
141+
`([System.Environment]::GetFolderPath('LocalApplicationData'))\PowerShellForGitHub.log` when
142+
there is no user profile (like in an Azure environment).
141143

142144
**`DisableLogging`** [bool] Defaults to `$false`.
143145

0 commit comments

Comments
 (0)