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 PowerShell instructions for GitHub Copilot CLI #32189

Open
1 task done
mpusch88 opened this issue Mar 23, 2024 · 5 comments
Open
1 task done

Fix PowerShell instructions for GitHub Copilot CLI #32189

mpusch88 opened this issue Mar 23, 2024 · 5 comments
Labels
content This issue or pull request belongs to the Docs Content team copilot Content related to GitHub Copilot help wanted Anyone is welcome to open a pull request to fix this issue

Comments

@mpusch88
Copy link

Code of Conduct

What article on docs.github.com is affected?

https://docs.github.com/en/copilot/github-copilot-in-the-cli/using-github-copilot-in-the-cli

What part(s) of the article would you like to see updated?

Currently these instructions do not clarify which versions of PowerShell the premade Copilot aliases are compatible with - it looks like the output of the gh copilot alias command only works for PowerShell 7+, not Windows PowerShell.

It seems like it would make sense to do one of the following:

  • Rewrite the output of the gh copilot alias -- pwsh command
  • Add a gh copilot alias -- powershell command
  • Explain in the documentation that this only works for PowerShell 7+.

The expected outcome of this is that it would help people avoid confusion and annoyance.

Additional information

It's documentation - it is always reproducible, and everyone is effected.

@mpusch88 mpusch88 added the content This issue or pull request belongs to the Docs Content team label Mar 23, 2024
@github-actions github-actions bot added the triage Do not begin working on this issue until triaged by the team label Mar 23, 2024
@mklement0
Copy link

mklement0 commented Mar 23, 2024

The generated function code can easily be made Windows PowerShell-compatible, so I think the best solution is to accept gh copilot alias -- powershell simply as an alias of gh copilot alias -- pwsh and emit code that works in both PowerShell editions:

  • The only reason the currently generated code doesn't work is the use of PS 7+-only clean blocks.

  • Using begin, process, end and clean blocks isn't actually necessary, given that the functions don't accept pipeline input, so the following cross-edition reformulation does away with them, and uses try { ... } finally { ... } for cleanup:

#  These functions work in both Windows PowerShell and PowerShell (Core) 7+
function ghcs {
	# Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
	# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
	param(
		[ValidateSet('gh', 'git', 'shell')]
		[Alias('t')]
		[String]$Target = 'shell',

		[Parameter(Position = 0, ValueFromRemainingArguments)]
		[string]$Prompt
	)
	# Create temporary file to store potential command user wants to execute when exiting
	$executeCommandFile = New-TemporaryFile

	# Store original value of GH_DEBUG environment variable
	$envGhDebug = $Env:GH_DEBUG

	if ($PSBoundParameters['Debug']) {
		$Env:GH_DEBUG = 'api'
	}

	try {
		gh copilot suggest -t $Target -s "$executeCommandFile" $Prompt

		# Execute command contained within temporary file if it is not empty
		if ($executeCommandFile.Length -gt 0) {
			# Extract command to execute from temporary file
			$executeCommand = (Get-Content -Path $executeCommandFile -Raw).Trim()

			# Insert command into PowerShell up/down arrow key history
			[Microsoft.PowerShell.PSConsoleReadLine]::AddToHistory($executeCommand)

			# Insert command into PowerShell history
			$now = Get-Date
			$executeCommandHistoryItem = [PSCustomObject]@{
				CommandLine        = $executeCommand
				ExecutionStatus    = [Management.Automation.Runspaces.PipelineState]::NotStarted
				StartExecutionTime = $now
				EndExecutionTime   = $now.AddSeconds(1)
			}
			Add-History -InputObject $executeCommandHistoryItem

			# Execute command
			Write-Host "`n"
			Invoke-Expression $executeCommand
      
		}
	}
	finally {
		# Clean up temporary file used to store potential command user wants to execute when exiting
		Remove-Item -Path $executeCommandFile
		# Restore GH_DEBUG environment variable to its original value
		$Env:GH_DEBUG = $envGhDebug
	}
}

function ghce {
	# Debug support provided by common PowerShell function parameters, which is natively aliased as -d or -db
	# https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_commonparameters?view=powershell-7.4#-debug
	param(
		[Parameter(Position = 0, ValueFromRemainingArguments)]
		[string[]]$Prompt
	)
	# Store original value of GH_DEBUG environment variable
	$envGhDebug = $Env:GH_DEBUG
	if ($PSBoundParameters['Debug']) {
		$Env:GH_DEBUG = 'api'
	}

	try {
		gh copilot explain $Prompt
	}
	finally {
		# Restore GH_DEBUG environment variable to its original value
		$Env:GH_DEBUG = $envGhDebug
	}

}

@nguyenalex836
Copy link
Contributor

@mpusch88 Thank you for opening this PR, and thank you @mklement0 for the added perspective! ✨ Our team will provide feedback regarding the best next steps for this issue - thanks for your patience! 💛

@nguyenalex836 nguyenalex836 added waiting for review Issue/PR is waiting for a writer's review copilot Content related to GitHub Copilot and removed triage Do not begin working on this issue until triaged by the team labels Mar 25, 2024
@github-actions github-actions bot added the stale There is no recent activity on this issue or pull request label May 24, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@nguyenalex836 nguyenalex836 removed the stale There is no recent activity on this issue or pull request label May 31, 2024
@nguyenalex836 nguyenalex836 reopened this May 31, 2024
@github-actions github-actions bot added the triage Do not begin working on this issue until triaged by the team label May 31, 2024
@nguyenalex836 nguyenalex836 removed the triage Do not begin working on this issue until triaged by the team label Jun 13, 2024
@hubwriter
Copy link
Contributor

@mpusch88 and @mklement0 - thanks for raising and commenting on this issue.

Please clarify what change needs to be made to the documentation.

@nguyenalex836 nguyenalex836 added more-information-needed More information is needed to complete review and removed waiting for review Issue/PR is waiting for a writer's review labels Jul 22, 2024
@mpusch88
Copy link
Author

"It seems like it would make sense to do one of the following:

Rewrite the output of the gh copilot alias -- pwsh command
Add a gh copilot alias -- powershell command
Explain in the documentation that this only works for PowerShell 7+."

The winner is clearly "Rewrite the output of the gh copilot alias -- pwsh command". I don't have time to do that right now.

@hubwriter hubwriter added help wanted Anyone is welcome to open a pull request to fix this issue and removed more-information-needed More information is needed to complete review labels Jul 24, 2024
@hubwriter
Copy link
Contributor

I've marked this issue help-wanted. If anyone reading this would like to raise a pull request to update the docs as suggested above that would be great.

For information about contributing to the docs see:
https://docs.github.com/en/contributing

@github-actions github-actions bot added the stale There is no recent activity on this issue or pull request label Sep 23, 2024
@nguyenalex836 nguyenalex836 removed the stale There is no recent activity on this issue or pull request label Sep 26, 2024
@github-actions github-actions bot added the stale There is no recent activity on this issue or pull request label Nov 26, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 3, 2024
@nguyenalex836 nguyenalex836 reopened this Dec 3, 2024
@nguyenalex836 nguyenalex836 removed the stale There is no recent activity on this issue or pull request label Dec 3, 2024
@github-actions github-actions bot added the triage Do not begin working on this issue until triaged by the team label Dec 3, 2024
@nguyenalex836 nguyenalex836 removed the triage Do not begin working on this issue until triaged by the team label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content This issue or pull request belongs to the Docs Content team copilot Content related to GitHub Copilot help wanted Anyone is welcome to open a pull request to fix this issue
Projects
None yet
Development

No branches or pull requests

4 participants