From 9cdadaa464647dae3076059d2755850ae9449270 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Mon, 8 May 2023 10:35:42 +0000 Subject: [PATCH 1/2] Exclude paths that start with ".." from recursive globbing This mainly targets `importPaths ".."`, but to some degree applies to all paths that may occur in a package description file. One use case for requiring such a declaration is when the module root is outside the Dub project root. For example, the library "foo" containing a D module "foo.bar" may be in a directory called "foo" and have "bar.d" at the directory root (same directory as "dub.sdl"). Currently, setting importPaths to ".." has the unpleasant side effect of scanning the entire parent directory (which may host other, unrelated projects). Dub does this for reasons such as caching; the build process itself does not require a list of all files that may or may not be imported, as the compiler discovers them not by globbing, but by path construction and file existence checks, based on the names of imported modules and the list of import paths specified with -I. Ideally, Dub should NEVER recursively glob the importPaths list, and instead communicate with the compiler to discover the full list of files that were actually imported (e.g. from compilers' verbose output). However, this change (which should not affect canonical use cases) should facilitate this particular directory structure. --- source/dub/recipe/packagerecipe.d | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/source/dub/recipe/packagerecipe.d b/source/dub/recipe/packagerecipe.d index c8bb5ecc82..eca476b6f6 100644 --- a/source/dub/recipe/packagerecipe.d +++ b/source/dub/recipe/packagerecipe.d @@ -494,6 +494,14 @@ struct BuildSettingsTemplate { continue; foreach (spath; paths) { + // exclude paths outside projects' roots from recursive globbing + import std.algorithm : startsWith; + import std.path : pathSplitter; + if (spath.pathSplitter.startsWith("..".only)) { + logDiagnostic("Not globbing path outside project root: %s", spath); + continue; + } + enforce(!spath.empty, "Paths must not be empty strings."); auto path = NativePath(spath); if (!path.absolute) path = base_path ~ path; From 33f38899a6ba73936c40cd76e21e52efab4e4981 Mon Sep 17 00:00:00 2001 From: Vladimir Panteleev Date: Mon, 8 May 2023 14:45:30 +0000 Subject: [PATCH 2/2] Add test for `importPaths ".."` --- test/issue2637-parent-directory.sh | 14 ++++++++++++++ test/issue2637-parent-directory/.no_build | 0 test/issue2637-parent-directory/pkg/.gitignore | 1 + test/issue2637-parent-directory/pkg/dub.sdl | 4 ++++ test/issue2637-parent-directory/pkg/mod.di | 3 +++ test/issue2637-parent-directory/pkg/prog.d | 9 +++++++++ 6 files changed, 31 insertions(+) create mode 100755 test/issue2637-parent-directory.sh create mode 100644 test/issue2637-parent-directory/.no_build create mode 100644 test/issue2637-parent-directory/pkg/.gitignore create mode 100644 test/issue2637-parent-directory/pkg/dub.sdl create mode 100644 test/issue2637-parent-directory/pkg/mod.di create mode 100644 test/issue2637-parent-directory/pkg/prog.d diff --git a/test/issue2637-parent-directory.sh b/test/issue2637-parent-directory.sh new file mode 100755 index 0000000000..3bdc7a4323 --- /dev/null +++ b/test/issue2637-parent-directory.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash + +set -e + +. $(dirname "${BASH_SOURCE[0]}")/common.sh +cd "${CURR_DIR}/issue2637-parent-directory" +test ! -e bad || rmdir bad +mkdir -m 000 bad +trap 'rmdir bad' EXIT + +( + cd pkg + $DUB run +) diff --git a/test/issue2637-parent-directory/.no_build b/test/issue2637-parent-directory/.no_build new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/issue2637-parent-directory/pkg/.gitignore b/test/issue2637-parent-directory/pkg/.gitignore new file mode 100644 index 0000000000..fc5bd90832 --- /dev/null +++ b/test/issue2637-parent-directory/pkg/.gitignore @@ -0,0 +1 @@ +/pkg diff --git a/test/issue2637-parent-directory/pkg/dub.sdl b/test/issue2637-parent-directory/pkg/dub.sdl new file mode 100644 index 0000000000..f3843edd2d --- /dev/null +++ b/test/issue2637-parent-directory/pkg/dub.sdl @@ -0,0 +1,4 @@ +name "pkg" +targetType "executable" +sourceFiles "prog.d" +importPaths ".." diff --git a/test/issue2637-parent-directory/pkg/mod.di b/test/issue2637-parent-directory/pkg/mod.di new file mode 100644 index 0000000000..4499e187f6 --- /dev/null +++ b/test/issue2637-parent-directory/pkg/mod.di @@ -0,0 +1,3 @@ +module pkg.mod; + +enum message = "Hello"; diff --git a/test/issue2637-parent-directory/pkg/prog.d b/test/issue2637-parent-directory/pkg/prog.d new file mode 100644 index 0000000000..0ace211f65 --- /dev/null +++ b/test/issue2637-parent-directory/pkg/prog.d @@ -0,0 +1,9 @@ +module pkg.prog; + +import std.stdio; +import pkg.mod; + +void main() +{ + writeln(message); +}