Fix tracking of paths with trailing spaces on Windows #3586
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
If you install
MrScienceMeistersNuclearWarfare
(latest version is for KSP 1.0.5) on Windows and then try to uninstall it, you get this error:Cause
The path can't be found because the
installed_files
entry inregistry.json
doesn't match the path on disk.ModuleInstaller
assumes thatDirectory.CreateDirectory
andFile.Create
will either create the paths they're given or fail. Astoudingly, this assumption is false on Windows, a purportedly real professional operating system that is sold to users across the world! Instead, both of these functions silently trim trailing spaces from paths. This code:... creates a directory called
test path
with no trailing spaces. This happens silently; there's no exception or other explicit indication that something unexpected has happened. If the application code needs to know the path, it has to know about this potential problem (despite it not being particularly clearly documented) and implement a workaround for it.Two directories in
MrScienceMeistersNuclearWarfare
end with trailing spaces. So when we uninstall this module, we try to remove the path with the trailing space, but Windows doesn't find it because it's not there, having been replaced by a different path during installation. 🤦Changes
ModuleInstaller.CopyZipEntry
returns the path it createdTxFileManager.CreateDirectory
function we use has no return value, butPath.GetDirectoryName
apparently performs the trimming logic, so we usePath.GetDirectoryName(Path.Combine(fullPath, "DUMMY"))
File.Create
returns aFileStream
object with aName
property that has itModuleInstaller.InstallModule
collects the created paths fromCopyZipEntry
and returns them instead of simply assuming that everyInstallableFile.destination
will be created as requestedThis will ensure that the entries in
registry.json
always match the real paths on disk, so uninstallation will no longer be tripped up by this Windows quirk.Fixes #3585.