Generate command string to execute without system path errors

I am currently having difficulty implementing a “run” module for my project, for which I am currently only adding build functionality.

import haxe.Json;
import sys.FileSystem;

using StringTools;

class Main

    public static function main()
        var args = Sys.args();
        var dir:String = args[args.length - 1];
        var useVerbose = false;
        var includedCompilationOptions = new Array<String>();
        var _application_path = Sys.getCwd();
        var _source_folder = "./";
        for (i in 1...args.length)
            switch (args[i])
                case "-v":
                    useVerbose = true;
                case "-D":
                    includedCompilationOptions.push(args[i + 1]);
                case "-input":
                    _source_folder = args[i + 1];
        var commandLine:String = "";
        var convos = new Array<String>();
        if (args[0] == "build")
            if (FileSystem.exists(dir + "/project.json"))
                var data:Dynamic = Json.parse(File.getContent(dir + "/project.json"));
                var target:String =;
                var output:String = data.output;
                convos = data.convos;
                if (data.source)
                    _source_folder = data.source;
                commandLine = 'haxe -cp $_source_folder -main Main -$target $output -lib sd2 -lib hscript ';
                for (i in 0...convos.length)
                    var filePath:String = Path.normalize(convos[i]);
                    var fileName:String = filePath.substring(filePath.lastIndexOf('/') + 1, filePath.lastIndexOf('.'));
                    commandLine += '-resource "$filePath"@$fileName ';
                commandLine += includedCompilationOptions.join(' ');
            if (useVerbose)
                Sys.println("Resources used: ");
                for (i in 0...convos.length)
            Sys.println("Starting build...");

When I use haxelib run sd2 build -v -D hscriptPos I get the following command generated:

haxe -cp ./ -main Main -js ../../scripts/sd2.js -lib sd2 -lib hscript -resource "../../chapters/01/start.sdc"@start -v -D hscriptPos

I execute this command later using Sys.command but I keep getting The system cannot find the path specified. errors. I’ve been trying to tackle this problem for several days, trying to find out the source of the problem.

When I trace the arguments being passed from the run module, the last argument is the path of the working directory, which I set using Sys.setCwd but this doesn’t seem to be the reason why I’m getting the errors. I’ve tried normalising the paths I’m using but that also doesn’t seem to make a difference.

I just don’t understand what’s causing these errors. If I copy and paste the command above and execute it directly in command line, it works.

I can’t see why the code implementation above doesn’t work. Can anyone notice the problem? Thanks in advance.

Perhaps it has something to do with command escaping?

Also, perhaps instead of relative paths to the Haxe command, you could resolve the paths in your tool first, and pass absolute paths in?

I’ve changed the code around to make absolute paths instead, I’m getting a different error now:

The input line is too long.

With the generated command:

haxe -cp "C:\Users\Luke\Documents\Games\The Spiritual Transition\Fall of Atlantis\build\source/" -main Main -js "C:/Users/Luke/Documents/Games/The Spiritual Transition/Fall of Atlantis/scripts/sd2.js" -lib sd2 -lib hscript -resource "C:/Users/Luke/Documents/Games/The Spiritual Transition/Fall of Atlantis/chapters/01/start.sdc"@start

Definitely getting closer to the answer… It’s very annoying, though. Windows these days…

What I could do is generate a hxml file and then execute that via Sys.command using haxe path/to/build.hxml

Okay, so I’ve implemented the above and the following generates the file:

-cp "C:\Users\Luke\Documents\Games\The Spiritual Transition\Fall of Atlantis\build\source/"
 -main Main
 -js "C:/Users/Luke/Documents/Games/The Spiritual Transition/Fall of Atlantis/scripts/sd2.js"
 -lib sd2
 -lib hscript
-resource "C:/Users/Luke/Documents/Games/The Spiritual Transition/Fall of Atlantis/chapters/01/start.sdc"@start

But apparently Haxe can’t find the file C:/Users/Luke/Documents/Games/The Spiritual Transition/Fall of Atlantis/chapters/01/start.sdc

I know it’s there, because when I paste in File Explorer it opens it successfully in Notepad. Does Haxe have issues with spaces in file paths?

EDIT: I’ve tried both absolute and relative paths to the resource file and Haxe apparently can’t find it, even though I know it’s there. I’ve tried both forward and backward slashes but to no avail. I decided to use the haxe build.hxml command directly, and of course that doesn’t work. So now I know what path it’s now complaining about but I don’t get why it’s complaining. The file path is definitely there, but for some reason Haxe can’t access it.

Second EDIT: Okay, so apparently using speech marks to wrap file paths with the -resource is incorrect. It should at least tell you that’s the error instead of telling me a very ambiguous error message. But finally got it working!