Skip to content

script that uses Platform.script on command line changes when run via pub run #1136

Closed
@DartBot

Description

@DartBot

<img src="https://avatars.githubusercontent.com/u/5479?v=3" align="left" width="96" height="96"hspace="10"> Issue by sethladd
Originally opened as dart-lang/sdk#21008


(not sure if this is a bug or just my misunderstanding?)

I have a command-line utility that I'm writing. One of the things it does is read some of the files inside of itself. To do this, it does:

var path = Platform.script.toFilePath();

This works when I just run my script using dart. However, this fails when I run via pub run:

~/Code/stagehand[copy-dir-of-files*]$ pub run stagehand
Unhandled exception:
Unsupported operation: Cannot extract a file path from a http URI

­0 Uri.toFilePath (dart:core/uri.dart:1583)

­1 WebStarterKitGenerator._loadFiles (package:stagehand/generators/webstarterkit.dart:25:42)

­2 WebStarterKitGenerator.WebStarterKitGenerator (package:stagehand/generators/webstarterkit.dart:18:15)

­3 generators (package:stagehand/stagehand.dart:20:36)

­4 main (http://localhost:49605/stagehand.dart:20:27)

­5 _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:212)

­6 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:122)

Why am I doing Platform.script.toFilePath() ? I need to get a list of files inside a directory of my package. So I figured I'd first found out where I am and them go into lib/

basically, I need to be able to list files inside of my package. The next few lines go a little something like this:

    var script = new File(path);
    var dir = script.parent;
    var filesDir = new Directory([dir.path, '..', 'lib', 'generators',
        'webstarterkit'].join(Platform.pathSeparator));
    print(filesDir.existsSync());
    print(filesDir.path);

Should I be able to do Platform.script.toFilePath() and still let users run this script via pub run ?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions