Skip to content

Commit dca2986

Browse files
committed
Merge pull request #112844 from bruvzg/cwd_error
Add error message when trying to load project from CWD.
2 parents 7ec0243 + a7358dd commit dca2986

File tree

7 files changed

+43
-2
lines changed

7 files changed

+43
-2
lines changed

core/os/os.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,10 @@ Error OS::set_cwd(const String &p_cwd) {
408408
return ERR_CANT_OPEN;
409409
}
410410

411+
String OS::get_cwd() const {
412+
return ".";
413+
}
414+
411415
Dictionary OS::get_memory_info() const {
412416
Dictionary meminfo;
413417

core/os/os.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class OS {
205205
virtual Error shell_open(const String &p_uri);
206206
virtual Error shell_show_in_file_manager(String p_path, bool p_open_folder = true);
207207
virtual Error set_cwd(const String &p_cwd);
208+
virtual String get_cwd() const;
208209

209210
virtual bool has_environment(const String &p_var) const = 0;
210211
virtual String get_environment(const String &p_var) const = 0;

drivers/unix/os_unix.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,16 @@ Error OS_Unix::set_cwd(const String &p_cwd) {
10961096
return OK;
10971097
}
10981098

1099+
String OS_Unix::get_cwd() const {
1100+
String dir;
1101+
char real_current_dir_name[2048];
1102+
ERR_FAIL_NULL_V(getcwd(real_current_dir_name, 2048), ".");
1103+
if (dir.append_utf8(real_current_dir_name) != OK) {
1104+
dir = real_current_dir_name;
1105+
}
1106+
return dir;
1107+
}
1108+
10991109
bool OS_Unix::has_environment(const String &p_var) const {
11001110
return getenv(p_var.utf8().get_data()) != nullptr;
11011111
}

drivers/unix/os_unix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ class OS_Unix : public OS {
101101
virtual Error get_dynamic_library_symbol_handle(void *p_library_handle, const String &p_name, void *&p_symbol_handle, bool p_optional = false) override;
102102

103103
virtual Error set_cwd(const String &p_cwd) override;
104+
virtual String get_cwd() const override;
104105

105106
virtual String get_name() const override;
106107
virtual String get_distribution_name() const override;

main/main.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,6 +992,7 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
992992
OS::get_singleton()->initialize();
993993

994994
#if !defined(OVERRIDE_PATH_ENABLED) && !defined(TOOLS_ENABLED)
995+
String old_cwd = OS::get_singleton()->get_cwd();
995996
#ifdef MACOS_ENABLED
996997
String new_cwd = OS::get_singleton()->get_bundle_resource_dir();
997998
if (new_cwd.is_empty() || !new_cwd.is_absolute_path()) {
@@ -2019,8 +2020,23 @@ Error Main::setup(const char *execpath, int argc, char *argv[], bool p_second_ph
20192020
#ifdef TOOLS_ENABLED
20202021
editor = false;
20212022
#else
2022-
const String error_msg = "Error: Couldn't load project data at path \"" + project_path + "\". Is the .pck file missing?\nIf you've renamed the executable, the associated .pck file should also be renamed to match the executable's name (without the extension).\n";
2023-
OS::get_singleton()->print("%s", error_msg.utf8().get_data());
2023+
String error_msg = "Error: Couldn't load project data at path \"" + (project_path == "." ? OS::get_singleton()->get_cwd() : project_path) + "\". Is the .pck file missing?\n\n";
2024+
#if !defined(OVERRIDE_PATH_ENABLED) && !defined(TOOLS_ENABLED)
2025+
String exec_path = OS::get_singleton()->get_executable_path();
2026+
String exec_basename = exec_path.get_file().get_basename();
2027+
2028+
if (FileAccess::exists(old_cwd.path_join(exec_basename + ".pck"))) {
2029+
error_msg += "\"" + exec_basename + ".pck\" was found in the current working directory. To be able to load a project from the CWD, use the `disable_path_overrides=no` SCons option when compiling Godot.\n";
2030+
} else if (FileAccess::exists(old_cwd.path_join("project.godot"))) {
2031+
error_msg += "\"project.godot\" was found in the current working directory. To be able to load a project from the CWD, use the `disable_path_overrides=no` SCons option when compiling Godot.\n";
2032+
} else {
2033+
error_msg += "If you've renamed the executable, the associated .pck file should also be renamed to match the executable's name (without the extension).\n";
2034+
}
2035+
#else
2036+
error_msg += "If you've renamed the executable, the associated .pck file should also be renamed to match the executable's name (without the extension).\n";
2037+
#endif
2038+
ERR_PRINT(error_msg);
2039+
20242040
OS::get_singleton()->alert(error_msg);
20252041

20262042
goto error;

platform/windows/os_windows.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,14 @@ Error OS_Windows::set_cwd(const String &p_cwd) {
16781678
return OK;
16791679
}
16801680

1681+
String OS_Windows::get_cwd() const {
1682+
Char16String real_current_dir_name;
1683+
size_t str_len = GetCurrentDirectoryW(0, nullptr);
1684+
real_current_dir_name.resize_uninitialized(str_len + 1);
1685+
GetCurrentDirectoryW(real_current_dir_name.size(), (LPWSTR)real_current_dir_name.ptrw());
1686+
return String::utf16((const char16_t *)real_current_dir_name.get_data()).trim_prefix(R"(\\?\)").replace_char('\\', '/');
1687+
}
1688+
16811689
Vector<String> OS_Windows::get_system_fonts() const {
16821690
if (!dwrite_init) {
16831691
return Vector<String>();

platform/windows/os_windows.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class OS_Windows : public OS {
201201
virtual double get_unix_time() const override;
202202

203203
virtual Error set_cwd(const String &p_cwd) override;
204+
virtual String get_cwd() const override;
204205

205206
virtual void add_frame_delay(bool p_can_draw, bool p_wake_for_events) override;
206207
virtual void delay_usec(uint32_t p_usec) const override;

0 commit comments

Comments
 (0)