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 #5376 - escape parameters passed to dotnet-ef from PMC #5618

Merged
merged 2 commits into from
Jun 1, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -657,8 +657,6 @@ function InvokeDotNetEf($project, [switch] $json, [switch] $skipBuild) {
throw "Cannot execute this command because 'Microsoft.EntityFrameworkCore.Tools' is not installed in project '$projectName'. Add 'Microsoft.EntityFrameworkCore.Tools' to the 'tools' section in project.json. See http://go.microsoft.com/fwlink/?LinkId=798221 for more details."
}

$output = $null

$config = $project.ConfigurationManager.ActiveConfiguration.ConfigurationName
$arguments = "--configuration", $config
Write-Debug "Using configuration $config"
Expand All @@ -675,39 +673,45 @@ function InvokeDotNetEf($project, [switch] $json, [switch] $skipBuild) {

if ($json) {
$arguments += ,"--json"
}
}

if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
$arguments += ,"--verbose"
}

$arguments = $arguments | ? { $_ } | % { "'$($_ -replace "'", "''" )'" }

$output = $null

$arguments = $arguments | ? { $_ } | % { if ($_ -like '* *') { "'$_'" } else { $_ } }

$command = "ef $($arguments -join ' ')"
try {
Write-Verbose "Working directory: $fullPath"
Push-Location $fullPath
$ErrorActionPreference='SilentlyContinue'
Write-Verbose "Executing command: dotnet $command"
$output = Invoke-Expression "& '$dotnet' $command" -ErrorVariable verboseOutput
# TODO don't use invoke-expression.
# This will require running dotnet-build as a separate command because build
# warnings still appear in stderr
$stdout = Invoke-Expression "& '$dotnet' $command" -ErrorVariable stderr
$exit = $LASTEXITCODE
$stdout | Out-String | Write-Verbose
Write-Debug "Finish executing command with code $exit"
if ($exit -ne 0) {
if (!($verboseOutput) -and $output) {
if (!($stderr) -and $stdout) {
# most often occurs when Microsoft.EntityFrameworkCore.Tools didn't install
throw $output
throw $stdout
}
throw $verboseOutput
throw $stderr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if there is no stderr? I.e. does this need if ($stderr) { ... } around it?

Copy link
Contributor Author

@natemcmaster natemcmaster Jun 1, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See line above. I could remove -and $stdout from the predicate because that is always true...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me put it another way - what happens if both stderr and stdout are empty? Does the call at line 704 error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose it would, but I do not know of any case when a call to dotnet returns with a non-zero exit code an has no output on either stderr or stdout

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I'd rather issue an error message in that scenario, but that's not the main point of this fix.

}

if ($json) {
Write-Debug "Parsing json output"
Write-Debug $($output -join [Environment]::NewLine)
$startLine = $output.IndexOf("//BEGIN") + 1
$endLine = $output.IndexOf("//END") - 1
$output = $output[$startLine..$endLine] -join [Environment]::NewLine | ConvertFrom-Json
$startLine = $stdout.IndexOf("//BEGIN") + 1
$endLine = $stdout.IndexOf("//END") - 1
$output = $stdout[$startLine..$endLine] -join [Environment]::NewLine | ConvertFrom-Json
} else {
$output = $output -join [Environment]::NewLine
$output = $stdout -join [Environment]::NewLine
}

# dotnet commands log verbose output to stderr
Write-Verbose $($verboseOutput -join [Environment]::NewLine)
}
finally {
$ErrorActionPreference='Stop'
Expand Down