diff options
author | Miguel Costa <miguel.costa@qt.io> | 2022-03-14 15:17:28 +0100 |
---|---|---|
committer | Miguel Costa <miguel.costa@qt.io> | 2022-03-22 10:10:45 +0000 |
commit | 74912d94466fa06d4fec5af39c3c2c77823bb1a2 (patch) | |
tree | fa9184b6d16291b0e440cb7e77f6db3fda082faa | |
parent | e9670fa2c5d625825b50b34f5df5dfd07fe7561b (diff) |
Fix error when Qt path is invalid
Fixed a bug in the Qt versions options page where, if the path to a Qt
installation was incorrect (i.e. not containing qmake.exe or
bin\qmake.exe), applying changes (pressing 'OK') was allowed but the
Qt version would not be registered or would be removed. This occurred
silently; the only feedback was an exception notification in the output
window. However, this was a generic system exception (Argument exception
in System.IO.Path.Combine()) which did not provide any information about
the cause of the error.
Pressing 'OK' on the versions page will now trigger a validation of the
Qt installation path, and will only proceed if the path is valid.
Fixes: QTVSADDINBUG-973
Change-Id: Ide6187bab1a0616d785d8348cbbd436ff771d8e1
Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
-rw-r--r-- | QtVsTools.Package/Options/QtVersionsPage.cs | 11 | ||||
-rw-r--r-- | QtVsTools.Package/Options/QtVersionsTable.cs | 38 |
2 files changed, 37 insertions, 12 deletions
diff --git a/QtVsTools.Package/Options/QtVersionsPage.cs b/QtVsTools.Package/Options/QtVersionsPage.cs index 551fb334..df612a7b 100644 --- a/QtVsTools.Package/Options/QtVersionsPage.cs +++ b/QtVsTools.Package/Options/QtVersionsPage.cs @@ -28,6 +28,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Windows; using Microsoft.VisualStudio.Shell; @@ -109,14 +110,20 @@ namespace QtVsTools.Options try { if (version.Host == BuildHost.Windows) { if (version.State.HasFlag((State)Column.Path)) { - var versionInfo = VersionInformation.Get(version.Path); + var versionPath = version.Path; + var ignoreCase = StringComparison.CurrentCultureIgnoreCase; + if (Path.GetFileName(versionPath).Equals("qmake.exe", ignoreCase)) + versionPath = Path.GetDirectoryName(versionPath); + if (Path.GetFileName(versionPath).Equals("bin", ignoreCase)) + versionPath = Path.GetDirectoryName(versionPath); + var versionInfo = VersionInformation.Get(versionPath); var generator = versionInfo.GetQMakeConfEntry("MAKEFILE_GENERATOR"); if (generator != "MSVC.NET" && generator != "MSBUILD") throw new Exception(string.Format( "This Qt version uses an unsupported makefile generator (used: " + "{0}, supported: MSVC.NET, MSBUILD)", generator)); + VersionManager.SaveVersion(version.VersionName, versionPath); } - VersionManager.SaveVersion(version.VersionName, version.Path); } else { string name = version.VersionName; string access = diff --git a/QtVsTools.Package/Options/QtVersionsTable.cs b/QtVsTools.Package/Options/QtVersionsTable.cs index ddcb90d9..c9808c7c 100644 --- a/QtVsTools.Package/Options/QtVersionsTable.cs +++ b/QtVsTools.Package/Options/QtVersionsTable.cs @@ -254,13 +254,28 @@ namespace QtVsTools.Options version.FieldPath.ValidationError = "Path cannot be empty"; IsValid = false; } else if (version.Host == BuildHost.Windows) { - var path = NormalizePath(version.Path); - if (!Directory.Exists(path)) { - version.FieldPath.ValidationError = "Path does not exist"; - IsValid = false; - } else if (!File.Exists(Path.Combine(path, "qmake.exe"))) { - version.FieldPath.ValidationError = "Cannot find qmake.exe"; + string path = NormalizePath(version.Path); + if (path == null) { + version.FieldPath.ValidationError = "Invalid path format"; IsValid = false; + } else { + var possibleQMakePaths = new[] { + // Path points to qmake.exe + path, + // Path points to folder containing qmake.exe + Path.Combine(path, "qmake.exe"), + // Path points to folder containing bin\qmake.exe + Path.Combine(path, "bin", "qmake.exe"), + }; + bool qmakeExists = possibleQMakePaths + .Where(p => File.Exists(p) + && Path.GetFileName(p).Equals("qmake.exe", + StringComparison.CurrentCultureIgnoreCase)) + .Any(); + if (!qmakeExists) { + version.FieldPath.ValidationError = "Cannot find qmake.exe"; + IsValid = false; + } } } if (previousValidation != version.FieldPath.ValidationError) @@ -506,10 +521,13 @@ namespace QtVsTools.Options { if (string.IsNullOrEmpty(path)) return path; - - return Path.GetFullPath(new Uri(path).LocalPath) - .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) - .ToUpperInvariant(); + try { + return Path.GetFullPath(new Uri(path).LocalPath) + .TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar) + .ToUpperInvariant(); + } catch (UriFormatException) { + return null; + } } void Explorer_Click(object sender, RoutedEventArgs e) |