[CLOSED] ForceDirectories anomaly lazarus / 64-FPC development ---------------------------------------------------------------------- lagprogramming · 2023-05-09 10:55 rtl/objpas/sysutils/disk.inc has function ForceDirectories(Const Dir: PathStr): Boolean; https://gitlab.com/freepascal.org/fpc/source/- /blob/main/rtl/objpas/sysutils/disk.inc The function might return true, false or raise an exception. Raising an exception is counterintuitive. The stable documentation writes Quote76.15.128 ForceDirectories Synopsis: Create a chain of directories Declaration: function ForceDirectories(const Dir: RawByteString) : Boolean function ForceDirectories(const Dir: UnicodeString) : Boolean Visibility: default Description: ForceDirectories tries to create any missing directories in Dir till the whole path in Dir exists. It returns True if Dir already existed or was created successfully. If it failed to create any of the parts, False is returned. The development documentation has been modified for this function but it avoids this anomaly. This mix of exceptions and boolean results is prone to easily insert bugs in code. ---------------------------------------------------------------------- PascalDragon · 2023-05-10 20:45 This exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there. ---------------------------------------------------------------------- AlexTP · 2023-05-11 20:21 No report in the bugtracker is needed, per PascalDragon. ---------------------------------------------------------------------- lagprogramming · 2023-05-19 14:48 Quote from: PascalDragon on May 10, 2023, 10:45:46 pmThis exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there. mkdir(''); doesn't raise an exception. directoryexists('',true); returns false, it doesn't raise an exception. I have doubts ordinary programmers enclose forcedirectories in try...except/finally blocks. It's counterintuitive. Anyway, because Delphi has this behavior, I have doubts Fpc and Lazarus would change the existing code. ---------------------------------------------------------------------- PascalDragon · 2023-05-21 11:18 Quote from: lagprogramming on May 19, 2023, 04:48:52 pmQuote from: PascalDragon on May 10, 2023, 10:45:46 pmThis exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there. mkdir(''); doesn't raise an exception. ForceDirectories is not an equivalent to MkDir as that requires the parent directories to already exist. Quote from: lagprogramming on May 19, 2023, 04:48:52 pmI have doubts ordinary programmers enclose forcedirectories in try...except/finally blocks. It's counterintuitive. Programmers will also very likely not pass empty strings to ForceDirectories which is the only case where that function raises an exception. Quote from: lagprogramming on May 19, 2023, 04:48:52 pmAnyway, because Delphi has this behavior, I have doubts Fpc and Lazarus would change the existing code. Correct. This will not change. ---------------------------------------------------------------------- lagprogramming · 2023-05-21 15:18 Quote from: PascalDragon on May 21, 2023, 01:18:40 pmQuote from: lagprogramming on May 19, 2023, 04:48:52 pmQuote from: PascalDragon on May 10, 2023, 10:45:46 pmThis exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there. mkdir(''); doesn't raise an exception. ForceDirectories is not an equivalent to MkDir as that requires the parent directories to already exist. Quote from: lagprogramming on May 19, 2023, 04:48:52 pmI have doubts ordinary programmers enclose forcedirectories in try...except/finally blocks. It's counterintuitive. Programmers will also very likely not pass empty strings to ForceDirectories which is the only case where that function raises an exception. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SetCurrentDir('/tmp/');ForceDirectories('subdir');The above line creates a subdirectory in an existing '/tmp/' directory, same as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i chdir('/tmp /');mkdir('subdir');//orSetCurrentDir('/tmp/');CreateDir('subdir');So, same as mkdir and CreateDir, ForceDirectories supports relative paths, too. After a chdir/SetCurrentDir to an existing directory, CreateDir('') always returns true while ForceDirectories('') always raises an exception. Both CreateDir and ForceDirectories are in the same rtl/objpas/sysutils/disk.inc file. I would expect ForceDirectories('') to return the same boolean result as CreateDir(''), or both of them to raise the same SCannotCreateEmptyDir exception. ---------------------------------------------------------------------- Bart · 2023-05-21 20:13 As pointed out: this is for Delphi compatibility. I agree: a function that return a boolean should not raise an exception to report a special kind of error, but thak the Greek for that. Bart ---------------------------------------------------------------------- PascalDragon · 2023-05-22 20:31 Quote from: lagprogramming on May 21, 2023, 05:18:19 pmQuote from: PascalDragon on May 21, 2023, 01:18:40 pmQuote from: lagprogramming on May 19, 2023, 04:48:52 pmQuote from: PascalDragon on May 10, 2023, 10:45:46 pmThis exception is only raised if the Dir parameter is empty which is essentially an invalid argument for the function, after all it makes no sense to ensure that no path exists. Delphi behaves the same here, so nothing will be changed there. mkdir(''); doesn't raise an exception. ForceDirectories is not an equivalent to MkDir as that requires the parent directories to already exist. Quote from: lagprogramming on May 19, 2023, 04:48:52 pmI have doubts ordinary programmers enclose forcedirectories in try...except/finally blocks. It's counterintuitive. Programmers will also very likely not pass empty strings to ForceDirectories which is the only case where that function raises an exception. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i SetCurrentDir('/tmp/');ForceDirectories('subdir');The above line creates a subdirectory in an existing '/tmp/' directory, same as Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i chdir('/tmp /');mkdir('subdir');//orSetCurrentDir('/tmp/');CreateDir('subdir');So, same as mkdir and CreateDir, ForceDirectories supports relative paths, too. After a chdir/SetCurrentDir to an existing directory, CreateDir('') always returns true while ForceDirectories('') always raises an exception. Both CreateDir and ForceDirectories are in the same rtl/objpas/sysutils/disk.inc file. I would expect ForceDirectories('') to return the same boolean result as CreateDir(''), or both of them to raise the same SCannotCreateEmptyDir exception. And ForceDirectories('/some/path/to/some/subdir') will also create any missing directory from some to subdir which CreateDir will not do. They are different functions with different usecases and for ForceDirectories it was decided - by the Delphi developers - that an empty directory is an invalid parameter which is different from the function simply having failed.