Comments (10)
Discovered when investigating dart-lang/test#2243.
from sdk.
Seems to be the absolute path which breaks copy
(and copySync
) for me. The following shows the same error:
import "dart:io";
void main() {
var test = File(r'test.');
try {
test.copySync(r'copyTest.txt');
print("Success");
} catch (e) {
print("$e");
}
test = test.absolute;
try {
test.copySync(r'copyTest.txt'); // Fails.
print("Success");
} catch (e) {
print("$e");
}
}
Also works with .\test.
and ..\testdata\test.
, so it's not any preceding path, only an absolute one, that breaks it.
from sdk.
https://dart-review.googlesource.com/c/sdk/+/356680 is the culprit
from sdk.
Per https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN#win32-file-namespaces on Windows you can't have (trailing) periods in filenames unless you prefix the filename with '\\?\'.
Since we do prefix filenames with '\?' before doing a copy
Line 816 in ca3e67a
We could strip trailing periods before adding a prefix
diff --git a/runtime/bin/file_win.cc b/runtime/bin/file_win.cc
index 66cadbedf1b..bf2c0c058e1 100644
--- a/runtime/bin/file_win.cc
+++ b/runtime/bin/file_win.cc
@@ -408,10 +408,14 @@ static std::unique_ptr<wchar_t[]> ToWinAPIPath(const char* utf8_path,
return absolute_path;
}
- // Add prefix and replace forward slashes with backward slashes.
+ // Add prefix, remove trailing periods, replace forward slashes with
+ // backward slashes.
auto result =
std::make_unique<wchar_t[]>(kLongPathPrefixLength + path_length + 1);
wcsncpy(result.get(), kLongPathPrefix, kLongPathPrefixLength);
+ if (is_file) {
+ while (path_length > 0 && absolute_path[path_length - 1] == '.') {
+ --path_length;
+ }
+ }
for (int i = 0; i < path_length; i++) {
result.get()[kLongPathPrefixLength + i] =
absolute_path[i] == L'/' ? L'\\' : absolute_path[i];
cc @mraleph
from sdk.
@aam I don't think the proposed change is enough. Rules around trailing dots apply to all path components, not just the trailing one. Consider path test.\test.
for example.
I think we have a choice to make:
- We could add better support for path where components include trailing dots - this would require rewriting
ToWinAPIFilePath
to avoid usingGetFullPathNameW
which currently eats those dots and then forcing\\?\
prefix if any components contain.
. - We can silently apply Win32 path normalization rules in
ToWinAPIFilePath
and strip trailing dots from all path components (so thattest.\test.
becomestest\test
). Maybe just usingPathCchCanonicalizeEx
would do the trick? - We could do a breaking change and make
dart:io
throw an error when path contains a component with a trailing dot.
I am in doubt that we actually want option 2 - because it silently leads to a very surprising behavior, especially when people are trying to write portable code. I think we should go for either 1 or 3. I am leaning towards 1.
Any opinions?
from sdk.
I would be fine with either option 1 or 3, I would not be a fan of option 2, that seems very surprising.
from sdk.
"Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not." (https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file)
It is true in Windows 11 - you are not able to cd/dir into such a directory and you can't do anything with it in Windows Explorer.
Given that, it's not clear whether writing our own canonicalizer that instead of using GetFullPathNameW
is worth it.
Also at present, if user wants to they can explicitly add '\?' prefix to a path/filename with trailing periods and file operations will work.
But then forward slashes won't be accepted anymore, user have to specify backslashes, meaning no canonicalization to such filenames.
import "dart:io";
void main() {
var test = File(r'\\?\c:\src\d2\sdk\test.');
test.writeAsStringSync('kuka');
try {
test.copySync(r'copyTest.txt');
print("Success");
} catch (e) {
print("$e");
}
test = test.absolute;
print(test.path);
try {
test.copySync(r'copyTest.txt'); // Fails.
print("Success");
} catch (e) {
print("$e");
}
}
PS C:\src\d2\sdk> out\ReleaseX64\dart d1.dart
Success
\\?\c:\src\d2\sdk\test.
Success
PS C:\src\d2\sdk>
from sdk.
Also note that Windows CLI shell as well as Windows Explorer does strip trailing periods from filenames automatically:
PS C:\src\d2\sdk> dir a
Get-ChildItem: Cannot find path 'C:\src\d2\sdk\a' because it does not exist.
PS C:\src\d2\sdk> copy .\d1.dart a....
PS C:\src\d2\sdk> dir a
Directory: C:\src\d2\sdk
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 6/13/2024 1:17 PM 420 a
PS C:\src\d2\sdk>
from sdk.
While stripping dots would be more "Windows-native", I think we should opt for supporting dots in filepaths instead - because we are multiplatform language.
We already hide some of the WinAPI complexity away from users (e.g. we don't require them to know about \\?\
prefix which is necessary to work with long file names), so I think we should just silently supports trailing dots as well.
We might want to add some warnings in File
and Directory
documentations that there are certain operating system specific recommendations around path (link to Windows docs) which users better adhere to.
from sdk.
https://dart-review.googlesource.com/c/sdk/+/375421 with the fix
from sdk.
Related Issues (20)
- "http/browser_client.dart" import causes tests to fail because of "http-1.2.2/lib/src/browser_client.dart:6:8: Error: Dart library 'dart:js_interop' is not available on this platform." HOT 3
- [Wildcard Variables] implement `THIS_ACCESS_FROM_FIELD_INITIALIZER` HOT 2
- dart2js: Internal Error: type 'InterfaceType' is not a subtype of type 'FunctionType' in type cast HOT 3
- DateTime.tryParse non consistent behavior across platforms HOT 1
- [Wildcard Variables][analyzer] Const wildcard variables have no errors when referenced HOT 2
- [Wildcard Variables][analyzer] Const wildcard functions have no errors when referenced HOT 1
- Internal error: Exception while using a AvoidRedundantArgumentValues to visit... HOT 2
- [Wildcard Variables][analyzer] Const wildcard initializer list variables should not accessed. HOT 1
- unintended_html_in_doc_comment triggers on some variants of allowed html tags in package:web HOT 1
- js_interop changes in 3.6 (master) to JSTypedArray subtypes break passing SharedArrayBuffer as the underlying buffer. HOT 4
- Dart analyzer fails to analyze condition in a list construction in enhanced enum constructor HOT 2
- [CP] Fix crashes due to unhandled StructuralParameterTypes in UP in the CFE HOT 1
- Macro with Map param doesn't work properly HOT 1
- [native assets] Add dynamic linking integration test for `dart` HOT 1
- Analyzer doesn't notice incorrect return type of generic method HOT 5
- [dart2wasm][ffi] Error: Argument to 'Native.addressOf' must be annotated with @Native. HOT 3
- [cfe/ffi] Cascading error on wrong types when using `.address` HOT 1
- Call dart function from a dynamic lib HOT 3
- [CP] Fix analysis_options.yaml "include:" for nested folders in workspace. HOT 1
- False positive in unintended_html_in_doc_comment lint HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sdk.