microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.47k stars 29.36k forks source link

VSCode Task or .csproj PostBuild event using the "reg" command does not work #158976

Open BBoldenow opened 2 years ago

BBoldenow commented 2 years ago

Steps to Reproduce:

  1. Create a file that contains registry keys
  2. Create a shell task in VSCode that imports the file:
{
    "label": "Import Registry Entries",
    "type": "shell",
    "command": "reg import fullfilepath",
    "group": "build"
}
  1. Alternatively I have this as a build event in a .csproj:
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(ProjectDir)RegisterDll.ps1 -targetPath $(TargetDir) -targetName $(TargetName)

The RegisterDll script:

Param(
     [Parameter()]
     [string]$targetPath
     [Parameter()]
     [string]$targetName
)
$regToCreate = $targetPath + $targetName + ".dll"
$regToLoad = $targetPath + $targetName + ".reg"

$regStringToReplace = "HKEY_CLASSES_ROOT"
$newRegString = "HKEY_CURRENT_USER\Software\Classes"

Get-Command reg | Out-Null

Write-Output "Creating new registry keys from dll: $($regToCreate)"

# This creates the registry file as expected both in VSCode and VS2022
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe /codebase /regfile $regToCreate

# This modifies the keys that were created
(Get-Content $regToLoad).Replace($regStringToReplace, $newRegString) | Set-Content $regToLoad

# From VSCode, this fails to import the keys from the file created above, I can import it manually just fine
# From VS2022, the entire script works fine
reg import $regToLoad
BBoldenow-Alt commented 2 years ago

I'm not able to verify my log in at work so I made an alt account. I forgot to point out that the VSCode terminal window reports "The operation completed successfully." for the reg import. Opening up the registry, or querying it, makes it pretty clear that that is not the case. I get the same print out when it actually is successful in VS2022.

meganrogge commented 2 years ago

are you seeing an error associated with running that task - in the console or otherwise? any task errors in the output channel for tasks?

BBoldenow-Alt commented 2 years ago

@meganrogge - There is nothing to indicate there's an error with the task - in fact the build makes it to the Post-Build step, and displays the default "The operation completed successfully." text. The reg command also returns "0" which is supposed to indicate success.

I see no logs in the "Tasks" output channel - errors or otherwise.

meganrogge commented 2 years ago

Is this a new issue or have you seen it in the past too?

BBoldenow-Alt commented 2 years ago

Is this a new issue or have you seen it in the past too?

This is a new way of registering our COM interop DLL, so unfortunately I can't say. Previously we relied on RegAsm and/or the .csproj setting "Register for COM interop". Unfortunately both of those methods require admin privileges, and we needed something that would work without that requirement. That led me down this path of creating a .reg file, and replacing the ROOT keys, then importing it. This method works great with VS2022, but most of our developers use VSCode.

BBoldenow commented 2 years ago

@meganrogge - I'd like to follow up on this, is there any additional information I could provide?

meganrogge commented 2 years ago

@BBoldenow I notice you use Get-Command reg in the script. Does it work if you pass Get-Command into the task "command" too?

BBoldenow commented 2 years ago

@BBoldenow I notice you use Get-Command reg in the script. Does it work if you pass Get-Command into the task "command" too?

@meganrogge I'm not sure I'm doing this as you expected, but I made two tasks to test what you're asking. One that does:

{ "label": "Import Registry Entries", "type": "shell", "command": "reg import fullfilepath", "group": "build", "dependsOn": [ "firstTask" ] }

And another that does:

{ "label": "firstTask", "type": "shell", "command": "Get-Command reg" }

This still doesn't work, but it's definitely "getting" the reg command (you can see it print out in the console).

I was in a hurry and completely mistyped my "firstTask"...sorry about that!

meganrogge commented 2 years ago

Just saw this part

From VSCode, this fails to import the keys from the file created above, I can import it manually just fine

From VS2022, the entire script works fine

reg import $regToLoad

seems like probably a permissions issue

could you log/ print after the line in your script where the file gets created to check if that file gets produced/exists?

BBoldenow-Alt commented 2 years ago

Just saw this part

From VSCode, this fails to import the keys from the file created above, I can import it manually just fine

From VS2022, the entire script works fine

reg import $regToLoad

seems like probably a permissions issue

could you log/ print after the line in your script where the file gets created to check if that file gets produced/exists?

@meganrogge - Yes, I put comments in the powershell script, but probably should have called it out more explicitly:

The file gets created, and the keys get modified to be HKEY_CURRENT_USER keys instead of HKEY_CLASSES_ROOT keys - this all works fine. The key import even appears to "work" as the reg import command prints out "The operation completed successfully." However in VSCode these keys fail to make it into the registry despite the successful print. I also thought it was a permissions issue, and have tried running VSCode as admin, but this doesn't seem to have any effect.

BBoldenow-Alt commented 2 years ago

@meganrogge - Do you need any additional information? I'm pretty stumped as to how to get this working in VS Code.

BBoldenow-Alt commented 2 years ago

@meganrogge - For what it's worth I did get this working as a task instead of a PostBuild event. I still think the PostBuild event should be resolved, but it let's me work around the issue in the meantime:

{ "label": "Register COM Interop (Options Prompted)", "type": "process", "command": "PowerShell.exe", "args": [ "${workspaceFolder}/Source/COM_UI/RegisterCOMInterop.ps1 -targetPath ${workspaceFolder}/.output/${input:debugOrRelease}/ -targetName COM_UI" ], "group": "build" }