From dd9f3c980fe2794617b0027f45647f34b9dad38c Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 15 Jan 2024 17:50:48 +0000 Subject: [PATCH] HackStudio: Absolutize project paths before opening them Relative paths cause issues in a couple of ways: - `open_project()` sets the working directory to that path, and then opens a project at that same path. This means opening `./foo` goes to `./foo`, and then tries to open `./foo/foo`. - Even with that rearranged, we would then have issues with trying to open files, because again we would try to open `./foo/foo/file` instead of `./foo/file`. - The relative path would get saved in "Recent Projects" which is wrong. Absolutizing the path before using it means we avoid these issues, and without having to rearchitect everything. :^) --- Userland/DevTools/HackStudio/HackStudioWidget.cpp | 11 ++++++----- Userland/DevTools/HackStudio/Project.cpp | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index c559de71c7..392ea37951 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -239,14 +239,15 @@ void HackStudioWidget::open_project(ByteString const& root_path) { if (warn_unsaved_changes("There are unsaved changes, do you want to save before closing current project?") == ContinueDecision::No) return; - if (auto result = Core::System::chdir(root_path); result.is_error()) { + auto absolute_root_path = FileSystem::absolute_path(root_path).release_value_but_fixme_should_propagate_errors(); + if (auto result = Core::System::chdir(absolute_root_path); result.is_error()) { warnln("Failed to open project: {}", result.release_error()); exit(1); } if (m_project) { close_current_project(); } - m_project = Project::open_with_root_path(root_path); + m_project = Project::open_with_root_path(absolute_root_path); VERIFY(m_project); m_project_builder = make(*m_terminal_wrapper, *m_project); if (m_project_tree_view) { @@ -254,7 +255,7 @@ void HackStudioWidget::open_project(ByteString const& root_path) m_project_tree_view->update(); } if (m_git_widget->initialized()) { - m_git_widget->change_repo(root_path); + m_git_widget->change_repo(absolute_root_path); m_git_widget->refresh(); } if (Debugger::is_initialized()) { @@ -275,8 +276,8 @@ void HackStudioWidget::open_project(ByteString const& root_path) }; auto recent_projects = read_recent_projects(); - recent_projects.remove_all_matching([&](auto& p) { return p == root_path; }); - recent_projects.insert(0, root_path); + recent_projects.remove_all_matching([&](auto& p) { return p == absolute_root_path; }); + recent_projects.insert(0, absolute_root_path); if (recent_projects.size() > recent_projects_history_size) recent_projects.shrink(recent_projects_history_size); diff --git a/Userland/DevTools/HackStudio/Project.cpp b/Userland/DevTools/HackStudio/Project.cpp index 3bde5faa80..0611576228 100644 --- a/Userland/DevTools/HackStudio/Project.cpp +++ b/Userland/DevTools/HackStudio/Project.cpp @@ -18,6 +18,7 @@ Project::Project(ByteString const& root_path) OwnPtr Project::open_with_root_path(ByteString const& root_path) { + VERIFY(LexicalPath(root_path).is_absolute()); if (!FileSystem::is_directory(root_path)) return {}; return adopt_own(*new Project(root_path));