diff --git a/source/dub/generators/build.d b/source/dub/generators/build.d index 15e284b..dda7bb1 100644 --- a/source/dub/generators/build.d +++ b/source/dub/generators/build.d @@ -65,7 +65,7 @@ prepareGeneration(buildsettings); // determine the absolute target path - if( !Path(buildsettings.targetPath).absolute ) + if (!Path(buildsettings.targetPath).absolute) buildsettings.targetPath = (m_project.mainPackage.path ~ Path(buildsettings.targetPath)).toNativeString(); // make all target/import paths relative @@ -77,7 +77,7 @@ Path exe_file_path; bool is_temp_target = false; if (generate_binary) { - if (settings.run && !isWritableDir(Path(buildsettings.targetPath))) { + if (settings.run && !isWritableDir(Path(buildsettings.targetPath), true)) { import std.random; auto rnd = to!string(uniform(uint.min, uint.max)); buildsettings.targetPath = (tmp~"dub/"~rnd).toNativeString(); @@ -156,12 +156,16 @@ // copy files and run the executable if (generate_binary && settings.run) { if (buildsettings.targetType == TargetType.executable) { + auto runcwd = cwd; if (buildsettings.workingDirectory.length) { - logDiagnostic("Switching to %s", (cwd ~ buildsettings.workingDirectory).toNativeString()); - chdir((cwd ~ buildsettings.workingDirectory).toNativeString()); + runcwd = cwd ~ buildsettings.workingDirectory; + logDiagnostic("Switching to %s", runcwd.toNativeString()); + chdir(runcwd.toNativeString()); } scope(exit) chdir(cwd.toNativeString()); - logInfo("Running %s...", exe_file_path.toNativeString()); + if (!exe_file_path.absolute) exe_file_path = cwd ~ exe_file_path; + exe_file_path = exe_file_path.relativeTo(runcwd); + logInfo("Running %s %s", exe_file_path.toNativeString(), settings.runArgs.join(" ")); auto prg_pid = spawnProcess(exe_file_path.toNativeString() ~ settings.runArgs); auto result = prg_pid.wait(); enforce(result == 0, "Program exited with code "~to!string(result)); diff --git a/source/dub/generators/rdmd.d b/source/dub/generators/rdmd.d index ac627ff..2ee3bee 100644 --- a/source/dub/generators/rdmd.d +++ b/source/dub/generators/rdmd.d @@ -59,7 +59,7 @@ Path run_exe_file; bool tmp_target = false; if (generate_binary) { - if (settings.run && !isWritableDir(Path(buildsettings.targetPath))) { + if (settings.run && !isWritableDir(Path(buildsettings.targetPath), true)) { import std.random; auto rnd = to!string(uniform(uint.min, uint.max)) ~ "-"; buildsettings.targetPath = (getTempDir()~".rdmd/source/").toNativeString(); @@ -101,11 +101,16 @@ if (generate_binary && settings.run) { if (buildsettings.targetType == TargetType.executable) { auto cwd = Path(getcwd()); - if (buildsettings.workingDirectory.length) { - logDiagnostic("Switching to %s", (cwd ~ buildsettings.workingDirectory).toNativeString()); - chdir((cwd ~ buildsettings.workingDirectory).toNativeString()); - } + auto runcwd = cwd; + if (buildsettings.workingDirectory.length) + runcwd = cwd ~ buildsettings.workingDirectory; + logDiagnostic("Switching to %s", runcwd.toNativeString()); + chdir(runcwd.toNativeString()); scope(exit) chdir(cwd.toNativeString()); + + if (!run_exe_file.absolute) run_exe_file = cwd ~ run_exe_file; + run_exe_file = run_exe_file.relativeTo(runcwd); + logInfo("Running %s...", run_exe_file.toNativeString()); auto prg_pid = spawnProcess(run_exe_file.toNativeString() ~ settings.runArgs); result = prg_pid.wait(); diff --git a/source/dub/internal/utils.d b/source/dub/internal/utils.d index 47e2c59..97ebe8d 100644 --- a/source/dub/internal/utils.d +++ b/source/dub/internal/utils.d @@ -43,10 +43,11 @@ return true; } -bool isWritableDir(Path p) +bool isWritableDir(Path p, bool create_if_missing = false) { import std.random; auto fname = p ~ format("__dub_write_test_%08X", uniform(0, uint.max)); + if (create_if_missing && !exists(p.toNativeString())) mkdirRecurse(p.toNativeString()); try openFile(fname, FileMode.CreateTrunc).close(); catch return false; remove(fname.toNativeString()); diff --git a/source/dub/internal/vibecompat/inet/path.d b/source/dub/internal/vibecompat/inet/path.d index f76ac75..51564b9 100644 --- a/source/dub/internal/vibecompat/inet/path.d +++ b/source/dub/internal/vibecompat/inet/path.d @@ -133,6 +133,7 @@ /// Computes the relative path from `parentPath` to this path. Path relativeTo(const Path parentPath) const { + assert(this.absolute && parentPath.absolute); version(Windows){ // a path such as ..\C:\windows is not valid, so force the path to stay absolute in this case if( this.absolute && !this.empty && m_nodes[0].toString().endsWith(":") &&