Skip to content

Commit e25d71b

Browse files
Ensure that the engine converts std::filesystem::path objects to UTF-8 strings on Windows (flutter#179528)
Fixes flutter#178896 --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 8fe7618 commit e25d71b

File tree

6 files changed

+22
-6
lines changed

6 files changed

+22
-6
lines changed

engine/src/flutter/fml/string_conversion.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ std::u16string Utf8ToUtf16(const std::string_view string) {
4444
return converter.from_bytes(string.data());
4545
}
4646

47+
std::string PathToUtf8(const std::filesystem::path& path) {
48+
const std::u8string path_u8 = path.u8string();
49+
return std::string(path_u8.begin(), path_u8.end());
50+
}
51+
4752
} // namespace fml

engine/src/flutter/fml/string_conversion.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef FLUTTER_FML_STRING_CONVERSION_H_
66
#define FLUTTER_FML_STRING_CONVERSION_H_
77

8+
#include <filesystem>
89
#include <string>
910
#include <vector>
1011

@@ -19,6 +20,9 @@ std::string Utf16ToUtf8(const std::u16string_view string);
1920
// Returns a UTF-16 encoded equivalent of a UTF-8 encoded input string.
2021
std::u16string Utf8ToUtf16(const std::string_view string);
2122

23+
// Returns the pathname encoded in UTF-8.
24+
std::string PathToUtf8(const std::filesystem::path& path);
25+
2226
} // namespace fml
2327

2428
#endif // FLUTTER_FML_STRING_CONVERSION_H_

engine/src/flutter/fml/string_conversion_unittests.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,10 @@ TEST(StringConversion, Utf16ToUtf8Unicode) {
3333
EXPECT_EQ(Utf16ToUtf8(u"\x2603"), "\xe2\x98\x83");
3434
}
3535

36+
TEST(StringConversion, PathToUtf8) {
37+
EXPECT_EQ(PathToUtf8(std::filesystem::path("abc")), "abc");
38+
EXPECT_EQ(PathToUtf8(std::filesystem::path(u"\x2603")), "\xe2\x98\x83");
39+
}
40+
3641
} // namespace testing
3742
} // namespace fml

engine/src/flutter/shell/platform/windows/flutter_project_bundle.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <filesystem>
88

99
#include "flutter/fml/logging.h"
10+
#include "flutter/fml/string_conversion.h"
1011
#include "flutter/shell/platform/common/engine_switches.h" // nogncheck
1112
#include "flutter/shell/platform/common/path_utils.h"
1213

@@ -73,7 +74,7 @@ UniqueAotDataPtr FlutterProjectBundle::LoadAotData(
7374
<< "; no such file.";
7475
return UniqueAotDataPtr(nullptr, nullptr);
7576
}
76-
std::string path_string = aot_library_path_.string();
77+
std::string path_string = fml::PathToUtf8(aot_library_path_);
7778
FlutterEngineAOTDataSource source = {};
7879
source.type = kFlutterEngineAOTDataSourceTypeElfPath;
7980
source.elf_path = path_string.c_str();

engine/src/flutter/shell/platform/windows/flutter_windows_engine.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ bool FlutterWindowsEngine::Run(std::string_view entrypoint) {
274274
FML_LOG(ERROR) << "Missing or unresolvable paths to assets.";
275275
return false;
276276
}
277-
std::string assets_path_string = project_->assets_path().string();
278-
std::string icu_path_string = project_->icu_path().string();
277+
std::string assets_path_string = fml::PathToUtf8(project_->assets_path());
278+
std::string icu_path_string = fml::PathToUtf8(project_->icu_path());
279279
if (embedder_api_.RunsAOTCompiledDartCode()) {
280280
aot_data_ = project_->LoadAotData(embedder_api_);
281281
if (!aot_data_) {

engine/src/flutter/tools/templater/templater_main.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flutter/fml/file.h"
1313
#include "flutter/fml/logging.h"
1414
#include "flutter/fml/mapping.h"
15+
#include "flutter/fml/string_conversion.h"
1516
#include "inja/inja.hpp"
1617

1718
namespace flutter {
@@ -50,9 +51,9 @@ bool TemplaterMain(const fml::CommandLine& command_line) {
5051
reinterpret_cast<const uint8_t*>(rendered_template.data()),
5152
rendered_template.size()};
5253

53-
auto current_dir =
54-
fml::OpenDirectory(std::filesystem::current_path().string().c_str(),
55-
false, fml::FilePermission::kReadWrite);
54+
auto current_dir = fml::OpenDirectory(
55+
fml::PathToUtf8(std::filesystem::current_path()).c_str(), false,
56+
fml::FilePermission::kReadWrite);
5657
if (!current_dir.is_valid()) {
5758
FML_LOG(ERROR) << "Could not open current directory.";
5859
return false;

0 commit comments

Comments
 (0)