jcolebrand
jcolebrand

Reputation: 16025

Visual Studio PreBuildEvent powershell The string is missing the terminator: "

Had the strangest error earlier that I was genuinely confused about, so I thought I would write up a self-answer to help people out. My googlefu turned up nothing neatly, so here we are.

This was my original line

<PreBuildEvent>powershell.exe -noninteractive -command "$(solutiondir)\..\PreBuild.ps1" "$(TargetFileName)" "$(ProjectDir)" "$(SolutionDir)"</PreBuildEvent>

and here's my PreBuild.ps1 file:

Param(
    [Parameter(Mandatory=$false)][string]$targetFileName,
    [Parameter(Mandatory=$false)][string]$projectDir,
    [Parameter(Mandatory=$false)][string]$solutionDir
)
process {
    Write-Host "`$targetFileName $targetFileName";
    Write-Host "`$projectDir $projectDir";
    Write-Host "`$solutionDir $solutionDir";
}

For some reason the variable $projectDir in my script contains both the projectdir and the solutiondir. Like so:

2>  Processing files with gulp script.
2>  $targetFileName project.dll
2>  $projectDir c:\src\sln\project c:\src\sln

So I changed it to this

<PreBuildEvent>powershell.exe -noninteractive -command "$(solutiondir)\..\PreBuild.ps1" "$(TargetFileName)" "$(ProjectDir)"</PreBuildEvent>

and I get this error in VS Output: The string is missing the terminator: ".

Upvotes: 1

Views: 724

Answers (1)

jcolebrand
jcolebrand

Reputation: 16025

Here's where I messed up. $(ProjectDir) ends with a trailing slash. So effectively my command is:

powershell.exe -noninteractive -command "c:\src\sln\project\..\PreBuild.ps1" "project.dll" "c:\src\sln\project\"

Do you see it? It's subtle, took me a few minutes. It's the \" at the end. That escapes the quote, making the string unbounded. Hence the missing closing quote.

So now if I put simply this line:

<PreBuildEvent>powershell.exe -noninteractive -command "$(solutiondir)\..\PreBuild.ps1" $(TargetFileName) $(ProjectDir)</PreBuildEvent>

It all works as expected. The quotes were really because I was afraid of spaces. If someone ends up with some in their paths, I'll go back and punt.

Upvotes: 3

Related Questions