Skip to content

Commit 6ec64b3

Browse files
authored
[engine] Support UTF-8 paths in Windows filesystem and path APIs (flutter#191360)
This change fixes hot reload failures and path resolution issues on Windows when project or temporary directory paths contain UTF-8 characters with non-ASCII code points (e.g. Japanese, Chinese, accented characters). ### Root Cause 1. During hot reload, the VM reads `.dill` files using `tonic::filesystem::ReadFileToBytes` in `file.cc`. On Windows, standard C library `open()` was used, which expects ANSI-encoded paths. 2. If the path contained UTF-8 characters not representable in the active ANSI code page, `open()` failed. 3. This failure triggered a fallback to compile from source via `CompileToKernel`, which attempted to start the Kernel Isolate. Because the Kernel Isolate is disabled in Flutter (`start_kernel_isolate = false`), it failed with the misleading error: `Error while starting Kernel isolate task`. 4. Related path APIs in `tonic/filesystem` (`AbsolutePath`, `GetAbsoluteFilePath`) and `fml/paths` (`AbsolutePath`) similarly used ANSI Win32 APIs (`_fullpath`, `CreateFileA`, `GetFinalPathNameByHandleA`). ### Solution 1. Added shared UTF-8 / UTF-16 conversion utilities (`Utf8ToWide`, `WideToUtf8`) in `tonic/filesystem/filesystem/windows_utils.h`. 2. Updated `tonic::filesystem::ReadFileToString` and `ReadFileToBytes` to use `_wopen` with wide strings on Windows. 3. Updated `tonic::filesystem::AbsolutePath` and `GetAbsoluteFilePath` to use `_wfullpath`, `CreateFileW`, and `GetFinalPathNameByHandleW`. 4. Updated `fml::paths::AbsolutePath` to use `fml::Utf8ToWideString`, `_wfullpath`, and `fml::WideStringToUtf8`. 5. Added an integration test `packages/flutter_tools/test/integration.shard/hot_reload_non_ascii_path_test.dart` verifying that hot reload succeeds when the project is in a directory containing non-ASCII characters (`試験`). Fixes flutter#64758 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent c842d69 commit 6ec64b3

6 files changed

Lines changed: 159 additions & 16 deletions

File tree

‎engine/src/flutter/fml/platform/win/paths_win.cc‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#include <windows.h>
88

99
#include <algorithm>
10+
#include <filesystem>
1011

11-
#include "flutter/fml/paths.h"
12+
#include "flutter/fml/logging.h"
1213
#include "flutter/fml/platform/win/wstring_conversion.h"
1314

1415
namespace fml {
@@ -55,8 +56,8 @@ size_t LastSeparator(const std::string& path) {
5556
} // namespace
5657

5758
std::pair<bool, std::string> GetExecutablePath() {
58-
HMODULE module = GetModuleHandle(NULL);
59-
if (module == NULL) {
59+
HMODULE module = GetModuleHandle(nullptr);
60+
if (module == nullptr) {
6061
return {false, ""};
6162
}
6263
wchar_t path[MAX_PATH];
@@ -68,9 +69,19 @@ std::pair<bool, std::string> GetExecutablePath() {
6869
}
6970

7071
std::string AbsolutePath(const std::string& path) {
71-
char absPath[MAX_PATH];
72-
_fullpath(absPath, path.c_str(), MAX_PATH);
73-
return std::string(absPath);
72+
std::error_code ec;
73+
std::filesystem::path p(std::u8string_view(
74+
reinterpret_cast<const char8_t*>(path.data()), path.size()));
75+
std::filesystem::path abs_path = std::filesystem::absolute(p, ec);
76+
if (ec) {
77+
FML_DLOG(ERROR) << "Failed to resolve absolute path for '" << path
78+
<< "': " << ec.message();
79+
return std::string();
80+
}
81+
abs_path = abs_path.lexically_normal();
82+
std::u8string u8_str = abs_path.u8string();
83+
return std::string(reinterpret_cast<const char*>(u8_str.data()),
84+
u8_str.size());
7485
}
7586

7687
std::string GetDirectoryName(const std::string& path) {

‎engine/src/flutter/third_party/tonic/BUILD.gn‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ source_set("tonic") {
7575
sources += [
7676
"file_loader/file_loader_win.cc",
7777
"filesystem/filesystem/path_win.cc",
78+
"filesystem/filesystem/windows_utils.h",
7879
]
7980
} else if (is_fuchsia) {
8081
sources += [

‎engine/src/flutter/third_party/tonic/filesystem/filesystem/file.cc‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
#if defined(OS_WIN)
2323
#include <BaseTsd.h>
24+
#include <io.h>
25+
#include <windows.h>
26+
27+
#include "tonic/filesystem/filesystem/windows_utils.h"
2428
typedef SSIZE_T ssize_t;
2529
#endif
2630

@@ -84,7 +88,12 @@ std::pair<uint8_t*, intptr_t> ReadFileDescriptorToBytes(int fd) {
8488
}
8589

8690
bool ReadFileToString(const std::string& path, std::string* result) {
91+
#if defined(OS_WIN)
92+
std::wstring wide_path = Utf8PathToWide(path);
93+
Descriptor fd(_wopen(wide_path.c_str(), O_RDONLY));
94+
#else
8795
Descriptor fd(open(path.c_str(), O_RDONLY));
96+
#endif
8897
return ReadFileDescriptor(fd.get(), result);
8998
}
9099

@@ -94,7 +103,12 @@ bool ReadFileDescriptorToString(int fd, std::string* result) {
94103

95104
std::pair<uint8_t*, intptr_t> ReadFileToBytes(const std::string& path) {
96105
std::pair<uint8_t*, intptr_t> failure_pair{nullptr, -1};
106+
#if defined(OS_WIN)
107+
std::wstring wide_path = Utf8PathToWide(path);
108+
Descriptor fd(_wopen(wide_path.c_str(), O_RDONLY | BINARY_MODE));
109+
#else
97110
Descriptor fd(open(path.c_str(), O_RDONLY | BINARY_MODE));
111+
#endif
98112
if (!fd.is_valid())
99113
return failure_pair;
100114
return ReadFileDescriptorToBytes(fd.get());

‎engine/src/flutter/third_party/tonic/filesystem/filesystem/path_win.cc‎

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
#include <algorithm>
1515
#include <cerrno>
1616
#include <cstring>
17+
#include <filesystem>
1718
#include <functional>
1819
#include <list>
1920
#include <memory>
21+
#include <string_view>
22+
23+
#include "tonic/common/log.h"
24+
#include "tonic/filesystem/filesystem/windows_utils.h"
2025

2126
namespace filesystem {
2227
namespace {
@@ -181,9 +186,19 @@ std::string SimplifyPath(std::string path) {
181186
}
182187

183188
std::string AbsolutePath(const std::string& path) {
184-
char absPath[MAX_PATH];
185-
_fullpath(absPath, path.c_str(), MAX_PATH);
186-
return std::string(absPath);
189+
std::error_code ec;
190+
std::filesystem::path p(std::u8string_view(
191+
reinterpret_cast<const char8_t*>(path.data()), path.size()));
192+
std::filesystem::path abs_path = std::filesystem::absolute(p, ec);
193+
if (ec) {
194+
tonic::Log("Failed to resolve absolute path for '%s': %s", path.c_str(),
195+
ec.message().c_str());
196+
return std::string();
197+
}
198+
abs_path = abs_path.lexically_normal();
199+
std::u8string u8_str = abs_path.u8string();
200+
return std::string(reinterpret_cast<const char*>(u8_str.data()),
201+
u8_str.size());
187202
}
188203

189204
std::string GetDirectoryName(const std::string& path) {
@@ -204,17 +219,30 @@ std::string GetBaseName(const std::string& path) {
204219
}
205220

206221
std::string GetAbsoluteFilePath(const std::string& path) {
222+
std::wstring wide_path = Utf8PathToWide(path);
207223
HANDLE file =
208-
CreateFileA(path.c_str(), FILE_READ_ATTRIBUTES,
224+
CreateFileW(wide_path.c_str(), FILE_READ_ATTRIBUTES,
209225
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
210226
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
211227
if (file == INVALID_HANDLE_VALUE) {
212228
return std::string();
213229
}
214-
char buffer[MAX_PATH];
230+
wchar_t buffer[MAX_PATH];
215231
DWORD ret =
216-
GetFinalPathNameByHandleA(file, buffer, MAX_PATH, FILE_NAME_NORMALIZED);
217-
if (ret == 0 || ret > MAX_PATH) {
232+
GetFinalPathNameByHandleW(file, buffer, MAX_PATH, FILE_NAME_NORMALIZED);
233+
std::wstring wide_result;
234+
if (ret > 0 && ret < MAX_PATH) {
235+
wide_result.assign(buffer, ret);
236+
} else if (ret >= MAX_PATH) {
237+
std::vector<wchar_t> dyn_buffer(ret);
238+
DWORD dyn_ret = GetFinalPathNameByHandleW(file, dyn_buffer.data(), ret,
239+
FILE_NAME_NORMALIZED);
240+
if (dyn_ret > 0 && dyn_ret < ret) {
241+
wide_result.assign(dyn_buffer.data(), dyn_ret);
242+
}
243+
}
244+
245+
if (wide_result.empty()) {
218246
std::string result;
219247
if (GetLastError() == ERROR_ACCESS_DENIED) {
220248
// In sandboxed apps, GetFinalPathNameByHandle requires the app to
@@ -231,10 +259,21 @@ std::string GetAbsoluteFilePath(const std::string& path) {
231259
CloseHandle(file);
232260
return result;
233261
}
234-
std::string result(buffer);
235-
result.erase(0, strlen("\\\\?\\"));
262+
// GetFinalPathNameByHandleW with FILE_NAME_NORMALIZED returns paths with
263+
// either a "\\?\UNC\server\share" prefix for network shares or a "\\?\"
264+
// prefix for local drive paths (e.g. "\\?\C:\..."). We normalize network
265+
// paths to standard UNC format ("\\server\share") and strip the "\\?\" prefix
266+
// for standard local paths.
267+
constexpr std::wstring_view kUncPrefix = L"\\\\?\\UNC\\";
268+
constexpr std::wstring_view kLongPathPrefix = L"\\\\?\\";
269+
if (wide_result.starts_with(kUncPrefix)) {
270+
wide_result.erase(0, kUncPrefix.size());
271+
wide_result = L"\\\\" + wide_result;
272+
} else if (wide_result.starts_with(kLongPathPrefix)) {
273+
wide_result.erase(0, kLongPathPrefix.size());
274+
}
236275
CloseHandle(file);
237-
return result;
276+
return WidePathToUtf8(wide_result);
238277
}
239278

240279
} // namespace filesystem
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FILESYSTEM_WINDOWS_UTILS_H_
6+
#define FILESYSTEM_WINDOWS_UTILS_H_
7+
8+
#include "tonic/common/build_config.h"
9+
10+
#if defined(OS_WIN)
11+
#include <filesystem>
12+
#include <string>
13+
#include <string_view>
14+
15+
namespace filesystem {
16+
17+
inline std::wstring Utf8PathToWide(std::string_view utf8_path) {
18+
if (utf8_path.empty()) {
19+
return std::wstring();
20+
}
21+
const char8_t* u8_data = reinterpret_cast<const char8_t*>(utf8_path.data());
22+
std::u8string_view u8_view(u8_data, utf8_path.size());
23+
return std::filesystem::path(u8_view).wstring();
24+
}
25+
26+
inline std::string WidePathToUtf8(std::wstring_view wide_path) {
27+
if (wide_path.empty()) {
28+
return std::string();
29+
}
30+
std::u8string u8_str = std::filesystem::path(wide_path).u8string();
31+
return std::string(reinterpret_cast<const char*>(u8_str.data()),
32+
u8_str.size());
33+
}
34+
35+
} // namespace filesystem
36+
37+
#endif // defined(OS_WIN)
38+
39+
#endif // FILESYSTEM_WINDOWS_UTILS_H_
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
@Tags(<String>['flutter-test-driver'])
6+
library;
7+
8+
import 'package:file/file.dart';
9+
10+
import '../src/common.dart';
11+
import 'test_data/hot_reload_project.dart';
12+
import 'test_driver.dart';
13+
import 'test_utils.dart';
14+
15+
void main() {
16+
group('Hot Reload with non-ASCII path', () {
17+
late Directory tempDir;
18+
final project = HotReloadProject();
19+
late FlutterRunTestDriver flutter;
20+
21+
setUp(() async {
22+
// Use Japanese characters "試験" (test) in the temp directory name.
23+
tempDir = createResolvedTempDirectorySync('hot_reload_test_試験.');
24+
await project.setUpIn(tempDir);
25+
flutter = FlutterRunTestDriver(tempDir);
26+
});
27+
28+
tearDown(() async {
29+
await flutter.stop();
30+
tryToDelete(tempDir);
31+
});
32+
33+
testWithoutContext('hot reload works', () async {
34+
await flutter.run();
35+
// Hot reload works without error.
36+
await flutter.hotReload();
37+
});
38+
});
39+
}

0 commit comments

Comments
 (0)