Skip to content

Commit 7888196

Browse files
authored
Add specific error message that creating Junctions requires absolute path (#19409)
1 parent d314b0b commit 7888196

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,13 @@ protected override void NewItem(
24342434

24352435
bool exists = false;
24362436

2437+
// junctions require an absolute path
2438+
if (!Path.IsPathRooted(strTargetPath))
2439+
{
2440+
WriteError(new ErrorRecord(new ArgumentException(FileSystemProviderStrings.JunctionAbsolutePath), "NotAbsolutePath", ErrorCategory.InvalidArgument, strTargetPath));
2441+
return;
2442+
}
2443+
24372444
try
24382445
{
24392446
exists = GetFileSystemInfo(strTargetPath, out isDirectory) != null;

src/System.Management.Automation/resources/FileSystemProviderStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,7 @@
345345
<data name="CopyingLocalBytesStatus" xml:space="preserve">
346346
<value>{0} of {1} ({2:0.0} MB/s)</value>
347347
</data>
348+
<data name="JunctionAbsolutePath" xml:space="preserve">
349+
<value>Creating a junction requires an absolute path for the target.</value>
350+
</data>
348351
</root>

test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,17 @@ Describe "Hard link and symbolic link tests" -Tags "CI", "RequireAdminOnWindows"
613613
Test-Path $junctionToDir | Should -BeTrue
614614
}
615615

616+
It 'New-Item fails creating junction with relative path' -Skip:(!$IsWindows) {
617+
try {
618+
Push-Location $TestDrive
619+
1 > 1.txt
620+
{ New-Item -ItemType Junction -Path 2.txt -Target 1.txt -ErrorAction Stop } | Should -Throw -ErrorId "NotAbsolutePath,Microsoft.PowerShell.Commands.NewItemCommand"
621+
}
622+
finally {
623+
Pop-Location
624+
}
625+
}
626+
616627
It 'New-Item can create hardlink with relative path' {
617628
try {
618629
Push-Location $TestDrive

0 commit comments

Comments
 (0)