GoogleCloudPlatform / google-cloud-powershell

PowerShell cmdlets for the Google Cloud Platform
http://googlecloudplatform.github.io/google-cloud-powershell/
Apache License 2.0
135 stars 61 forks source link

$Env:PATH is not set correctly on MacOS/*nix systems in PS7 on module installation. #643

Open phatmandrake opened 4 years ago

phatmandrake commented 4 years ago

This is a breaking behavior on MacOS Catalina and presumably, any other system Unix based OS. This issue results in the GoogleCloud Module being unable to find the gcloud binary, which fundamentally breaks the entire module.

File: ../powershell/Modules/GoogleCloud/1.0.1.10/GoogleCloud.psm1 Function: Install-GCloudSdkSilently()

Current: Failure Reason: $CloudBinPath uses literal \ characters to build the path. This is inappropriate for *nix systems.

    if (-not (IsWindows)) {
        curl https://sdk.cloud.google.com | bash -s -- --disable-prompts
        $cloudBinPath = "$HOME\google-cloud-sdk\bin"
        $envPath = [System.Environment]::GetEnvironmentVariable("PATH")
        if (-not $envPath.Contains($cloudBinPath)) {
            [System.Environment]::SetEnvironmentVariable("PATH", "$($envPath):$cloudBinPath")
        }
        return
    }

Adjusted: Resolution Reason: By building the path with the Join-Path Cmdlet, it ensures the correct directory separators are used.

if (-not (IsWindows)) {
        curl https://sdk.cloud.google.com | bash -s -- --disable-prompts
        $cloudBinPath = Join-Path $home "google-cloud-sdk" "bin"
        $envPath = [System.Environment]::GetEnvironmentVariable("PATH")
        if (-not $envPath.Contains($cloudBinPath)) {
            [System.Environment]::SetEnvironmentVariable("PATH", "$($envPath):$cloudBinPath")
        }
        return
    }

There is an additional issue in that [System.Environment]::SetEnvironmentVariable("PATH", "$($envPath):$cloudBinPath") appears to be only supported on Windows for persistent Environment Variables changes. DaughtNeat

PS on MacOS has a means for aggregating the PATH env variables set by other shells on the system. I haven't quite ascertained the exact mechanism for the behavior. This could be used to update $env:Path in PS, but it could just as easily be modified by updating $PSProfile to have a statement that appends the gcloud directory to $Env:Path on each session. There would need to be some logic to account for different profiles perhaps.

I realize this may be a bit of a graveyard, but once I was able to address the malformed path and added $Env:PATH += ":$(Join-Path $home "google-cloud-sdk" "bin")" (case-senstive af )to my PSProfile the module, thus far, works as anticipated.

I would submit a pull request, but I am not convinced on the best path for addressing the second issue for this yet.

Personally I don't think anyone should have to statically update their PATH to use the binary. At best I would say it's something that should perhaps occur when the Functions are called, and only temporarily for the session.

Hope this helps someone.