Skip to content

Target folder is not created, even with -Force parameter. Git.ps1#99

Open
rcarboneras wants to merge 2 commits intoPowerShellOrg:masterfrom
rcarboneras:master
Open

Target folder is not created, even with -Force parameter. Git.ps1#99
rcarboneras wants to merge 2 commits intoPowerShellOrg:masterfrom
rcarboneras:master

Conversation

@rcarboneras
Copy link
Copy Markdown

When using a Git repository, if target doesn't exist, the dependency is downloaded in the current directory and the error is silenced, affecting the path sctructure of your proyect. Unless you check the $Errors variable, you'll never now what is happening.

However, when using PowerShell Gallery, the bellow piece of code does the work in the Save-Module function:
When -Force is specified, Path will be created if not available

    # When -Force is specified, Path will be created if not available.
    if(-not $Force)
    {
        if($Path)
        {
            $destinationPath = Resolve-PathHelper -Path $Path -CallerPSCmdlet $PSCmdlet | Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore

            if(-not $destinationPath -or -not (Microsoft.PowerShell.Management\Test-path $destinationPath))
            {
                $errorMessage = ($LocalizedData.PathNotFound -f $Path)
                ThrowError  -ExceptionName "System.ArgumentException" `
                            -ExceptionMessage $errorMessage `
                            -ErrorId "PathNotFound" `
                            -CallerPSCmdlet $PSCmdlet `
                            -ExceptionObject $Path `
                            -ErrorCategory InvalidArgument
            }

            $PSBoundParameters['Path'] = $destinationPath
        }
        else
        {
            $destinationPath = Resolve-PathHelper -Path $LiteralPath -IsLiteralPath -CallerPSCmdlet $PSCmdlet | Microsoft.PowerShell.Utility\Select-Object -First 1 -ErrorAction Ignore

            if(-not $destinationPath -or -not (Microsoft.PowerShell.Management\Test-Path -LiteralPath $destinationPath))
            {
                $errorMessage = ($LocalizedData.PathNotFound -f $LiteralPath)
                ThrowError  -ExceptionName "System.ArgumentException" `
                            -ExceptionMessage $errorMessage `
                            -ErrorId "PathNotFound" `
                            -CallerPSCmdlet $PSCmdlet `
                            -ExceptionObject $LiteralPath `
                            -ErrorCategory InvalidArgument
            }

            $PSBoundParameters['LiteralPath'] = $destinationPath
        }
    }

I propose then to implement the same behaviour in Git.ps1 script with the following lines:

else
{
if ($Force) {New-Item -ItemType Directory -Name $Dependency.Target -Force | Out-Null
Write-Debug "Target folder $($Dependency.Target) created as -Force switch was specified"
$Target = Join-Path $PWD "$($Dependency.Target)"
}
else {
$Target = $PWD.Path
Write-Debug "Target defaulted to current dir: $Target"
}
}

@rcarboneras rcarboneras changed the title Update Git.ps1 Target folder is not created, even with -Force parameter. Git.ps1 May 8, 2019
@rcarboneras
Copy link
Copy Markdown
Author

I've made a minor change in the way of creating the folder:

else
{
if ($true) {New-Item -ItemType Directory -Name (Split-Path $Dependency.Target -Leaf) -Force | Out-Null
Write-Debug "Target folder $($Dependency.Target) created as -Force switch was specified"
$Target = Join-Path $PWD "$(Split-Path $Dependency.Target -Leaf)"
}

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the Git dependency script to better handle cases where Dependency.Target does not exist, aiming to honor -Force by creating the target folder instead of silently cloning into the current directory.

Changes:

  • Adds a -Force code path intended to create the target folder when Dependency.Target cannot be resolved.
  • Adjusts how $Target is computed in the “target missing/unresolvable” branch.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +96 to +104
if ($Force) {
New-Item -ItemType Directory -Name (Split-Path $Dependency.Target -Leaf) -Force | Out-Null
Write-Debug "Target folder $($Dependency.Target) created as -Force switch was specified"
$Target = Join-Path $PWD "$(Split-Path $Dependency.Target -Leaf)"
}
else {
$Target = $PWD.Path
Write-Debug "Target defaulted to current dir: $Target"
}
Comment on lines +96 to +104
if ($Force) {
New-Item -ItemType Directory -Name (Split-Path $Dependency.Target -Leaf) -Force | Out-Null
Write-Debug "Target folder $($Dependency.Target) created as -Force switch was specified"
$Target = Join-Path $PWD "$(Split-Path $Dependency.Target -Leaf)"
}
else {
$Target = $PWD.Path
Write-Debug "Target defaulted to current dir: $Target"
}
Comment on lines +96 to +104
if ($Force) {
New-Item -ItemType Directory -Name (Split-Path $Dependency.Target -Leaf) -Force | Out-Null
Write-Debug "Target folder $($Dependency.Target) created as -Force switch was specified"
$Target = Join-Path $PWD "$(Split-Path $Dependency.Target -Leaf)"
}
else {
$Target = $PWD.Path
Write-Debug "Target defaulted to current dir: $Target"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants