Skip to content

Commit bed1101

Browse files
dcharkesCommit Queue
authored andcommitted
[native assets] Propagate OS error on invocation via PATHEXT
Native assets resolution will now fail with an error message similar to: ``` Invalid argument(s): Couldn't resolve native function '<...>' in '<...>' : Failed to canonicalize the script uri '<path without extension>'. OS error: 'The system cannot find the file specified. ``` Supporting PATHEXT will be done in a follow up CL. TEST=pkg/dartdev/test/native_assets/build_test.dart Bug: #60946 Change-Id: I7db07b6c310ca8badd509c1d5d0595f671f1062d Cq-Include-Trybots: luci.dart.try:pkg-win-release-arm64-try,pkg-win-release-try Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/435162 Reviewed-by: Michael Goderbauer <[email protected]> Commit-Queue: Daco Harkes <[email protected]>
1 parent 753ec34 commit bed1101

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

pkg/dartdev/test/native_assets/build_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ void main([List<String> args = const []]) async {
6666
final link = Link.fromUri(
6767
tempUri.resolve(OS.current.executableFileName('my_link')));
6868
await link.create(absoluteExeUri.toFilePath());
69+
if (OS.current == OS.windows) {
70+
for (final exeUri in [
71+
removeDotExe(link.uri),
72+
removeDotExe(absoluteExeUri),
73+
removeDotExe(relativeExeUri),
74+
]) {
75+
final result = await runProcess(
76+
executable: exeUri,
77+
arguments: [],
78+
workingDirectory: dartAppUri,
79+
logger: logger,
80+
);
81+
// TODO(https://dartbug.com/60946): Support PATHEXT.
82+
expect(result.exitCode, isNot(0));
83+
expect(
84+
result.stderr,
85+
stringContainsInOrder(
86+
[
87+
'Failed to canonicalize the script uri',
88+
'The system cannot find the file specified.',
89+
],
90+
),
91+
);
92+
}
93+
}
6994
for (final exeUri in [
7095
absoluteExeUri,
7196
relativeExeUri,
@@ -354,3 +379,12 @@ Future<void> _withTempDir(Future<void> Function(Uri tempUri) fun) async {
354379
}
355380
}
356381
}
382+
383+
Uri removeDotExe(Uri withExe) {
384+
final exeName = withExe.pathSegments.lastWhere((e) => e.isNotEmpty);
385+
if (!exeName.endsWith('.exe')) {
386+
throw StateError('Expected executable to end in .exe, got $exeName');
387+
}
388+
final fileName = exeName.replaceAll('.exe', '');
389+
return withExe.resolve(fileName);
390+
}

runtime/bin/native_assets_api_impl.cc

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "include/bin/native_assets_api.h"
66

7+
#include "bin/utils.h"
78
#include "platform/globals.h"
89
#include "platform/utils.h"
910

@@ -54,7 +55,7 @@ const int file_schema_length = 7;
5455

5556
// Get a file uri with only forward slashes from the script path.
5657
// Returned string must be freed by caller.
57-
CStringUniquePtr CleanScriptUri(const char* script_uri) {
58+
CStringUniquePtr CleanScriptUri(const char* script_uri, char** error) {
5859
CStringUniquePtr script_path;
5960

6061
if (strlen(script_uri) > file_schema_length &&
@@ -68,8 +69,15 @@ CStringUniquePtr CleanScriptUri(const char* script_uri) {
6869
}
6970

7071
// Resolve symlinks.
71-
char canon_path[PATH_MAX];
72-
File::GetCanonicalPath(nullptr, script_path.get(), canon_path, PATH_MAX);
72+
const char* canon_path = File::GetCanonicalPath(nullptr, script_path.get());
73+
if (canon_path == nullptr) {
74+
OSError os_error;
75+
SET_ERROR_MSG(
76+
error,
77+
"Failed to canonicalize the script uri '%s'. OS error: '%s' (%i).",
78+
script_path.get(), os_error.message(), os_error.code());
79+
return CStringUniquePtr();
80+
}
7381

7482
// Convert path to Uri. Inspired by sdk/lib/core/uri.dart _makeFileUri.
7583
// Only has a single case, the path is always absolute and always a file.
@@ -141,7 +149,10 @@ void* NativeAssets::DlopenRelative(const char* path,
141149
const char* script_uri,
142150
char** error) {
143151
void* handle = nullptr;
144-
CStringUniquePtr platform_script_cstr = CleanScriptUri(script_uri);
152+
CStringUniquePtr platform_script_cstr = CleanScriptUri(script_uri, error);
153+
if (*error != nullptr) {
154+
return nullptr;
155+
}
145156
const intptr_t len = strlen(path);
146157
char* path_copy = reinterpret_cast<char*>(malloc(len + 1));
147158
snprintf(path_copy, len + 1, "%s", path);

0 commit comments

Comments
 (0)