-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Can't get path to current script #1145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Set owner to @whesse. |
This comment was originally written by [email protected] Is that a duplicate of Issue #409? |
Yes, sorry. I didn't find the other bug when I searched to see if this had already been reported. |
After thinking about this some more, I don't believe this is the same issue as bug #405. My understanding is that that bug is for: // a/b/main.dart // c/d/other.dart $ dart_bin ./a/b/main.dart What I want here is the ability to get this: // a/b/main.dart // c/d/other.dart $ dart_bin ./a/b/main.dart Something like FILE in C++, or file in Python. My specific use case is I have a test script that needs to load some test data from a file. It knows where the file lives relative to itself. It does not know where it lives relative to where the user is invoking dart or relative to where the main script that ultimately #imports it is. Re-opening this bug since as far as I can tell the issue is still valid. I need this to work in frog too. Should I file a separate bug for that? Added Triaged label. |
It seems to me that this would be a URI property of a library, saying where the library is loaded from. Is this part of reflection? I don't see it as likely that a file that is included by #source would be able to find its location, but a library imported by #import may be able to know its URI. cc @gbracha. |
This comment was originally written by [email protected] file / $0 are highly desirable. Please find a way to do this. |
I think this is really about the reflection API. Basically, what URI was used to import the library. Added Accepted label. |
Unfortunately, the URI may not be available after deployment, tree-shaking etc. So we cannot rely on it. We may want a different solution to get at resources bundled with the application. Added WontFix label. |
The primary use case for this is command-line scripts that need to find other files relative to themselves, so I'm OK with it not being available for deployed or tree-shaken apps. |
This is currently blocking Pub's ability to install packages from the SDK without forcing the user to manually specify the SDK path. Please re-open or suggest an alternative that will be available in the short term. |
So, if this is strictly for server side apps, why not write a native function? |
Three reasons.
|
Nathan, I do not understand why the executable() in the Options interface is not good enough for what you need. |
The Options.executable returns the command-line invocation used to run the current script. What I'm asking for is a way to get the path of the current Dart file. Bob gave a good example above of the functional difference between the two. In practical terms, the return values of Options.executable and Options.script only provide the ability to figure out where the Dart executable or the script entrypoint are. Neither of these suffice for all the use-cases of file. It's important that a library be able to figure out where it exists in the filesystem so that it can (for example) load resources from its installed directory. |
I'm trying to reconcile two positions here:
As I've said, the "path to the current library" has no meaning in a deployed application. More that that - it has no meaning for the Dart program at runtime - that is why an implementation is allowed to lose this structure during compilation. In principle, one could require that a library have a property URI; however, that property would necessarily be misleading, as it has no reliable meaning in a deployed application. The first logically executed import that loads a library sets the URI property based on the (resolved) URI used in that import. In a deployed application, it might set the URI to the URI of the application as a whole (or to an error value, or null, or whatever). Is this what you are asking for? Or do you actually expect to know what the path is for code in a sourced file? This variation is particularly unattractive. Either way I can't say I'm comfortable adding such deployment-dependent functionality. Your use case might be served by a native function in the VM. Perhaps the VM needs to add something to the embedder API. |
Note that the request here isn't for the path to the current library, it's the path to the current file. For Pub, those two are equivalent since Pub doesn't use #source, but that isn't true for all possible use cases here.
That would be ideal, but I'd settle for path to the main library file.
That's perfectly fine. This bug was merged with #409, which is a library bug. Options.executable and Options.script are part of the embedder and not in the language proper. This could be as simple as having an implicitly available top-level constant like FILE that's defined magically by the VM in every library the same way exit() is now. |
Added this to the Later milestone. |
Re-opening since, I think, the discussion shows that this is still an open issue. Added Triaged label. |
We have a Platform.script now. Closing. Please reopen if that is not correct. |
The best solution here is really a resource API like the one @lrhn is working on, though, so I'm fine with leaving this issue closed. |
The Resource API doesn't give you the path to your script, though. It does allow you to load files/things with a package: URI and get the same resolution logic used when importing a Dart library via package:. Maybe that was the intent of this issue? If so, great! |
It can give you the real URL for a |
As far as I know, the API is still in the design stage. |
I would also like this please. |
+1 I want this feature. Is this can be done in some way? |
+1 Please. As for |
I am trying to load a natively compiled file and trying to run it using Process.run but when I use it as a library and get to another folder the path to the compiled file changes . I need the path of the original library here ! For example This is mouse.dart: import 'dart:io';
class _Mouse {
void position() {
if (Platform.isWindows) {
} else {
Directory.current = '../../bin/scripts/mouse/darwin/';
Process.run('./mouse', ['position']).then((results) {
print(results);
});
}
}
}
final mouse = _Mouse(); The path to mouse file ( a compiled cli tool ) works if I execute this from mouse.dart but if I switch to example.dart: import 'package:dart_autogui/dart_autogui.dart';
void main() {
mouse.position();
} This generates an error as follows :
This should not happen . If I had something like |
Guys, it's 2025 and we still cannot get the absolute path of a file ? How should loading of native libraries supposed to work ? Should the user be forced to download these in an extra step ? Or getting hold of some other resources bundled with the dart package .... this is a standard feature in all major languages. And it's still not possible to get the absolute path of an installed package, whcih would be a workaround for installed packages at least. |
When writing Dart scripts that read other files, it's helpful if you can identify that file with a path that's relative to the script itself. Right now, I don't think there's a way to get that in Dart. Instead, paths are always relative to where Dart was invoked from. This causes problems if you want to invoke the same script from different paths and have it still be able to reliably find files.
For example, I have a unit test (utils/tests/dartdoc/src/dartdoc_tests.dart) that wants to load a file in its same directory (dummy.dart). This unit test is run both through test.dart and frog's presubmit.py from different paths. Short of "let me see if it's here? no, how about here?" kind of hacks, I don't know of a clean way to support that.
The text was updated successfully, but these errors were encountered: